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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With