我遇到了麻烦。它不会将当前分数保存为高分,因为它应该大于0.我希望代码保存分数然后当我再次播放时,高分更新。所有相关代码如下。谢谢:)我是Python / Pygame的新手
def highScores(high_score):
intro = True
while intro == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(black)
font1 = pygame.font.SysFont('gillsans', 35, bold = True)
font2 = pygame.font.SysFont('gillsans', 20)
title = font1.render("High Score", True, white)
gameDisplay.blit(title, (200,100))
first = font2.render("Your high score is: "+str(high_score), True, white)
gameDisplay.blit(first, (70,200))
pygame.draw.rect(gameDisplay, white, (350, 400, 100, 45))
button("Back",350, 400, 100, 45,"back")
pygame.display.update()
def highScore(count):
high_score = get_high_score(count)
smallText = pygame.font.SysFont('gillsans',30)
text = smallText.render("High Score: "+str(high_score), True, white)
gameDisplay.blit(text, (420,0))
def get_high_score(count):
high_score = count
try:
high_score_file = open("high_score.txt", "r")
high_score = int(high_score_file.read())
#high_score_file.close()
#return high_score
except:
pass
if count >= high_score:
return high_score
save_high_score()
def save_high_score(count):
try:
high_score_file = open("high_score.txt", "w")
high_score_file.write(str(count))
#high_score_file.close()
#return high_score
except:
pass
if count >= high_score:
return high_score
save_high_score()
答案 0 :(得分:1)
在get_high_score
中,您在保存之前返回:
return high_score
save_high_score()
您可以撤销这两个陈述:
save_high_score()
return high_score
如果您确实需要save_high_score
中的save_high_score
来电,则必须重构您的代码。这样做会发生意外的递归。