如何更改 Pygame 中的背景颜色?
How to change the background color in Pygame?
我正在使用 pygame 创建游戏,我正在使用 rapidtables.com 来完成这项工作,但我的游戏 window 仍然没有显示所需的颜色。 (它正在显示,但只有在我关闭游戏时才会显示 window)。这是我的代码-
#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更改颜色。之所以在关闭游戏window时才看到,是因为在关闭window时,触发了pygame.QUIT
事件。在您的代码中,当该事件被触发时,您将 done
设置为 True
,这意味着循环将停止 运行 并且将执行循环外的代码。由于您正在填充 window 并在循环外更新显示,因此只有当循环不再是 运行 时(仅当 pygame.QUIT
事件被触发时,屏幕才会变色)。所以要解决这个问题,请将 screen.fill((255, 0, 0))
移到游戏循环中。
修改后的游戏循环:
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)
我添加了pygame.quit()
和sys.exit(0)
是为了退出游戏和结束程序(别忘了添加import sys
)。
完整代码:
# 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)
我正在使用 pygame 创建游戏,我正在使用 rapidtables.com 来完成这项工作,但我的游戏 window 仍然没有显示所需的颜色。 (它正在显示,但只有在我关闭游戏时才会显示 window)。这是我的代码-
#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更改颜色。之所以在关闭游戏window时才看到,是因为在关闭window时,触发了pygame.QUIT
事件。在您的代码中,当该事件被触发时,您将 done
设置为 True
,这意味着循环将停止 运行 并且将执行循环外的代码。由于您正在填充 window 并在循环外更新显示,因此只有当循环不再是 运行 时(仅当 pygame.QUIT
事件被触发时,屏幕才会变色)。所以要解决这个问题,请将 screen.fill((255, 0, 0))
移到游戏循环中。
修改后的游戏循环:
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)
我添加了pygame.quit()
和sys.exit(0)
是为了退出游戏和结束程序(别忘了添加import sys
)。
完整代码:
# 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)