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