Designing a Simple Game with Scratch
Scratch is a programming language where the code is colored blocks that snap together like puzzle pieces. You drag them instead of typing them, so you cannot make a spelling mistake — which means the only thing left to get wrong is the thinking, and that is the part worth practicing.
It was made at MIT, it is free, and it runs in a web browser.
The Game You Are Building
Apples fall from the top of the screen. You move left and right with the arrow keys and catch them. Every catch scores a point.
That description is already the whole design. Two characters, two behaviors, one score.
- A computer with a web browser
- Scratch, which runs on the Scratch website — an account is only needed if you want to save your work
Build It
- Start a new project. You will see a cat on the stage. Click the trash can to delete it — you are choosing your own characters.
- Add two sprites. Use the "Choose a Sprite" button in the bottom right. Pick a character to play as, then add an apple.
- Make the apple fall. Click the apple. From Events drag out
when green flag clicked. Under it put aforeverblock from Control, and inside that putchange y by -5andwait 0.1 seconds. - Send the apple back to the top. Still inside the
foreverblock, addif y position < -170 then, and inside that putgo to x: (pick random -200 to 200) y: 150. Now the apple reappears at the top in a new place every time it falls off the bottom. - Move the player. Click your character. Add
when green flag clicked, then aforeverblock containingif key right arrow pressed? then change x by 10andif key left arrow pressed? then change x by -10. - Add the score. In Variables click "Make a Variable" and call it
Score. On your character, afterwhen green flag clicked, addset Score to 0. Then inside itsforeverblock addif touching apple? then change Score by 1andgo to x: (pick random -200 to 200) y: 150for the apple.
Press the green flag. If apples fall and your character slides sideways, you have a game.
Why forever Is Everywhere
Almost every block above sits inside a forever loop, and that is not an
accident. A game is not a list of instructions that runs once — it is a small
set of checks repeated many times a second. Has a key been pressed? Has the
apple hit the bottom? Are the two sprites touching? Ask those questions fast
enough and it looks like a world.
Why does the code for moving your character sit inside a forever block?
- So the character moves faster
- So the game keeps checking the arrow keys again and again while you play
- Because Scratch requires every project to have one
Make It Yours
- Change the backdrop. Click "Stage" in the bottom right, then "Backdrops".
- Add a second apple. Right-click the apple sprite and duplicate it, then
change the
waitto a different number so it falls at a different speed. - Add a way to lose. Give the apple a check: if it reaches the bottom
without being caught,
change Score by -1.
That last one turns a toy into a game. Until something can go wrong, catching apples is not really a challenge.