Reputation: 137
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
Upvotes: 0
Views: 792
Reputation: 1025
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()
Upvotes: 2