ok, i made a high score system with python for my pinball game.
basically it saves the high scores to text files, then opens them again when it needs to.
import GameLogic as G
cont = G.getCurrentController()
own = cont.getOwner()
gameover = cont.getSensor("gameover")
text = open("data\high_score1.txt", "r")
current_score1 = text.read()
current_score1 = int(current_score1)
text.close()
text = open("data\high_score2.txt", "r")
current_score2 = text.read()
current_score2 = int(current_score2)
text.close()
text = open("data\high_score3.txt", "r")
current_score3 = text.read()
current_score3 = int(current_score3)
text.close()
text = open("data\high_score4.txt", "r")
current_score4 = text.read()
current_score4 = int(current_score4)
text.close()
text = open("data\high_score5.txt", "r")
current_score5 = text.read()
current_score5 = int(current_score5)
text.close()
scores = [current_score1, current_score2, current_score3, current_score4, current_score5]
scores.sort()
scores.reverse()
if gameover.isPositive():
new_score = own.points
scores = [current_score1, current_score2, current_score3, current_score4, current_score5, new_score]
scores.sort()
scores.reverse()
print "scores", scores
text_file = open("data\high_score1.txt", "w")
new_score1 = str(scores[0])
text_file.write(new_score1)
text_file.close()
text_file = open("data\high_score2.txt", "w")
new_score2 = str(scores[1])
text_file.write(new_score2)
text_file.close()
text_file = open("data\high_score3.txt", "w")
new_score3 = str(scores[2])
text_file.write(new_score3)
text_file.close()
text_file = open("data\high_score4.txt", "w")
new_score4 = str(scores[3])
text_file.write(new_score4)
text_file.close()
text_file = open("data\high_score5.txt", "w")
new_score5 = str(scores[4])
text_file.write(new_score5)
text_file.close()
that all works perfectly, you can see how i open text files for writing and reading.
now i want to add an overlay scene to the ‘game over’ scene ‘if’ the new score is higher than the current high score. so when the game is over (when your number of balls left == -1), i open the text file “new_score” and write the score to that file.
import GameLogic as G
cont = G.getCurrentController()
own = cont.getOwner()
gameover = cont.getSensor("gameover")
if gameover.isPositive():
score = own.points
score = str(score)
text_file = open("data
ew_score.txt", "w")
text_file.write(score)
text_file.close()
print "new score recorded"
the problem is that i get an error saying that new_score doesnt exist. oh, but it does. im not making a mistake( not one that i can spot) it worked perfectly for the first part of code. where i opened all five of those.
see, heres a picture for proof

im confused. i rewrote the second part of code i dont know how many times. it should work!
all of the files are in the ‘data’ subdirectory so thats why its text_file = open(“data\high_score2.txt”, "w blah blah


