Today, we're going to learn how to make a jumping game in Scratch. There are lots of popular games like this already, and one thing they all have in common is: you just can't stop playing them!
Perhaps it's the prospect of beating your old high score, or simply the thrill of reaching new heights. Whatever your reason, we'll have some fun building this game, learn a lot of cool strategies, and most importantly, by the end of this tutorial, you'll have an addictive game you can challenge your friends and family with!
Here's how the game will work. When we start, platforms will begin falling from the sky. Our character will start falling too, and the only way to stay alive will be to keep jumping from one platform to another. The longer we last, the higher our score!
For even more Scratch coding fun, be sure to check out our live small group intro Scratch class, and our Game Building coding club full of creative weekly challenges.
You might also enjoy building your Pokémon games and customizing them in all sorts of creative ways, in our live online Pokémon Game Building Class:
How to make a jumping game in Scratch for beginners
For this jumping game tutorial, we'll be using the X and Y locations in Scratch, Clones, and Variables not only for our score, but also to make the game harder as it goes on. Here's a link to the Finished Project.
To get this game setup, let's choose a backdrop, choose one sprite for your Platforms, and choose one sprite to jump. We're going to use the Paddle sprite for our platforms, and for our jumping sprite, we'll choose the Hedgehog, because we think hedgehogs are pretty cool. Let's get started!
1. Create Multiple Costumes for Our Platforms
This game will be more fun if we have different sized platforms. So let's go to the costumes tab and make one really short, one really long, and one just normal. Duplicate your sprite's costume twice, and with the select tool, drag one costume out to be long, and drag the other costume in to be short.
2. Clone your Platform
Let's create clones of our platform so we can have more than one on the screen at once. When we clone a sprite, we make a copy of it, and we can give that copy (the clone) a job to do. Let's use a 'forever' loop and a 'wait' block to clone our platform every 3 seconds. We're going to hide it too, so the only thing we see is the clones, not the original sprite.
3. Give our Clones a Job
Now we have our clones, but they don't know what to do with themselves! We can use the "When I start as a clone" block to give them a job. Let's tell those clones that they should move from the top of the screen to the bottom of the screen, and then delete themselves.
First things first, they need to pick one of the three costumes in our sprite, and also 'show' up since the original sprite is hidden. Then we'll tell them exactly where they should start: anywhere from the left edge (X: -210) to the right edge (X: 210) and at the top (Y: 170). Then, we'll tell them to change their Y position by a negative number until they're at the bottom of the screen (Y < -170). After that, their job is done, and they can delete themselves. Thanks, clones.
Ok! This is a good time to test our code. When we click the green flag, our platforms should be falling down the screen. Now onto the jumping sprite. The jumping sprite should do three things: move left and right, fall down, and most importantly, jump.
4. Move Left and Right
Let's move left and right when we use those arrow keys on the keyboard. We can check to see if a key is pressed by using a 'key _ pressed' Sensing block with an 'if-then' block. Let's tell our sprite to point right (direction 90) and move ten steps if we hit the right arrow key, and point left (direction -90) and move ten steps if we hit the left arrow key. Remember to put it all in a forever loop so we're always checking to see if those keys are pressed! Oh, and you can set the rotation style of your sprite to 'left-right' if you don't want it to turn upside down.
5. Make it Jump!
We'll jump when we press the Up key. Of course, everyone knows hedgehogs can't fly, and let's assume your sprite can't either. Before we jump, let's check to see if we're on a platform first, that way we have something to jump off of. If we are on a platform, then we can check to see if the Up arrow key is pressed. We'll make our sprite 'jump' by increasing its Y position a tiny amount inside of a repeat block.
6. Make it Fall
Time to add some gravity. We're already checking to see if our sprite is on a Platform with the code, 'if touching platform.' If we replace our 'if-then' block with an 'if-then-else' block, we can do something when it's not touching the platform too! If it's touching the platform we can jump, if it's not touching a paddle though (else) let's fall by changing Y by -10. Let's make sure, when we click the green flag, that our sprite starts at the top of the screen, and give the player a couple seconds to get ready.
7. Game Over Condition
Our sprite may fall now, but the game never actually ends. Let's check to see if our sprite is at the bottom of the screen (Y < -180) and stop the game if it is. Bug alert! Sometimes our game ends instantly when we try to restart it. That might happen if we click the green flag when our sprite is already at the bottom, even though we told it to go to the top elsewhere in the code. Let's add a small wait block to prevent this bug and give our sprite time to get out of the danger zone.
8. Add a Score
What's the point of playing a game if you can't beat your old high score, or better yet, your friend's high score? Let's create a 'Score' variable, and the longer you stay alive, the higher your score. We can increase it by one every second. Not sure which sprite to put this code under? For code like this, that doesn't really relate to any one sprite, we like to use the backdrop. Yes, you can write code for your backdrop!
Alright, we suppose we've got ourselves a pretty fun game here already. Are you thinking what we're thinking though? Is this game just a little too easy? Let's turn up the difficulty a bit...
9. Make our Platforms Fall Faster
Let's go back to our platform code, and make a variable called 'Fall Speed'. This variable will control how fast the Platforms fall down the screen, so instead of telling our clones to change Y by -1, we can tell them to change Y by Fall Speed now. Let's also write some new code to set Fall Speed to -1 when we click the green flag, and decrease it just a little bit every few seconds.
10. Clone our Platforms Quicker
If our platforms are falling faster now, we should clone them faster, too. Create another variable called 'Clone Speed', and set it to 3 when we click the green flag. Instead of waiting 3 seconds between each clone, we can wait 'Clone Speed' seconds now, and every time we change 'Fall Speed', let's change clone speed as well. We have to make sure not to make Clone Speed too small though, or else we'll have way too many clones at once, and our game might crash!
Ok, now we're really jumping! There are just a couple things still missing though. Sounds and Animations can take any good game and turn it into a great game! Or vice versa, make an otherwise great game just a little bit dull.
11. Add Sound
Play a sound every time your sprite jumps, and when you get a Game Over. We can add some background music in our backdrop code too, by putting a 'play sound _ until done' inside a 'forever' loop. If you want to be extra fancy, use an 'if-then-else' block to change which music you play as the score goes up, and the game gets a little faster.
12. Add Animation
Every time we jump, let's have our sprite cycle through a few different costumes. We can use a 'broadcast' block for this, so we don't interrupt the code we already have. Broadcasting sends a message, and we can have our sprite (or any sprite!) receive that message and activate some code. Whenever we jump, Broadcast a new message called 'Jump Animation,' and have your sprite cycle through some costumes when it receives this message. The Hedgehog comes with some great costumes for jumping already, but if your sprite doesn't, drawing them yourself can be really fun too!
Wow! Looking (and sounding) a lot better now. You may have noticed a small bug though. When we get a 'Game Over' our score still goes up for a few seconds, the platforms keep falling, and the background music and the 'lose' music play at the same time. We can polish this game up just a little more.
13. Stop Scripts in Other Sprites
When we get a game over, let's tell our other sprites to stop what they're doing while the 'lose' music plays. We can do this with Broadcasting, and the 'stop other scripts in this sprite block.' You can think of 'scripts' as just a fancy word for 'code blocks.' Let's have our Platform and our Backdrop stop what they're doing when they receive a "Game Over" message.
14. Create a Game Over Sprite
As a final 'cherry on top, let's create our own 'Game Over' sprite, so the player knows that they've indeed lost. This is a great time to show them their score as well. We can create this sprite by selecting 'paint' in the choose a sprite menu, and using the 'text' tool. Let's tell our new sprite to hide itself and the score when the green flag is clicked, and show itself and the score when it receives the 'Game Over' message.
Ideas to make a run and jump game in Scratch your own
Alright, we'd say that's one fine game we've built here. But we bet you can make it even better! Let's think about some ways you can take this great game and make it truly legendary.
1. Add More Backdrops
An appealing aspect of jumping games that we didn't include in ours is the prospect of discovering new zones. If you've ever played the popular jumping game, 'Jelly Jump,' you might recall that that background isn't always the same color. Perhaps you can incorporate that concept into this game as well. Maybe you choose a new backdrop every time the game starts, or, better yet, change the backdrop as the score goes up, and the player makes it farther than they did before!
Jumping is fun and all, but what if we could fly, or maybe make all of our platforms long ones, if not but for a fleeting moment? Power-Ups can add a lot of fun to any game and even create new strategies for the most competitive of players. We remember playing the game Doodle Jump in school with our friends (not during class, of course...). Our friends were concerned with simply staying on the platforms, but we always knew, as we're sure you do as well, that the true secret to success was aiming for those Power-Ups! What sort of Power-Up sprites could you add to this game?
3. Vertical Jumping? How about Horizontal?
We've made a game in which platforms fall and we must stop from hitting the bottom of the screen. That's cool. But another cool version of a jumping game has obstacles coming at you horizontally, and the goal is to avoid them hitting you! If you've ever played something like Temple Run for example, you know what we mean. How could we modify this game to flip is on its side, and make the goal avoiding platforms, instead of aiming for them?
Make a jumping game in Scratch
Today we built a simple, yet addictive jumping game in Scratch. We utilized X and Y locations of sprites to get everything moving (and jumping!). We created Clones, and had plenty of 'if-then' blocks to make our game work correctly. And we even used Variables to transform this game from a casual Sunday stroll into a white-knuckled Friday fracas. And to top it all off, we learned some great strategies that you can use to turn any of your Scratch projects from good to great to legendary!
If you enjoyed this tutorial, we've got plenty of other awesome games to build in our Create & Learn blog. And if you fancy yourself a programmer (which, if you made it this far, let me tell you, you definitely are!), you'll have a blast going through our Scratch Ninja curriculum, where you'll not only hone the programming skills you already have, but learn more great strategies to make any game you can think of. Alright kids, great job today, and see you in the next one!
Written by Ian Kuzmik, a Create & Learn instructor with a Bachelor's Degree in English from Tulane University. He's been teaching grades K-8 since 2019, with a focus in the subjects of ESL and Computer Science.