2016-12-03 4 views
0

私はこれに問題があります。それは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() 
+2

'get get_high_score(count):'あなたが 'save_high_score()'を実行する前に、あなたが戻ってきたように見えます。実行するときにパラメータ 'count'を含める必要があります。すなわち 'save_high_score(count)'です。実行中のものと実行中のものを表示するために、いくつかの 'print'コマンドをコードに入れてください。 –

+0

裸の 'except'は使用しないでください。可能性のあるすべてのエラーが隠され、プログラムを非常に難しくするからです。あなたは 'IOError:を除いて'と思っている例外を常に指定してから、そのメッセージを出力します。また、 'try'節は可能な限り短くしておいてください。 – skrx

答えて

1

get_high_scoreでは、あなたが戻ってきているに新しいです:

return high_score 
    save_high_score() 

あなたはちょうど2つの文を逆転できる:

save_high_score() 
    return high_score 

あなたの場合実際にsave_high_scoreコールがsave_high_scoreにある必要がある場合は、コードをリファクタリングする必要があります。これを行うと、意図しない再帰が行われます。

関連する問題