python 为什么我的程序停止响应?

hrirmatl  于 2023-01-04  发布在  Python
关注(0)|答案(1)|浏览(96)

我尝试在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)
vx6bjr1n

vx6bjr1n1#

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

相关问题