This question already has an answer here:
Why is nothing drawn in PyGame at all?

(1个答案)


在5个月前关闭。



import pygame, sys
pygame.init()
screen = pygame.display.set_mode([800,600])
white = [255, 255, 255]
red = [255, 0, 0]
screen.fill(white)
pygame.display.set_caption("My program")
pygame.display.flip()



background = input("What color would you like?: ")
if background == "red":
    screen.fill(red)

running = True
while running:
    for i in pygame.event.get():
        if i.type == pygame.QUIT:
        running = False
        pygame.quit()

我正在尝试问用户他想要什么背景色。如果用户写红色,则颜色不会改变,仍然保持白色。

最佳答案

下次更新显示时,它将重新绘制为红色。添加pygame.display.update():

background = input("What color would you like?: ")
if background == "red":
    screen.fill(red)
    pygame.display.update()

或者,您可以(有条件地)更改背景颜色后将pygame.display.flip()移至。

另请参阅Difference between pygame.display.update and pygame.display.flip

关于python - Pygame:如何更改背景颜色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41189928/

10-14 18:18