mardi 6 mars 2018

Why is If statement not working Python and Pygame

So I have created my first game and I want to track the high score with shelve. My logic is that I have a global variable called highscore. At the beginning of the game in game_menu() it is assigned the value that is in the highscore file. The logic is that if the current score (dodged) is greater than highscore, then the highscore file gets assigned the value of dodged and that is displayed in the game menu. This does work for the most part, but even if the dodged variable is smaller than highscore, it still gets assigned to the file. It's like the game is not seeing the If statement. Below is the important part of code of the game.

    d = shelve.open("highscore.txt")
    highscore = d["highscore"]
    d.close()

    best_score(highscore)

Above, we have assign to the global variable the value inside the file and display via best_score

    if dodged > highscore:
       d = shelve.open("highscore.txt")
       d["highscore"] = dodged
       d.close

This is where I think the problem is. It clearly says if dodged is bigger, but even if it's equal or smaller the file gets replaced with the dodged variable. I even tried putting in ifesle, but nothing works. Can someone help!

Aucun commentaire:

Enregistrer un commentaire