Python 为什么不是';这不是我在Pygame上的高分储蓄吗?

Python 为什么不是';这不是我在Pygame上的高分储蓄吗?,python,pygame,Python,Pygame,我在这方面遇到了麻烦。当当前分数大于0时,它不会将其保存为高分。我希望代码保存分数,然后当我再次播放时,高分被更新。所有相关代码如下所示。谢谢:)我是Python/Pygame新手 def highScores(high_score): intro = True while intro == True: for event in pygame.event.get(): if event.type == pygame.QUIT:

我在这方面遇到了麻烦。当当前分数大于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()

在“获取高分”中,保存前返回:

    return high_score
    save_high_score()
你可以颠倒这两种说法:

    save_high_score()
    return high_score

如果您确实需要
save_high_score
调用
save_high_score
,则必须重构代码。这样做将执行一个意外的递归。

获取高分
中,您将在保存之前返回:

    return high_score
    save_high_score()
你可以颠倒这两种说法:

    save_high_score()
    return high_score

如果您确实需要
save_high_score
调用
save_high_score
,则必须重构代码。这样做会产生意外的递归。

def get_high_score(count):
执行
save_high_score()
之前,看起来您正在返回,执行时应该包含参数
count
。i、 e.
保存高分(计数)
。在代码中放入一些
print
命令,以显示执行的内容和时间。不要使用除s之外的裸
,因为这会隐藏所有可能的错误,并使程序很难调试。始终指定您期望的异常,例如
除了IOError:
,然后为用户打印消息。此外,请尽量缩短
try
子句。在
def get_high_score(count)中:
在执行
save_high_score()
之前,看起来您正在返回,并且在执行时应包含参数
count
。i、 e.
保存高分(计数)
。在代码中放入一些
print
命令,以显示执行的内容和时间。不要使用除
s之外的裸
,因为这会隐藏所有可能的错误,并使程序很难调试。始终指定您期望的异常,例如
除了IOError:
,然后为用户打印消息。此外,请将
try
子句尽量简短。