SCRATCH GOLD II
SCRATCH GOLD I
2 of 3

1.06 High score

Now save the high score so that you can play against your friends.

Add two new variables called high score and name to your project.

When the game ends because the player gets the sequence wrong, the game should check whether the score is higher than the current high score. If it is, the game should save the score as the high score, and also store the name of the player.

Add code to your character sprite to store the high score. Also ask for the player’s name, and store it in the name variable.

It’s fun to keep track of a high score in a game.

Let’s say you have a variable called score, which gets set to zero at the beginning of each game.

Add another variable called high score.

variables menu with Make a Variable highlighted
new variable popup box with high score as the variable name

At the end of the game (or whenever you want to update the high score), you’ll need to check whether you have a new high score.

code blocks require to make high score equal score

You need to add this new code to the character sprite for the other three colours too!

Can you see that the ‘Game over’ code for each of the four colours is exactly the same?

ballerina

If you need to change any of the ‘Game over’ code, for example to add a sound or change the ‘Game over’ message, you have to change it four times. That’s annoying and wastes a lot of time.

Instead, you can define your own code block, and use it anywhere in your project.

Click on My blocks, and then on Make a Block. Call this new block Game over.

Add the code from the else block connected to the red broadcast to the Game over block so that it looks like this:

ballerina

Now remove the code that’s in the else block connected to the red broadcast, and add in the Game over block instead:

ballerina

Test your new block by playing the game and clicking the red button at the wrong point in the colour sequence.

Your new Game over block is a function, a little script that you can use anywhere you like in your code by adding the Game over block in.

Also replace the code in the else  block connected to the broadcasts for the other colours with your new Game over block. Here is what the code for the blue message should look like

ballerina

Now add a sound that plays when the wrong button is pressed. You only need to add this code once in the Game over block that you made, and not four separate times!

ballerina
×
×

Cart