Creating a high score leaderboard
In many Scratch games, you want to create games that keep score and track high scores and rankings from one game to the next. In this article, I show you how to create a high score leaderboard for a game that will persist across sessions and display the rankings of the top three scores by name.
You can add this leaderboard to any game you have available. I'm going to use the Asteroid Blaster game that's included as one of the sample projects bundled with the Raspberry Pi version of Scratch [1]. I'm also going to incorporate the script from the "Stamping Numbers" article in the previous issue [2] that uses numbered costumes to display the score at the end of the game. Figure 1 shows the end result.
In Asteroid Blaster, the player has three lives to move a ship and shoot as many asteroids as possible. The player loses a life each time the ship collides with an asteroid. The game ends after the third collision. That's where I'm going to jump in and capture the scores to create a high score leaderboard. The game needs to track the top three scores, so each time the game ends, I need to check if the current score is higher than one of the top three scores. If the score is in the top three spots, I record the player's name and score.
[...]