【发布时间】:2020-07-22 06:41:21
【问题描述】:
每当我尝试运行我的程序时,它都会冻结并崩溃。我不是 pygame 编码大师,但我几乎可以肯定这与我的主循环有关。它应该允许用户使用箭头键左右移动。我尝试添加时钟并更改屏幕大小,但没有奏效。代码如下:
import pygame
import sys
import random
import time
pygame.init()
screen = pygame.display.set_mode((500,500))
events = pygame.event.get()
clock = pygame.time.Clock()
x = 50
y = 50
game_over = False
while not game_over:
for event in events:
if event.type != pygame.QUIT:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
x += 5
elif event.key == pygame.K_LEFT:
x -= 5
else:
sys.exit()
screen.fill((0,0,0))
pygame.draw.rect(screen, (255,255,255), (x,y,50,50))
clock.tick(30)
pygame.display.update()
【问题讨论】:
-
是否可以将 Python 生成的错误信息包含在内?