python - 需要其他方式来停止我的游戏(pygame)!

标签 python crash pygame infinite-loop

计时器结束后,我希望显示“游戏结束”文本,并停止执行其他所有操作。我的程序在第65行崩溃(时间== 0之后,游戏循环中的while循环)。除了无限循环之外,还有什么其他方法可以做到这一点?

import pygame, sys
from pygame.locals import *

pygame.init()

FPS = 100
fpsClock = pygame.time.Clock()

DISPLAYSURF = pygame.display.set_mode((500, 500))
pygame.display.set_caption('Cookie Clicker')

cookie = pygame.image.load('cookie.png')
cookieX = 95
cookieY = 65
bgColor = (56, 142, 142)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

pygame.mixer.music.load('Grind.mp3')
pygame.mixer.music.play(-1, 10.2)

text = '0'
fontObj = pygame.font.Font('freesansbold.ttf', 35)
textSurfaceObj = fontObj.render(text, True, BLACK)
textRectObj = textSurfaceObj.get_rect()
textRectObj.center = (250, 420)

time = 2
seconds = ' seconds'
fontObj2 = pygame.font.Font('freesansbold.ttf', 35)
textSurfaceObj2 = fontObj2.render(str(time)+seconds, True, BLACK)
textRectObj2 = textSurfaceObj2.get_rect()
textRectObj2.center = (370, 30) 

pygame.time.set_timer(USEREVENT+1, 1000)

gameover = 'Game Over'
fontObj3 = pygame.font.Font('freesansbold.ttf', 72)
textSurfaceObj3 = fontObj3.render(gameover, True, BLACK, WHITE)
textRectObj3 = textSurfaceObj3.get_rect()
textRectObj3.center = (250, 250) 

#game-loop
while True:
    DISPLAYSURF.fill(bgColor)
    DISPLAYSURF.blit(cookie, (cookieX, cookieY))
    DISPLAYSURF.blit(textSurfaceObj, textRectObj)
    DISPLAYSURF.blit(textSurfaceObj2, textRectObj2)

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == MOUSEBUTTONDOWN:
            temp = int(text)
            temp += 1
            text = str(temp)
        if event.type == USEREVENT+1:
            time -= 1
            if time == 0:
                textSurfaceObj2 = fontObj2.render(str(time)+seconds, True, BLACK)
                DISPLAYSURF.blit(textSurfaceObj2, textRectObj2)
                DISPLAYSURF.blit(textSurfaceObj3, textRectObj3)
                pygame.display.update()
                while True:
                    pass


    textSurfaceObj = fontObj.render(text, True, BLACK)

    textSurfaceObj2 = fontObj2.render(str(time)+seconds, True, BLACK) 

    pygame.display.update()

    fpsClock.tick(FPS)

最佳答案

您不能只做while time > 0吗?

关于python - 需要其他方式来停止我的游戏(pygame)!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26749254/

相关文章:

python - 使用来自 CSV 的输入从 Python 运行动态查询

python - ArrayField 上的 DRF 搜索

ios - 通常会在[ZXCapture dealloc]崩溃

solr - 30 秒后 Riak 开始崩溃

python - 在 pygame 中来回移动 Sprite

python - 播放 WAV 歌曲时机器人不跳舞

python - 从 numpy.uint8 数组中提取无符号字符

java - Android,将覆盖图标添加到 onLocationChange 导致崩溃

python - Pygame Sprite 不会移动但没有错误?

python - 如何在 Python 中抑制控制台输出?