A jumping game is one of the most satisfying things a class can build in Scratch. A cat runs along the ground, trees come in from the right, and the player presses the space bar to jump over them. Finished, it looks simple. Getting there means working with coordinates, loops and variables, plus a bit of logic that catches most children out the first time. It makes a good follow-up to the collection game in our previous Scratch tutorial.
The video covers making the character jump, adding gravity so it falls back down, sending obstacles across the screen, and keeping score. Around fifteen minutes to build.
Setting up the stage
Start a new project at scratch.mit.edu. Keep the cat this time. It has two costumes, legs open and legs closed, and switching between them is enough to make it look as though it’s running.
Two small jobs first. Rename the sprite from Sprite1 to Cat, because you’ll need to refer to it by name later on. Then rename the project, Jumping Game or similar, and save it. A list of saved projects all called “Untitled” is no help to anyone.
Choose a simple backdrop with a clear ground line and drag the cat down so its feet sit on it. In the video that’s X minus 150, Y minus 110. Add a green flag block with “go to X -150 Y -110” so the cat goes back to the same spot at the start of every game. When you drag the block out, Scratch fills in the current position for you.
If the class has covered coordinates in maths, this bit will click quickly. 0,0 is the centre of the stage. Increase Y and the sprite goes up; decrease it and the sprite goes down. X works the same way, left and right.
Adding gravity
Gravity here is just a loop that keeps lowering the sprite. Put “change Y by minus 6” inside a forever loop and the cat falls. It also keeps falling, straight off the bottom of the screen.
We only want it to fall when it’s in the air. Wrap the movement in an if statement: if Y position is greater than minus 110, change Y by minus 6. The greater than block is in Operators, and the Y position reporter is at the bottom of the Motion blocks. Drag the cat up into the air to test it. It should drop and stop on the ground.
This is a simplification. Real objects speed up as they fall; this cat falls at a steady rate. Some children will spot that, and it’s a nice conversation to have if they do.
Making the cat jump
Add another green flag script with a forever loop. Inside it, an if statement checks whether the space key is pressed, and if it is, repeats “change Y by 10” ten times. Press space and the cat jumps. Gravity brings it back down. Change the numbers to change the height.
Then someone presses space over and over, and the cat keeps jumping in mid-air, higher and higher. Every class finds this. The fix is to jump only when the cat is on the ground, so two things have to be true at once. Take the “and” block from Operators and build: if key space pressed and Y position is less than minus 105. The gap between minus 110 and minus 105 gives a few pixels of leeway, so the jump still works when the cat is resting on the ground.
Stop the class here. The “and” block is only true when both sides are true, and that idea turns up again and again in programming. Time spent on it now is time saved later.
To finish the cat, add a third script: forever, wait 0.25 seconds, next costume. A full second looks sluggish. A quarter of a second looks like running.
Adding the tree
Choose a tree from the sprite library and shrink it to around 30%, sitting on the ground at the right-hand side of the stage. Give it a green flag script that sends it to that starting position.
The movement uses one loop inside another. Inside a forever loop, put “go to” the starting position, then “repeat until touching edge”, and inside that, “change X by minus 10”. The tree slides left until it hits the edge, jumps back to the start, and comes round again. Nested loops are often new to children, so talk through which loop is doing what.
You may find the cat doesn’t clear the tree. Make the tree smaller, make the cat jump higher, or both. In the video, changing the jump to “change Y by 15” did it.
Ending the game and keeping score
The collision check is short. Give the tree another green flag script: wait until touching Cat, then stop all. This uses “wait until” instead of the forever and if pattern from the collection game. Both work. Putting them side by side shows children that there’s rarely only one way to write a program.
For the score, make a variable called Score, set it to 0 when the green flag is clicked, and add “change Score by 1” inside the tree’s forever loop, just after the repeat until block. Each time a tree reaches the edge, the player has survived it, so the score goes up. If you want a Game Over message too, the collection game tutorial shows how to do it with a broadcast.
Where to take it next
Once the basic game works, it’s easy to build on. Add a random wait of between half a second and a second before each tree comes back, so the timing is harder to predict. Swap the fixed minus 10 for a variable set to a random number between minus 8 and minus 3, and each tree travels at its own speed. Add more trees. Each change gives children a real reason to reach for random numbers and variables.
The finished code is on Scratch [link], so children can open it and remix it.
Classroom Application: Three Steps
- Let them find the double jump. Build the simple jump first and let the class discover the bug for themselves. They’ll remember the fix far better than a warning at the start.
- Make the logic visible. When you add the “and” block, write both conditions on the board. Ask children whether each is true in different situations: in the air with space pressed, on the ground with nothing pressed, and so on.
- Use the extensions for differentiation. Early finishers can add random timing or random speeds. Everyone ends up with a working game, and the more confident programmers have somewhere to go next.
The game covers the KS2 computing expectations for sequence, selection and repetition, working with variables, and using logical reasoning to explain how a program works and to find and correct errors.
Make a Jumping Game in Scratch in About 15 Minutes
The Whiteboard Blog – Education, Technology, AI and Science CPD and Support