为什么按下按钮后屏幕颜色保持不变?

问题描述 投票:0回答:1

import pygame


pygame.init()
screen = pygame.display.set_mode((700, 700))
pygame.display.set_caption('Checkers')


while True:
  screen.fill((114,157,224))
  pygame.display.update()
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
    pygame.quit()
  elif event.type == pygame.KEYDOWN:
    if event.key == pygame.K_a:
      screen.fill((70,44,133))
      pygame.display.update()

我尝试将 pygame.KEYDOWN 更改为 pygame.key.get_pressed() 但没有任何改变。尽管我用谷歌搜索,甚至询问 ChatGPT 我没有正确的答案:(

python pygame game-development
1个回答
0
投票

因为您将以下代码放入 while 中,所以在每次循环迭代中屏幕颜色自动变为蓝色。将这段代码放在设置标题之后一段时间即可解决问题。

screen.fill((114,157,224))
pygame.display.update()

您的正确代码如下:

import pygame

pygame.init()
screen = pygame.display.set_mode((700, 700))
pygame.display.set_caption('Checkers')
# correct place of initial color of display
screen.fill((114, 157, 224))
pygame.display.update()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                screen.fill((70, 44, 133))
                pygame.display.update()
© www.soinside.com 2019 - 2024. All rights reserved.