0

I created a small game. I would like to save the 3 highest scores in a textfile and display them after the game. I created a textfile with following content: 0 0 0 (should represent the status before you play game for first time). I created 2 functions update_highscores() and display_highscores(), but nothing happens after finishing the game. the achieved scores are not stored in the text file and the highscores were not displayed after the game. How can i save and show the highscores?

def update_highscores():
    global score, scores
    file = "C:\Programmieren\Eigene Spiele\Catch The Bananas\highscores.txt"
    scores=[]
    with open(filename, "r") as file:
        line = file.readline()
        high_scores = line.split()

    for high_score in high_scores:
        if (score > int(high_score)):
            scores.append(str(score) + " ")
            score = int(high_score)
        else:
            scores.append(str(high_score) + " ")

    with open (filename, "w") as file:
        for high_score in scores:
            file.write(high_score)


def display_highscores():
    screen.draw.text("HIGHSCORES", (350,150), fontsize=40, color = "black")
    y = 200
    position = 1
    for high_score in scores:
        screen.draw.text(str(position) + ". " + high_score, (350, y), color = "black")
        y = y + 25
        position = position + 1
Diego
  • 216
  • 1
  • 11
siclaro
  • 137
  • 13
  • You define `file` and then overwrite it in `with open(filename) as file:` (and I can't see where filename is defined). – Steve Oct 21 '19 at 17:41
  • Is it a typo that you made your file path `file` but when you open it you say `filename`? This should cause an error when the function is called. – Mike - SMT Oct 21 '19 at 17:48

1 Answers1

2

The update_highscores code should work fine if you change

file = "C:\Programmieren\Eigene Spiele\Catch The Bananas\highscores.txt"

to

filename = r"C:\Programmieren\Eigene Spiele\Catch The Bananas\highscores.txt"

The two things that changed were: changing file to filename, otherwise this code throws an exception because filename is not defined. I assume it's meant to be this way. The second thing I changed is adding an r before the string so that the backslashes are interpreted literally. Two other options that would also work are:

"C:\\Programmieren\\Eigene Spiele\\Catch The Bananas\\highscores.txt"

or

"C:/Programmieren/Eigene Spiele/Catch The Bananas/highscores.txt"

Just remember that a single backslash in a non-raw string will usually try to escape the next character.

Aside from that, just make sure that the file exists, and that it contains 0 0 0, or any sequence of characters separated by spaces. If the file is not initialized properly, there won't be any scores to replace.

This code works for me, so if there's still a problem, it's just with displaying the scores. They update in the file just fine. But I don't know what library you're using for screen, so I can't test that.

Oh, also: make sure you're actually calling the function. I assume it's elsewhere in your code and you just omitted it. Obviously, your code won't work if you don't call the function.

Here is my code that works. Just replace the path to highscores.txt and run this code by itself. If it works, the problem is somewhere else in your code, and we won't be able to help you unless you give us more of your code.

score = int(input("Enter new score: "))
scores = []

def update_highscores():
    global score, scores
    filename = r"path\to\highscores.txt"
    scores=[]
    with open(filename, "r") as file:
        line = file.readline()
        high_scores = line.split()

    for high_score in high_scores:
        if (score > int(high_score)):
            scores.append(str(score) + " ")
            score = int(high_score)
        else:
            scores.append(str(high_score) + " ")

    with open (filename, "w") as file:
        for high_score in scores:
            print(high_score)
            file.write(high_score)


update_highscores()
input()
Ahndwoo
  • 1,025
  • 4
  • 16
  • Thankyou, i changed it exactly as you described it, but it is still not working. Nothing happens regarding saving the highscores. For "screen" i use pygame. – siclaro Oct 23 '19 at 10:20
  • @siclaro I've modified my answer to include my working code. Try running that code (having replaced the `filename` with your path). – Ahndwoo Oct 23 '19 at 11:47
  • thankyou! It is working when i do it your way (input of Highscore manually). But the highscore the player reaches should be saved automatically. This is the sorce code of the game: https://stackoverflow.com/questions/58325813/pyinstaller-error-message-name-actor-is-not-defined – siclaro Oct 26 '19 at 11:42