file - Highscores using python / Saving 10 highscores and ordering them -
i making game on python , tkinter, , save highscores of game. have entry wich takes name of player , assign global (name1) , global (score1) in wich score saved.
the thing need file stores best 10 highscores, showing them biggest score lowest.
my question is: how make that?
how can save name , score file? how can save scores order? (including name asociated score)
i confused using methods of .readline() , .write() , .seek() , of those.
edit:
i need this:
the globals name1, wich lets "john" , score1, wich @ end of game have int object.
so after few games, john achieved 3000 on score, , 3500, , 2000.
the highscores file should like:
john 3500
john 3000
john 2000
and if mike comes out , play , achieve 3200 score, file should this:
john 3500
mike 3200
john 3000
john 2000
the max amount of players on highscore must ten. if there ten highscores saved, , bigger 1 comes out, lowest score must althought ignored or deleted. (i need file because need display on tkinter window)
pd: excuse weird english! thank you!
user2109788's answer need, , pickle
or shelve
best way of serialising data given file doesn't need human readable.
feeling generous today, following code should readily adaptable specific situation tkinter.
to keep 10 values in high score table, add new high score list, sorting , retaining first 10 values slicing sorted list. 10 values should fine, terrible idea if list large.
from operator import itemgetter import pickle # pickle high_scores = [ ('liz', 1800), ('desi', 5000), ('mike', 3200), ('john', 2000), ('gabi', 3150), ('john', 3500), ('gabi', 3100), ('john', 3000), ('liz', 2800), ('desi', 2800), ] high_scores.append(('dave', 3300)) high_scores = sorted(high_scores, key=itemgetter(1), reverse=true)[:10] open('highscores.txt', 'w') f: pickle.dump(high_scores, f) # unpickle high_scores = [] open('highscores.txt', 'r') f: high_scores = pickle.load(f)
Comments
Post a Comment