简体   繁体   English

Pygame:如何更改背景颜色?

[英]Pygame: How to change background color?

I am new to python.我是 python 的新手。 Previously I have used C and C++.以前我使用过 C 和 C++。 I am creating a pygame and I am using rapidtables.com for this work but however my game window is still not showing the required color.(It's showing but only when I close my game window).我正在创建一个 pygame 并且我正在使用 rapidtables.com 来完成这项工作,但是我的游戏 window 仍然没有显示我所需的游戏窗口。(它只在我关闭时显示)。 Well here is my code-好吧,这是我的代码-

 #Space Invaders from turtle import Screen, screensize import pygame #Intialize the game pygame.init() #formation of screen screen = pygame.display.set_mode((800,600)) #Title pygame.display.set_caption("Space Invaders") #Game loop done = False while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True pygame.display.flip() #RGB, Red, Green and blue screen.fill((255, 0, 0)) pygame.display.update()

My game window is still not showing the required color (It's showing but only when I close my game window).我的游戏 window 仍然没有显示所需的颜色(它显示但仅在我关闭游戏窗口时显示)。

This is happening because you aren't changing the window color inside the game loop.发生这种情况是因为您没有游戏循环中更改 window 颜色。 The reason why you only see it when you close the game window is because when you close the window, the pygame.QUIT event is triggered.之所以只在关闭游戏 window 时看到,是因为在关闭 window 时,触发了pygame.QUIT事件。 In your code, when that event is triggered, you set done to True , meaning the loop will stop running and the code outside of the loop will be executed.在您的代码中,当该事件被触发时,您将done设置为True ,这意味着循环将停止运行并且循环外的代码将被执行。 Since you are filling the window and updating the display outside of the loop, the screen only gets colored when the loop is no longer running (only when the pygame.QUIT event is triggered).由于您正在填充 window 并更新循环外部的显示,因此仅当循环不再运行时屏幕才会着色(仅当触发pygame.QUIT事件时)。 So to fix that, move screen.fill((255, 0, 0)) to inside your game loop.因此,要解决这个问题,请将screen.fill((255, 0, 0))移动到游戏循环内部。

Modified Game Loop:修改后的游戏循环:

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    # RGB, Red, Green and blue
    screen.fill((255, 0, 0))
    pygame.display.flip()
pygame.quit()
sys.exit(0)

I added pygame.quit and sys.exit(0) in order to quit the game and end the program (don't forget to add import sys ).我添加了pygame.quitsys.exit(0)以退出游戏并结束程序(不要忘记添加import sys )。

Full Code:完整代码:

# Space Invaders
from turtle import Screen, screensize
import pygame
import sys

# Intialize the game
pygame.init()

# formation of screen
screen = pygame.display.set_mode((800, 600))

# Title
pygame.display.set_caption("Space Invaders")

# Game loop
done = False

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    # RGB, Red, Green and blue
    screen.fill((255, 0, 0))
    pygame.display.flip()
pygame.quit()
sys.exit(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM