pygame.display.update() 未检测到新更改

pygame.display.update() not detecting new changes

所以我已经将 window 颜色更改为白色,不幸的是它没有更新,it also returns an error

代码:

import pygame

WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display = pygame.display.set_caption('PyGame Test')
WHITE = (255,255,255)
def main():
  run = True
  while run:
      for event in pygame.event.get():
          if event.type == pygame.QUIT:
              run = False

      WIN.fill(WHITE)
      pygame.display.update()
      
  pygame.quit()

if __name__ == '__main__':
  main()

我做错了什么?

我相信你需要初始化显示模块。试试 pygame.init()

http://www.pygame.org/docs/ref/display.html#pygame.display.init

要设置 window 的标题,您需要键入 pygame.display.set_caption("Caption")。在您的代码中,我看到您键入了 pygame.display = pygame.display.set_caption('PyGame Test'),结果是 AttributeError,因为 pygame.display.set_caption() returns None,这意味着您将 pygame.display 设置为None。因此,如果您将该行替换为 pygame.display.set_caption('PyGame Test'),它将起作用。

完整代码:

import pygame

WIDTH, HEIGHT = 900, 500

WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('PyGame Test')
WHITE = (255, 255, 255)


def main():
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        WIN.fill(WHITE)
        pygame.display.update()

    pygame.quit()


if __name__ == '__main__':
    main()

有关详细信息,请查看 this page