我正在尝试在 pygame 中制作一个游戏,其中玩家移动玩家矩形,当它们与其他矩形碰撞时,他们会获得分数。现在,我只是想让蓝点矩形以固定间隔(4 秒)在屏幕上的随机点生成,并在 3.5 秒后消失,然后重新出现在不同的位置。然而,在某些时候该程序就会停止工作。它在打印“def blue point”(添加用于测试)后立即停止,并且 pygame 屏幕停止加载,它说“没有响应”,我必须将其关闭。没有错误消息,所以我很难找出问题所在。
我尝试简化代码,但没有任何效果。我是 pygame 的新手,所以我很困惑。
这是我认为相关的代码:
import pygame
import sys
import random
running = True
#set timers:
#lightbluepts timer, 4 sec
time_delay2 = 4000
BLUEPT = pygame.USEREVENT + 2
pygame.time.set_timer(BLUEPT , time_delay2, 7)
#how long blue pts last, 3.5 secs
time_delay3 = 3500
ENDBLPT = pygame.USEREVENT + 3
pygame.init()
while running:
events = pygame.event.get()
for event in events:
if event.type == BLUEPT:
blpt = True
bluerect = deflightbluepts()
pygame.time.set_timer(ENDBLPT, time_delay3, 1)
print('def blue pt')
elif event.type == ENDBLPT:
blpt = False
print('END blue pt')
elif event.type == TIME_UP:
print("tick tock")
screen.fill((0,0,0))
pygame.draw.rect(screen, "green", player_rect)
#draw point rectangles
while blpt:
pygame.draw.rect(screen, "blue", bluerect)
pygame.display.update()
fpsClock.tick_busy_loop(FPS)
最佳答案
while blpt:
是一个无限循环。将其替换为 if blpt:
。 blpt
只决定blurect
是否在当前帧绘制:
while running:
# [...]
screen.fill((0,0,0))
pygame.draw.rect(screen, "green", player_rect)
#draw point rectangles
if blpt:
pygame.draw.rect(screen, "blue", bluerect)
pygame.display.update()
关于python - 为什么我的程序停止响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74999002/