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

标签 python pygame

我正在尝试在 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/

相关文章:

python - 如何在 Debian jessie 上安装适用于 Python 3 的 pygame?

python - 属性错误: 'class' object has no attribute 'rect'

python - 为什么 Pandas 内连接会给出 ValueError : len(left_on) must equal the number of levels in the index of "right"?

python - 将 python 应用程序转换为 webapp 的最简单框架?

python - 在 HUG 上更改端口

python - 我应该如何使用pygame连续旋转图像?

python - 如何将多个列表附加到 python 字典中的一个键?

python - pygame需要键盘中断来初始化显示

python - 为什么我的 pygame 在 Mac 上加载到空白屏幕?

python - 用 Pygame 画抛物线