In many kind of Scratch games, you want to create games that store score and track high scores and ranmajesties from one game to the next. In this short article, I present you exactly how to create a high score leaderboard for a game that will persist throughout sessions and display the ranqueens of the optimal three scores by name.

You are watching: How to make a scoreboard in scratch

You can include this leaderboard to any kind of game you have available. I"m going to use the Asteroid Blaster game that"s included as one of the sample projects bundled through the Raspberry Pi version of Scratch <1>. I"m also going to incorpoprice the manuscript from the "Stamping Numbers" short article in the previous issue <2> that uses numbered costumes to display screen the score at the finish of the game. Figure 1 mirrors the finish outcome.


*
Figure 1: Asteroid Blaster leaderboard.

In Asteroid Blaster, the player has 3 resides to relocate a ship and also shoot as many kind of asteroids as feasible. The player loses a life each time the ship collides through an asteroid. The game ends after the 3rd collision. That"s wbelow I"m going to jump in and capture the scores to develop a high score leaderboard. The game needs to track the peak 3 scores, so each time the game ends, I should check if the existing score is higher than one of the peak three scores. If the score is in the height three spots, I document the player"s name and also score.

To finish the programming, I want to screen the current player"s score and, after the optimal scores are evaluated, the height three high scores. Then I"ll prompt the player to play one more game.

Video Game Over, Check the Scores

I"m adding all my leaderboard scripts to a brand-new sprite called Score, which will incorporate costumes representing numbers, as I disputed in the previous article. Figure 2 shows the sprite"s initialization manuscript founding via the when I receive(Play Game) block. As you deserve to view, you should produce and also hide three variables named High Score 1, High Score 2, and High Score 3. The factor I"m making use of a Play Game broadactors instead of the usual as soon as clicked block is because the player will have an option to rebegin the game. I"ve likewise updated all the other scripts in the existing game to begin on this broadactors message. The clear block removes the stamped score from the display screen.


*
Figure 2: Initializing the Score sprite.

Figure 3 shows the manuscript that runs after the player provides all three stays. It"s prompted with a once I receive(Video Game Over), which replaces the say(Video Game Over) for(2) secs block in the Asteroid Blaster ship sprite. Without rewriting too a lot of the original manuscript, you need to include a speak script block after the broadcast(Game Over) block; this prevents the game from restarting immediately.


*
Figure 3: Finding and also displaying top-three scores.

The script calls for 2 lists, Leaderboard Names and also Leaderboard Scores, that are used to capture the leaders and also their high scores. The Leaderboard Scores list demands to contain a number in each of the initially 3 list items, or the testimonial that checks whether the score is better than the existing value will certainly not work; with a non-number, the inspect will always evaluate to false and no player will certainly ever before be able to enter a high score.

I pick to include a zero to each of the initially three items as soon as I create the Leaderboard Scores list. This ensures that the score evaluations later on in the manuscript will work-related. You have the right to either add the zeros directly to the list"s phase monitor or usage the add (0) to (Leaderboard Scores) block.

Scratch will certainly remember the worths of the lists and also variables from one session to the next, so making use of this manuscript, Scratch retains the name and scores from one session to the following, making it possible to track the high scores after the game has actually been closed and reopened in Scratch. You can even send your game to a friend and maintain the list values. This is why I carry out not incorporate any blocks to initialize the beginning values of the Leaderboard Scores and Leaderboard Names lists. After I put up the game, the just time I desire the high scores to readjust is when a player earns a top-3 score.

Next off, you must create the variable named Leaderboard Position to track each place in the lists as the manuscript checks the existing score to identify if it"s a high score. The initially thing the Game Over manuscript does is stamp the player"s existing score making use of the broadcast(Show Current Score) block. That script is not pictured.

Next, I evaluate the score to view if it is greater than the lowest score in the list, which is item 3 in Leaderboard Scores. If the current score isn"t greater, there"s no factor to proceed checking anypoint else, and it"s an chance to display screen a practice message to the player.

If the score is huge enough to be one of the height three scores, I set the Leaderboard Position variable to 1 bereason I desire to start my testimonial through the initially item in the list. The repeat(3) loop then evaluates whether or not score is better than each item in the list. I"m only interested in capturing the top-3 high scores ever, yet if you wanted to have a bigger leaderboard, you can adjust the 3 to one more number you discover appropriate. I figure if the top-3 spots are good sufficient for the Olympic podium, it"s great sufficient for Asteroid Blaster.

In the Leaderboard Scores list, item 1 is the highest score and also item 3 is the lowest. So, after the script determines that the player has actually a top-three score, my initially check is to watch if the score is better tha things 1. If the score is larger than the worth in item 1, I recognize that the player earned a high score. The manuscript can include the score into the initially item of the Leaderboard Scores list.

If the score is not higher than the worth in item 1 of the list, the manuscript following checks the value in item 2 by making use of the Leaderboard Position variable to identify exactly how many type of times the script has actually run through the repeat() loop to the list item number and thereby recognize what list item is being evaluated. If the score is better than the worth in item 2, you likewise know that the player"s score is either less than or equal to the top score. So, the manuscript deserve to add the score to item 2. If the score is much less than the value in item 2, it moves onto item 3.

When you insert a new item right into the list, the staying worths are moved to the next place in the list. At this point, the list will contain four items.

After you identify the high score the person earned, prompt the player for a name by informing them their rank. The nested join() () blocks uses a mix of text and variables to attain this. The Leaderboard Position that you add to the join() () block not just tracks which item number in the Leaderboard Scores list is being evaluated, it represents the player"s rank.

After the player enters a name, it"s added to the Leaderboard Names list in the same place as the score was added to the Leaderboard Scores list. Now that you have an association, if you want to display screen the second highest score, for example, you would certainly retrieve the item 2 worths from both the Leaderboard Names and also Leaderboard Scores lists. As lengthy as you add, modify, or delete both lists at the very same time, tbelow will certainly constantly be a clear partnership that you can rely on, and you"ll check out that relationship as soon as it comes time to display the high scores.

The broadcast(Show Top Scores) and wait blocks provided in Figure 3 will certainly create the script in Figure 4.


*
Figure 4: Keeping track of top-3 scores.
Sjust how Top Scores

As shown in Figure 4, you incorporate the Leaderboard Names and also Leaderboard Scores for each list item into a high score variable. Everypoint matches. For instance, High Score 1 is assigned the worths in the initially items of Leaderboard Names and also Leaderboard Scores. The scrip provides the present variable() block to screen a monitor on the stage via the brand-new values for each of the high score variables. Figure 1 shows what this looks like at the end of the game.

Being able to manage the screen of the variables via a block allows you to present and also hide the variable value fairly quickly. If you were to allow the variable monitors to be shown continually, the stage would come to be cluttered.

You have the right to not position the phase monitor by means of the script; but, you can drag the monitors approximately the stage to place them. Then, each time you screen the variable, it will certainly present up where you last positioned it.

Although it"s possible to display and hide each variable"s stage monitor, lists carry out not have equivalent present or hide blocks. To display screen the list monitors on the stage, you would certainly need to go to the Variables palette and also select the list you want to present or hide.

See more: Alice Through The Looking Glass Hair, Club Alice Hair At Through The Looking Glass

The final few blocks of the script in Figure 4 prompt the player to start one more game. Pressing y will rebegin the game by means of the Play Video Game broadactors. Any other input will finish the game. To use the Play Game broadcast to replay the game in Asteroid Blaster, you"ll need to readjust the scripts for the ship, laser, and also asteroids to start as soon as each script receives the broadactors.