Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving the highscore for a game?

I have made a very simple game in python using pygame. The score is based on whatever level the player reached. I have the level as a variable called score. I want to display the top level at the start or end of the game.

I would be even more happy to display more than one score, but all of the other threads I have seen were too complicated for me to understand, so please keep it simple: I'm a beginner, only one score is necessary.

like image 589
Kevin Klute Avatar asked May 24 '13 01:05

Kevin Klute


People also ask

How do you save a highScore in unity?

However, I could also use Player Prefs to store a high score value. For example, I could save the game's high score by setting a float Player Pref, with a string key of “highScore”, using the Set Float function of the Player Prefs class. I could then load the same value again using the Get Float function.

How do I add points in unity?

Then I need to update the UI to reflect the current score value. A basic start is to add ten points to the player score, which is done with the += operator. This is the same as saying, player points = player points +10. After the new score has been calculated, the UI can be updated by accessing the score text variable.


1 Answers

I recommend you use shelve. For example:

import shelve
d = shelve.open('score.txt')  # here you will save the score variable   
d['score'] = score            # thats all, now it is saved on disk.
d.close()

Next time you open your program use:

import shelve
d = shelve.open('score.txt')
score = d['score']  # the score is read from disk
d.close()

and it will be read from disk. You can use this technique to save a list of scores if you want in the same way.

like image 159
elyase Avatar answered Sep 25 '22 20:09

elyase