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
variable.name
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?
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:
Now remove the code that’s in the
block connected to the else
broadcast, and add in the red
block instead:Game over
Test your new block by playing the game and clicking the red button at the wrong point in the colour sequence.
Your new
block is a function, a little script that you can use anywhere you like in your code by adding the Game over
block in.Game over
Also replace the code in the
block connected to the else
broadcasts
for the other colours with your new
block. Here is what the code for the Game over
blue
message should look like
Now add a sound that plays when the wrong button is pressed. You only need to add this code once in the
block that you made, and not four separate times!Game over