Python 3.7 pygame 错误"not responding"用于工作



我正试图用python制作一艘移动的宇宙飞船,但我收到了一个错误,上面写着"没有响应";我以前在pygame中加载过代码,但现在它不起作用,我不知道为什么这里有代码

import pygame
def player(x, y):
screen.blit(playerImg, (x, y))
pygame.init()
screen = pygame.display.set_mode((800, 600))
playerImg = pygame.image.load("spaceship.png")
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

playerX = 350
playerY = 500
playerX_change = 0
playerX -= 2
running = True;
while running:
screen.fill((255, 0, 0))
player(playerX, playerY)
playerX -= 1
pygame.display.update()

您需要修复主循环以侦听系统事件。

试试这个代码:

running = True;
while running:
for events in pygame.event.get(): #get all pygame events
if events.type == pygame.QUIT: #if event is quit then shutdown window and program
pygame.quit()
sys.exit()
screen.fill((255, 0, 0))
player(playerX, playerY)
playerX -= 1
pygame.display.update()

最新更新