Making a Game in Scratch
A step-by-step primary lesson on making your first game in Scratch: move a sprite, catch falling objects, keep score with variables, and add a win message. With quiz.
Key takeaways
- A simple game needs a player sprite, something to catch or avoid, and a goal
- You move a sprite by changing its x and y position inside a forever loop
- A variable like 'score' keeps track of points as the game is played
- An 'if touching' block lets the game react when two sprites collide
What makes a game a game?
Almost every simple game has three things:
- A player you control (here, a basket you move left and right).
- Something to catch or avoid (here, apples falling from the sky).
- A goal, like getting a high score.
In this lesson we will build a "Catch the Apples" game in Scratch. Scratch is a free coding tool where you snap colored blocks together to make characters move. If you have never used it, start with getting started with Scratch first, then come back here.
Step 1: Set up the sprites
In Scratch, a character is called a sprite. We need two sprites:
- A Basket (the player). You can pick the "Bowl" sprite from the sprite library, or draw your own.
- An Apple (the thing to catch). Pick the "Apple" sprite from the library.
Click the basket on the stage. We want it to start at the bottom. Add this script:
when green flag clicked
go to x: 0 y: -140
The numbers x and y are positions on the stage. We will learn more about them in coordinates and movement in Scratch. For now, just know that y: -140 puts the basket near the bottom.
Step 2: Move the basket with arrow keys
We want the player to move the basket left and right with the arrow keys. We use a forever loop so the game keeps checking the keyboard:
when green flag clicked
go to x: 0 y: -140
forever
if key [right arrow] pressed? then
change x by 10
if key [left arrow] pressed? then
change x by -10
The forever block repeats everything inside it without stopping. Each time around, it checks: is the right arrow down? Move right. Is the left arrow down? Move left. Now click the green flag and try moving the basket!
Step 3: Make the apple fall
Click the Apple sprite. We want it to start at the top in a random spot, then fall down. In Scratch, a smaller y means lower on the stage, so to fall we make y go down:
when green flag clicked
forever
go to x: (pick random -200 to 200) y: 160
repeat until <touching [Basket]? or (y position) < -150>
change y by -5
Here is what happens:
- The apple jumps to the top (
y: 160) at a random x, so it does not always fall in the same place. - The
repeat untilloop moves the apple down 5 steps at a time. - It keeps falling until it touches the basket, or drops below the bottom of the screen.
- Then the forever loop sends it back to the top to fall again.
Step 4: Keep score with a variable
A variable is a box that stores a value you can change. We will make one called score.
Click "Variables," then "Make a Variable," and name it score. A little counter appears on the stage.
At the start of the game, set the score to zero. Add this to the basket's script, right after the green flag:
set [score] to 0
Now, on the Apple sprite, we want the score to go up when the apple is caught. Change the apple's loop so it checks for a catch:
when green flag clicked
forever
go to x: (pick random -200 to 200) y: 160
repeat until <touching [Basket]? or (y position) < -150>
change y by -5
if touching [Basket]? then
change [score] by 1
The if touching [Basket]? block checks whether the apple hit the basket. If it did, change [score] by 1 adds one point. To learn more about variables, see variables explained.
Step 5: Add a win message
Let's say catching 10 apples wins the game. Add this to the apple's loop, after the score changes:
if (score) = 10 then
say [You win!] for 2 seconds
stop [all]
When the score reaches 10, the sprite shows "You win!" and stop [all] ends the game. You now have a complete, playable game!
Try it: make it your own
Your game works, but the best part of coding is changing it. Try one of these challenges:
- Make it harder: change
change y by -5tochange y by -8so apples fall faster. - Add a sound: play a "pop" sound when the apple is caught, using a Sound block.
- Add a bad item: add a "bomb" sprite. If the basket touches it, change the score by -5!
- Add a timer: use a
timerand end the game after 30 seconds instead of at 10 points.
Each change teaches you something new. Computers do exactly what your blocks say, so if something acts strange, read your blocks in order and fix them — that is called debugging, and every coder does it. Have fun building! 🎮
Quick quiz
Test yourself and earn XP
What does a 'forever' loop do in a Scratch game?
A forever loop repeats its blocks again and again, which is perfect for checking the keyboard and movement during a game.
Why do we use a variable called 'score'?
A variable stores a value that can change. 'score' remembers the points so the game can show and update them.
Which block lets the game know two sprites have collided?
The 'touching' block checks whether one sprite is touching another, so the game can react.
To make the apple fall down the screen, you should...
In Scratch, smaller y means lower on the stage, so changing y by a negative number moves a sprite down.
When the apple reaches the bottom, a good next step is to...
Sending the apple back to the top lets it fall again, so the game keeps going.
FAQ
No. You can use Scratch free in a web browser at the Scratch website. You only need an account if you want to save and share your project.
Not at all. By snapping together a handful of blocks you can build a real, playable catch game. This lesson walks you through it one step at a time.
Keep exploring
More in Coding