需要另一种方法来阻止我的游戏(pygame)!

时间:2014-11-05 03:33:52

标签: python crash pygame infinite-loop

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

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)

1 个答案:

答案 0 :(得分:0)

你不能做while time > 0之类的事情吗?