Add a high score table to your Scratch game

By Russell Barnes. Posted

Keep players coming back for more by keeping a record of the best scores, and telling them how they measure up

This tutorial can be found in The MagPi 37

This project features scripts that enable you to create a high score table, and then add new scores to it if they’re high enough. There isn’t an easy way to display and hide a list from within your program, so the scripts also tell players how they ranked and what the next highest score is, so they know how close they came to beating it. This code will work with most simple games, but you might need to make some changes if your game invites players to play again, or has scripts that continue when the game has ended.

You'll need

One of your Scratch games. Pick a favourite!

STEP-01 Make your game

You’ll need a game to add this script to – either one of your own, or one that you’ve programmed from a book or magazine. Try playing the game a few times to work out the likely scores. Some games award a few points, some hundreds, and some thousands. The starting numbers in your high score table should present a challenge to players, but not be completely unachievable. Take care with your own games: if you’ve spent days playing them in development, they’ll be much easier for you than anyone else.

STEP-02 Add your high score sprite

The scripts for the high score can all go on the same sprite. This sprite will tell players if they got a high score. It could be the main character of your game, the sprite used on the title screen (see The MagPi 36), or it could be a new sprite. We’ve added the sprite royalperson for our high score table. You’ll find it in the 'people' folder, even though it looks like a dog. It’ll be in the way during the game, so add Listing 1 to hide it when the green flag is clicked.

 Listing 1

STEP-03 Set up your list

Your high score table will be stored in a list. Click the Variables button above the Blocks Palette, click the button to make a list and call it ‘high scores’. In the Blocks Palette, you can click the tickbox beside the list name to show or hide the list on the Stage. This is a handy way to view the whole list, and you can edit the values in it by clicking them and typing on them. The list gets in the way of your game, so we recommend unticking the box.

STEP-04 Set your starting scores

You can type some starting scores into the list on the Stage, but it’s better to use a script to generate your high scores. Listing 2 does this. It runs if it receives the broadcast reset high scores, but you can also click the script once to reset your scores. To change the lowest score, change the value in the set high score loop block. To change how much scores go up by, edit the value in the change high score loop block. Note: the pointed Operator blocks are shown as rounded in our code because of limitations in the Scratchblocks software we use for laying out code for the magazine.

 Listing 2

STEP-05 Add your high score code

Listing 3 checks the score and adds it to the high score table in the correct position if high enough. It also tells the player how well they did. Add it to your high score sprite. Take care with building the script that goes in the hole of the repeat until block. You’ll need to drag in blocks in a similar order to this: or, >, item 1 of high scores, high score loop, =, high score loop. When the next highest score is announced, add blocks in the order: say Hello! for 2 secs, join hello world, item 1 of high scores, -, high score loop.

 Listing 3

STEP-06 Patch it in to your game

To finish, connect your high score script to your game. If the game doesn’t already use the variable score, click on Variables and make that variable for all sprites. You want the high score script to run when your game ends, so you need to add some code at that point in your game. Add a block to set score to your game’s score variable if you’re not already using the variable score in the game. Finally, add a block to broadcast check high scores. To keep your high scores, simply save your game. When you save a Scratch program, the list values, including your high score table in this case, are saved too.

This tutorial was written by Sean McManus

From The MagPi store

Subscribe

Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.