0

I am making a game on python and tkinter, and I would like to save the highscores of the game. I have an entry which takes the name of the player and assign it to a global (Name1) and a global (Score1) in which the score is saved.

The thing is that I need a file that stores the best 10 highscores, obviously showing them from the biggest score to the lowest.

My question is: How do I make that?

How can I save the Name and the Score to the file? How can I save the scores with an order? (Including the name associated to the score)

I am really confused using the methods of .readline() and .write() and .seek() and all of those.

EDIT:

I need something like this:

The globals are Name1, Which lets say is "John" And Score1, Which at the end of the game will have an int object.

So after a few games, John achieved 3000 on the score, and then 3500, and then 2000.

The highscores file should look like:

John 3500
John 3000
John 2000

And if Mike comes out and play and achieve a 3200 score, the file should look like this:

John 3500
Mike 3200
John 3000
John 2000

The max amount of players on the highscore must be ten. So if there are already ten Highscores saved, and a bigger one comes out, the lowest score must be although ignored or deleted. (I need the file because I need to do the display on a Tkinter window)

PD: Excuse my weird English! Thank you!

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Bryan Black
  • 341
  • 2
  • 5
  • 13

2 Answers2

2

You can have a list of tuples in you script to store highscores and sort it before displaying it to the user. You can use shelve or pickle to store python variables(To store the highscores)

List of tuple will look like: [(Name1, Score1), (Name2, Score2),....,(Name10, score10)]

Community
  • 1
  • 1
user2109788
  • 1,266
  • 2
  • 12
  • 29
  • Thanks man! I had thought it before posting the question, the thing is that I didnt know how to do the logic part of sorting, writing, and reading. But Your answer is kind of like the one below. And the one below is explaining what you just said in a more practical way :) Thank You very much! – Bryan Black May 30 '14 at 10:18
2

user2109788's answer is basically all you need, and pickle or shelve is the best way of serialising your data given that the file doesn't need to be human readable.

Feeling generous today, the following code should be readily adaptable to your specific situation with tkinter.

To keep only 10 values in the high score table, I just add a new high score to the list, sorting it and retaining the first 10 values by slicing the sorted list. For 10 values it should be fine, but this would be a terrible idea if your list were very 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]

with open('highscores.txt', 'w') as f:
    pickle.dump(high_scores, f)


# unpickle
high_scores = []

with open('highscores.txt', 'r') as f:
    high_scores = pickle.load(f)
Talvalin
  • 7,789
  • 2
  • 30
  • 40
  • So just checking, whenever I open a new game, and get a new highscore I need to do the pickle.load(f) to get the high_scores list of the scores and then do the append, sorting and dumping? :) Thanks a lot by the way! – Bryan Black May 30 '14 at 10:14
  • If opening a new game means that all global information from a previous game is lost, then yes, you would use `pickle.load(f)` to get the high score list for display and then append, sort and dump after the game finishes. – Talvalin May 30 '14 at 10:26