Building a Game in Scratch
Using Scratch 3, you will create an interactive game where you need to help Marty catch their favourite snacks that are falling from the sky to earn points. There are also some snacks falling from the sky that Marty doesn't like so we need to avoid them!
After completing this activity, you could then add your own twists to the game to make it harder or more challenging for players.
What you will need
- A device that works with Scratch 3
What will you learn about?
- Coordinates on the Scratch stage
- How to move characters around the screen
- Using sensing blocks to work out what characters on the stage are touching
- Coding variables to keep track of a score
- Logical if statements to help us make decisions during the game
Extra information for educators
This activity introduces some key concepts of programming that can also be found in our Lesson Packs for Marty the Robot.
setting the scene
Before we start coding our game, we need to make sure that we set the scene with a Marty sprite on the stage.
Click the button below to open a brand new Scratch project, ready for coding.
You will notice that on the stage, there is currently a cat. We want our main character to be Marty and not Scratch cat so we need to remove them from the stage. To do this, click on the delete button as shown below,
Next, we want to add a new character (or sprite as it is called in Scratch). But we don't want to use one of the options that Scratch have already built in. We want to upload our own character image to be used. Download and save the image using the button below,
Great! Now we are ready to upload our image of Marty into our Scratch project as the main character.
To do this, we need to hover over the cat face icon and select the option to Upload Sprite as shown below. Then all that is left to do is select the Marty image wherever it is stored in your files.
You might notice that the Marty on the Scratch stage is a little big. Let's change that!
All you have to do is change the number in the Size textbox. Try playing around with this number until you find a size that you think is sensible. We set ours to 30.
Hopefully you now have something that looks like this...
moving left & right to catch the snacks
If we want Marty to be able to move side to side to catch the falling food, then we will need to code some movements.
When we click the left arrow key on our keyboard we want Marty to move left and when we click on the right arrow key on our keyboard we want Marty to move right.
Let's start with clicking on the right arrow key,
When you click on the right arrow key, Marty should now move along the screen to the right. We just need to add in some instructions to move left. But this time because we want to move in the opposite direction we will need to use the '-' sign so our character knows to move the other way.
Hint: You can think of these numbers like a number line. As you go along the number line towards the positive numbers you go to the right and if you go towards the negative numbers you move along the left!
Challenge: coding marty to move faster or slower
We now have Marty moving from side to side, ready to catch some food. But how could we change our code to make Marty move faster or slower?
I need some help!
All we need to do to change the speed that Marty moves across our screen is change the number in our blue move code block.
Try changing the number 10 to a bigger number. What happens? Hopefully, you will notice that Marty moves across the screen in bigger steps so moves faster.
Now try and change that number to something less than 10. Marty is now taking smaller steps so is moving slower than before.
Play around with this number and decide what speed you want Marty to move!
adding in some snacks to catch
Our game is starting to take shape, the next step is to add in some snacks for Marty to try and catch!
The first snack we are going to add into our game is something that Marty likes. For this, we are going to add in some bananas for Marty to catch but you can pick what you think Marty's favourite snack would be!
To add in our bananas, we just need to add in a new sprite using the Choose a Sprite button,
Don't forget to re-size the new sprite that you add in! You should have something like below,
The bananas are going to randomly drop from different parts of the sky on our stage. To do this we need to think about coordinates and where the bananas will be on the screen. Take a look at this block,
This block will tell a sprite to go to different parts of the stage based on the coordinates that we give it. The very middle of the stage will have the coordinates 0 for x and 0 for y.
Try moving your sprite around on the stage and see how their x and y coordinates change!
After exploring how these coordinates work, we know roughly where the top of the stage is but how do we make sure the snack isn't dropped from the middle of the screen all the time?
We can use a block from the Operators section. This green pick random block will pick a random number between the numbers that we tell it. In here, we have included the most left position and most right position that we could drop the snack from. That means that the snack will randomly fall from different parts of the top of the screen.
You can use these numbers in your program too!
Lets put that together with a few other instruction blocks,
We want to hide the snack so that the player can't see where it goes until it is ready to start falling from the sky. We have also added in a short pause so that the snack doesn't always fall at the same time.
the sky is falling!
We have Marty's favourite snack appearing at random parts along the top of the screen so now we need to add in some coding blocks to see some food falling from the sky.
To code the food to start falling down the screen, we need to slowly decrease the y coordinates of the sprite. Using a loop, we want to keep doing this until the snack is either caught by Marty or reaches the bottom of the screen,
What happens if you change the number in the change y by block?
By putting together all of those instructions we get something that looks like,
Don't forget to add in a forever loop so that the food will keep falling throughout the game and not just once!
What happens when marty catches the snack?
You might notice when you tested your game that when Marty catches the bananas, they disappear and go back to the top of the screen but it would be nice if there was some kind of noise that let us know we caught them!!
Before we play the sound, we need to double check that Marty has in fact caught the snack falling down the screen. We can use an if statement again to help us do this and if the two sprites are touching then we can play a fun sound to let the player know they have successfully caught that falling snack!
What happens if you change the number in the change y by block?
Here's what the instructions for our banana sprite look like now,
Make sure the new instructions to play a sound are inside of the forever loop
adding a second falling snack
We now have one falling snack for Marty to catch but wouldn't it be fun to have another one? This time, lets add in a snack that Marty doesn't like so needs to try and avoid!
The first step is to add a new sprite to the stage like we did for the bananas. This time you need to pick something that Marty does not like. We decided to add a donut as the snack that Marty does not like!
Don't forget to re-size your new sprite!
Then the code for this new snack will be exactly that same as the code we put together for the bananas. Here's a reminder of what that looked like,
challenge: can you add in your own sound recording?
It would be pretty cool if we could change the sound that was played when we catch the food Marty doesn't like to be an annoying noise to let the player know they caught the wrong snack!
I need some help!
It is really easy to add in our own sounds to a Scratch project. You can even record your own voice and play it back in your game!
To do this, all you need to do is click on the arrow beside the sound block that we have in our program and click on the option to record... to make your own sound to include,
let's keep score!
We now have an awesome game that lets the player help Marty catch their favourite snack and avoid the others falling from the sky! The final step is to keep a score.
To keep track of our score, we will need to use something called a variable. A variable is used in programming to remember something for us, in our case, we ill use it to store and remember the score in our game. Let's start off by creating a brand new variable called score,
We then need to set the score to start off at 0. Let's add this code into the code blocks that we have for Marty,
Here is a reminder of what our final blocks of code look like for the Marty sprite,
Next, we need to add in an instruction to add 1 to our score each time we catch some bananas. We just need one block of code to do this meaning that our final list of code blocks for the banana sprite looks like this,
And finally for our donut sprite, we need to do the same except this time we want to deduct one point from the score each time the player catches a donut. This is what our final code blocks look like for the donut sprite,
Yay! You have just built your very own game in Scratch with Marty the Robot as the main character. In this activity you have exploring how to use coordinates, logic and sensor blocks to help us make decisions and move different characters around the screen.
Have a think about what you like the most about your game and what you might like to change. Maybe you want to make the game easier or more challenging? Here are some ideas on what you could try next...
- Make the game more challenging. Can you add more food sprites that fall from the sky to make it even harder to catch just the bananas?
- Customise the game. Can you change the background of the game? Maybe you can design a kitchen background and upload it to Scratch!
- Could there be more levels to your game? Try adding in new levels where Marty has to do different tasks to earn points!