Pygame窗口没有响应,然后显示错误

问题描述

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窗口没有响应,然后显示错误'无法调用int对象。我使用Visual Studio 2019的社区版本。

解决方法

首先,要解决程序给出的错误,您需要使用pygame.QUIT,而不是pygame.QUIT(),因为每个事件类型都被赋予了一个数字,而pygame.QUIT被设置为您返回时返回的数字点击十字架。

对于第二个问题,您只需要添加pygame.display.flip()或pygame.display.update(),因为这需要更新屏幕。它们基本上是等效的,但更新也可以采用矩形作为参数,从而只允许更新部分屏幕。

,

执行此操作

root = pygame.init()

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