Memory
Memory
Projects
Memory
Create a game in which you memorise and repeat random
colour sequences
Step 1 Introduction
In this project, you will create a memory game in which you have to memorise and repeat a random sequence of colours!
What you will make
https://projects.raspberrypi.org/en/projects/memory/print 1/30
12/22/24, 2:51 PM Memory
Choose a character sprite and a backdrop. You could use the ballerina, but your character doesn’t have to be a
person, they only need to be able to show different colours.
1 = red
2 = blue
3 = green
4 = yellow
https://projects.raspberrypi.org/en/projects/memory/print 2/30
12/22/24, 2:51 PM Memory
Give your character four costumes that have different colours, one costumes for each of the four colours shown
above. Make sure that your coloured costumes are in the same order as the list above.
If you want, you can use the color a shape tool to fill parts of the costume with a different colour.
Next, add a list for storing the random sequence of colours that the player has to remember.
https://projects.raspberrypi.org/en/projects/memory/print 3/30
12/22/24, 2:51 PM Memory
Create a list called sequence. Only the character sprite needs to see this list, so you can select For this sprite only
when you create the list.
Make a list
Type in the name of your list. You can choose whether you would like your list to be available to all sprites, or
to only a specific sprite. Click OK.
Once you have created the list, it will be displayed on the stage, or you can untick the list in the Scripts tab to
hide it.
Click the + at the bottom of the list to add items, and click the cross next to an item to delete it.
https://projects.raspberrypi.org/en/projects/memory/print 4/30
12/22/24, 2:51 PM Memory
New blocks will appear and allow you to use your new list in your project.
You should now see lots of new code blocks for using lists. The empty list should be visible in the top left-hand corner of the
Stage.
https://projects.raspberrypi.org/en/projects/memory/print 5/30
12/22/24, 2:51 PM Memory
Each colour has a different number, so you can choose a random colour by randomly choosing a number and adding it to the
list.
Add this code to the character sprite to choose a random number and add it to sequence:
when clicked
Test your code. Check that, each time you click the flag, a random number between 1 and 4 gets added to the list.
https://projects.raspberrypi.org/en/projects/memory/print 6/30
12/22/24, 2:51 PM Memory
Can you add code to your program to generate five random numbers at once?
I need a hint
This is what your code should look like:
when clicked
repeat 5
Each time a number gets added to the list, the character should change its costume so the costume’s colour
matches the number. Put these blocks into your code immediately below where a random number is added to
sequence:
wait 1 seconds
https://projects.raspberrypi.org/en/projects/memory/print 7/30
12/22/24, 2:51 PM Memory
Test your project a few times. Do you notice that sometimes the same number is chosen twice (or more) in a row,
which makes the sequence harder to memorise?
Can you make a drum sound play each time the character sprite changes costume? And how about a different drum sound for
each colour?
https://projects.raspberrypi.org/en/projects/memory/print 8/30
12/22/24, 2:51 PM Memory
Add the Music extension to your project so you can use the play drum block.
The Music section then appears at the bottom of the blocks menu.
https://projects.raspberrypi.org/en/projects/memory/print 9/30
12/22/24, 2:51 PM Memory
https://projects.raspberrypi.org/en/projects/memory/print 10/30
12/22/24, 2:51 PM Memory
The code that plays the drum is very similar to the code that changes the character’s costume.
I need a hint
Here is how your finished code should look:
when clicked
repeat 5
wait 1 seconds
https://projects.raspberrypi.org/en/projects/memory/print 11/30
12/22/24, 2:51 PM Memory
Now you’re going to add four buttons the player has to press to repeat the colour sequence.
Add four new sprites to your project to represent the four buttons.
Edit the new sprites’ costumes so that there is one sprite in each of the four colours
Put the sprites in the same order on the stage as the costumes: red, blue, green, yellow
Add code to the red sprite so that, when the sprite is clicked, it broadcasts a ‘red’ message to the character sprite:
broadcast red
A broadcast is like a message announced over a loudspeaker, which you can for example hear in schools or supermarkets. All
of the sprites can hear the broadcast, but only the sprite whose job it is to respond will do something.
Add similar code to the blue, green, and yellow sprites to make them broadcast messages about their own colour.
Do you remember that the broadcast is like a loudspeaker message? You will add code to make it the character sprite’s job to
respond to the broadcast messages.
https://projects.raspberrypi.org/en/projects/memory/print 12/30
12/22/24, 2:51 PM Memory
When your character sprite receives the message red, the code should check whether the number 1 is at the start
of the sequence list (which means that red is the next colour in the sequence).
If 1 is at the start of the list, the code should remove the number from the list, because the player remembered the
correct colour. Otherwise it’s game over, and the code needs to stop all to end the game.
delete 1 of sequence
else
stop all
https://projects.raspberrypi.org/en/projects/memory/print 13/30
12/22/24, 2:51 PM Memory
Add to the code you just wrote so that a drum beat also plays when the character sprite receives the correct
broadcast.
I need a hint
delete 1 of sequence
else
stop all
Duplicate the code you used to make your character sprite respond to the message red. Change the duplicated
code so that it sends the message blue.
When the sprite responds to the message blue, which bit of code should stay the same, and which bit should change?
Remember that each colour has a corresponding number.
https://projects.raspberrypi.org/en/projects/memory/print 14/30
12/22/24, 2:51 PM Memory
Change the character sprite’s code so that the character responds correctly to the blue message.
I need a hint
Here is how your code should look for the blue broadcast.
delete 1 of sequence
else
stop all
Duplicate the code again twice (for the green and yellow buttons), and change the necessary parts so that the
character responds correctly to the new broadcasts.
Remember to test the code! Can you memorise a sequence of five colours? Is the sequence different each time?
When the player repeats the whole colour sequence correctly, the sequence list is empty and the player wins. If you want, you
can also display some flashing lights as a reward once the sequence list is empty.
Add this code to the end of your character’s when flag clicked script:
https://projects.raspberrypi.org/en/projects/memory/print 15/30
12/22/24, 2:51 PM Memory
Switch to the Stage, and import the drum machine sound or another sound you like.
Sounds are organised by category, and you can hover over the icon to hear a sound. Choose a suitable sound.
You should then see that your sprite has your chosen sound.
https://projects.raspberrypi.org/en/projects/memory/print 16/30
12/22/24, 2:51 PM Memory
Add this code to play a sound and make the backdrop change colour when the player wins.
repeat 50
https://projects.raspberrypi.org/en/projects/memory/print 17/30
12/22/24, 2:51 PM Memory
So far, the player only has to remember a sequence of five colours. Improve your game by adding a score, and adding code so
that as the player scores points, the game moves to the next level and the colour sequence to remember becomes longer.
Type in the name of your variable. You can choose whether you would like your variable to be available to all
sprites, or to only this sprite. Press OK.
Once you have created the variable, it will be displayed on the Stage, or you can untick the variable in the
Scripts tab to hide it.
Based on the score, the game will decide on the length of the colour sequence. Start with a score (and a sequence length) of
3.
Add a block at the start of your character’s when flag clicked code to set the score to 3.
https://projects.raspberrypi.org/en/projects/memory/print 18/30
12/22/24, 2:51 PM Memory
Instead of always creating a sequence of five colours, you now want the score to determine the sequence length.
Change the character’s repeat loop (for creating the colour sequence) to repeat score times:
repeat score
If the player repeats the correct sequence, you should add 1 to score, and doing so increases the length of the next
sequence. Add the following block to the character’s code at the point you know the sequence is correct:
change score by 1
I need a hint
You know the sequence is correct at the point when the game broadcasts the ‘win’ message.
https://projects.raspberrypi.org/en/projects/memory/print 19/30
12/22/24, 2:51 PM Memory
Finally, add a forever loop around the code that generates the sequence, so that the game creates a new colour
sequence for each level. This is how your character’s code might look:
when clicked
set score to 3
forever
repeat score
wait 1 seconds
change score by 1
Get your friends to test out your game. Remember to hide the sequence list before they play it!
https://projects.raspberrypi.org/en/projects/memory/print 20/30
12/22/24, 2:51 PM Memory
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.
https://projects.raspberrypi.org/en/projects/memory/print 21/30
12/22/24, 2:51 PM Memory
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.
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.
I need a hint
Here’s how your code for when the red button is pressed should look:
https://projects.raspberrypi.org/en/projects/memory/print 22/30
12/22/24, 2:51 PM Memory
delete 1 of sequence
else
stop all
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?
https://projects.raspberrypi.org/en/projects/memory/print 23/30
12/22/24, 2:51 PM Memory
stop all
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:
stop all
https://projects.raspberrypi.org/en/projects/memory/print 24/30
12/22/24, 2:51 PM Memory
Now remove the code that’s in the else block connected to the red broadcast, and add in the Game over block
instead:
delete 1 of sequence
else
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 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.
https://projects.raspberrypi.org/en/projects/memory/print 25/30
12/22/24, 2:51 PM Memory
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
delete 1 of sequence
else
Game over
https://projects.raspberrypi.org/en/projects/memory/print 26/30
12/22/24, 2:51 PM Memory
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!
stop all
https://projects.raspberrypi.org/en/projects/memory/print 27/30
12/22/24, 2:51 PM Memory
delete 1 of sequence
else
Game Over
delete 1 of sequence
else
Game over
Can you make another custom block that all buttons can use?
Another costume
Can you see that your game starts with your character showing one of the four colours, and that the character always displays
the last colour in the sequence while the player is repeating the colour sequence?
Can you add another plain white costume to your character, and add code so that the character displays this costume at the
start of the game and while the player is repeating the sequence?
https://projects.raspberrypi.org/en/projects/memory/print 28/30
12/22/24, 2:51 PM Memory
Difficulty level
Can you allow your player to choose between playing the game in ‘easy mode’ (using just the red and blue colours) and ‘normal
mode’ (which uses all four colours)?
If you want, you can even add a ‘hard’ mode, which makes use of a fifth drum!
https://projects.raspberrypi.org/en/projects/memory/print 29/30
12/22/24, 2:51 PM Memory
https://projects.raspberrypi.org/en/projects/memory/print 30/30