Pygame:如何更改背景颜色 [英] Pygame: how to change background color

查看:167
本文介绍了Pygame:如何更改背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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()

我想询问用户他想要什么背景颜色.如果用户写红色,颜色不会改变,仍然保持白色.

I'm trying to ask the user what background color he would like to have. If the user writes red, the color doesn't change and still stays white.

推荐答案

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

It will redraw as red the next time you update the display. Add pygame.display.update():

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

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

Or, you could move the pygame.display.flip() to after you (conditionally) change the background color.

另见 pygame.display.update 和 pygame 之间的区别.display.flip

这篇关于Pygame:如何更改背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆