How to Save Max Scores in TinyDB
(for an updated version with score, name, and date, see
Saving highscore)
Sample runs (depending on custom formatting)
You will need a TinyDB tag to save your list under, and a global variable to hold the list while you work on it:
At startup, load the list from TinyDB:
Pay special attention to the list that is loaded for first time use.
The high scores list is a list of lists (a table), with each row of the table a pair (score, name).
I added a stopper entry (0, "Beat My Score") for the end of the high score list, to make it easier to stop at the end when searching for an insertion point when adding new scores.
Designer
We have a Label for the Max Scores, two text boxes (new score and name), and a Button to Add a new pair (Score, Name).
The addition process is in a separate procedure, add_a_high_score.
I used one of two possible lazy ways to show the scores, (see above).
The procedure uses a local variable to track where it should insert the new score pair into the global high_scores list. Starting at slot 1, it goes past all the higher scores to find where to insert the new score. (This is where my stopper at the end helps, so I don't have to go checking for end of list. On second thought, what if some one gets a negative score? Try it and see what happens.)
After the higher scores have been passed, we have the new insert index for our new pair, so we do the insert. We then immediately store the new global high_scores list back to TinyDB. (Never leave off to tomorrow what you can do today.)
The source:
max_score.aia (2.8 KB)
The blocks: