首页 > 解决方案 > python 3.7 pygame错误“没有响应”使用工作

问题描述

我正在尝试在 python 中制作一个移动的宇宙飞船,但我收到一条错误消息,上面写着“没有响应”

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()

标签: pythonpygame

解决方案


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

试试这个代码:

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()

推荐阅读