When the timer ends, I want the 'Game Over' text to display and everything else to stop executing. My program crashes on line 65 (the while-loop inside my game-loop, after time == 0). What's an alternative way to do this other than an infinite loop?
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)