gpt4 book ai didi

python - Pygame:如何改变背景颜色

转载 作者:太空宇宙 更新时间:2023-11-04 10:05:37 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com