Pygame window 没有响应然后显示错误

Pygame window is not responding and then it displays an error

import pygame
#initialize the screen
pygame.init()

#create the screen
screen = pygame.display.set_mode((800, 600))

#tile and icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load("spaceship.png")
pygame.display.set_icon(icon)

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

我的 pygame window 没有响应然后显示错误 'int object is not callable'。我使用 visual studio 2019 的社区版本。

首先,为了应对您的程序给出的错误,您需要使用 pygame.QUIT,而不是 pygame.QUIT(),因为每个事件类型都有一个数字并且设置了 pygame.QUIT单击十字时返回的数字。

对于第二个问题,您只需添加 pygame.display.flip() 或 pygame.display.update(),因为这是更新屏幕所必需的。它们基本上是等价的,但是 update 也可以接受一个矩形作为参数,让你只更新屏幕的一部分。

这样做,

root = pygame.init()

最重要的是,你忘了在程序末尾写root.mainloop()。 这就是您的 window 没有响应的主要原因。