Essential Scratch - 9 Multiple Choice Quiz

ESSENTIALS
ESSENTIALS
[ CHAPTER ELEVEN ]
ADD A HIGH
SCORE TABLE
Keep players coming back for more by keeping
a record of the best scores, and telling them
how they measure up
54
[ Chapter Eleven
One ] ]
[ LEARN TO CODE WITH SCRATCH ]
he sprite checks
the player’s score
and tells them
ho they did
ick the box in the
locks alette to
see the list and
edit its alues No
cheating no
his 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.
T
>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.
[ Add a High Score Table ]
55
ESSENTIALS
.01
>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
This code will work with most
simple games, but you might
need to make some changes
(see the previous chapter), or it could be a new sprite. We’ve added
the sprite ro alperson 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.
>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.
56
[ Chapter Eleven
One ] ]
[ LEARN
[ LEARNTOTOCODE
CODEWITH
WITHSCRATCH
SCRATCH] ]
.02
>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’ve used
for laying out code for this book.
[ Add a High Score Table ]
57
ESSENTIALS
.03
>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 it’s 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, >, ite 1 of high scores,
high score loop, =, high score loop. When the next highest score is
announced, add blocks in the order: sa ello for 2 secs, oin hello
world, ite 1 of high scores, , high score loop.
>STEP-06
Insert it into 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.
58
[ Chapter Eleven
One ] ]