gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:11:43 25 4
gpt4 key购买 nike

计时器结束后,我希望显示“游戏结束”文本,并停止执行其他所有操作。我的程序在第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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com