Pygame:在按键上更改屏幕颜色

时间:2014-06-07 06:17:48

标签: python colors pygame

我是pygame的新手,我正试图通过按键来改变screen.fill颜色。我将在下面提供任何帮助/提示的代码将非常感谢。

import pygame
from pygame.locals import *

red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
white = (255,255,255)

background_color = (0,0,0,)
(width, height) = (800, 600)

screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Color Test')
screen.fill(background_color)

pygame.display.flip()

running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            background_color = white
            print('left pressed')
            pygame.display.update()

1 个答案:

答案 0 :(得分:1)

更改background_color的值后,您需要填写屏幕。

...
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
        background_color = white
        print('left pressed')
        screen.fill(background_color) #<--- Here
        pygame.display.update
    ...