Making a Platformer in Scratch
A step-by-step middle-school lesson on building a platformer in Scratch: left and right movement, gravity, jumping with a velocity variable, landing on platforms with touching colour, and a goal. With a worked project and quiz.
Key takeaways
- A platformer needs left/right movement, gravity that pulls the player down, and jumping
- Gravity is made by steadily changing a 'y velocity' variable each frame
- Landing works by detecting platforms with 'touching colour' and stopping the fall
- A jump sets the y velocity to a positive value so the player rises, then gravity pulls it back
What is a platformer?
A platformer is one of the most famous kinds of game โ think of a character running and jumping across floating platforms, avoiding gaps, and reaching a flag at the end. Building one in Scratch teaches you a really important idea in game programming: gravity. Instead of moving the player up and down directly, you give it a velocity and let a steady pull do the rest, just like in the real world.
This lesson is a bigger project than most, so take it one step at a time. By the end you will have a working platformer with left/right movement, falling, jumping and landing. If Scratch is new to you, start with getting started with Scratch. Because the player's height changes constantly, it helps to understand the stage grid first โ see coordinates and movement in Scratch.
The big idea: position and velocity
Most beginners move a sprite by saying "change y by 5" directly. A platformer is different. We keep a variable called y velocity โ the player's vertical speed โ and every frame we do two things:
- Apply the velocity:
change y by (y velocity). If velocity is positive, the player rises; if negative, it falls. - Apply gravity:
change [y velocity] by -1. Each frame, gravity makes the velocity a little more negative, so the player falls faster and faster โ exactly how dropping feels.
A jump simply sets y velocity to a positive number (say 12). The player shoots up, gravity steadily reduces the velocity to zero at the top, then makes it negative so the player falls back. This one idea is what makes jumps feel natural. To manage the velocity we need variables; if those are new, see variables and score in Scratch.
Step 1: Draw the level
We will paint the platforms onto the stage backdrop, all in one colour so a single check can detect them.
- Click the Stage thumbnail, open the Backdrops tab, and start a blank backdrop.
- Pick the rectangle tool and one colour โ let's say dark green. Draw a solid floor along the bottom and a few floating platforms above it.
- With a different colour, draw a small goal flag in a hard-to-reach corner โ for example, gold.
Keep your first level simple, with platforms close enough to jump between. You can make it trickier later.
Step 2: Set up the player and variables
Add a small player sprite (the Beetle or a drawn square works well). Make two variables, both for all sprites is fine here:
y velocityโ the player's vertical speed.
Now give the player a starting script:
when green flag clicked
go to x: -200 y: 100
set [y velocity] to 0
This drops the player in from the top-left so we can watch gravity pull it down to the floor.
Step 3: Left and right movement
Horizontal movement is the easy part. Add a forever loop that reacts to the arrow keys:
forever
if <key [right arrow] pressed?> then
change x by 5
if <touching color [dark green]?> then
change x by -5
if <key [left arrow] pressed?> then
change x by -5
if <touching color [dark green]?> then
change x by 5
This is the "move, then undo if blocked" trick. Each branch nudges the player sideways, then checks touching color [dark green]?. If the player is now stuck inside a platform wall, it moves straight back. That keeps the player from sliding through the sides of platforms.
Step 4: Add gravity and falling
Now the heart of the platformer. Inside the same forever loop, add the gravity logic:
change [y velocity] by -1
change y by (y velocity)
if <touching color [dark green]?> then
repeat until <not <touching color [dark green]?>>
change y by 1
set [y velocity] to 0
Read this carefully โ it is the most important part:
- change [y velocity] by -1 โ gravity tugs the velocity downward each frame.
- change y by (y velocity) โ the player actually moves by its current vertical speed. Falling speeds up because velocity keeps getting more negative.
- if touching color [dark green]? โ the player has landed (or sunk into) a platform.
- The repeat until not touching loop nudges the player up 1 step at a time until it sits on top of the platform rather than inside it.
- set [y velocity] to 0 stops the fall, so the player rests on the platform.
Click the green flag now. The player should drop from the sky and land neatly on the floor. That is gravity, built from scratch!
Step 5: Jumping
A jump only works when the player is on the ground โ otherwise you could jump in mid-air forever. We check for ground by testing one step below the player. Add this inside the forever loop:
if <key [up arrow] pressed?> then
change y by -2
if <touching color [dark green]?> then
set [y velocity] to 12
change y by 2
Here is the clever bit:
- change y by -2 peeks just below the player's feet.
- if touching color [dark green]? โ is there ground right below us? If yes, we are standing on a platform.
- set [y velocity] to 12 launches the player upward. Gravity from Step 4 will slow the rise and bring it back down.
- change y by 2 moves the player back to where it was after the peek.
Press the up arrow and watch the player leap, arc through the air, and land. The jump feels springy because gravity is constantly working on the velocity.
Step 6: Win at the goal
Finally, react when the player reaches the gold goal. Add this at the bottom of the forever loop:
if <touching color [gold]?> then
say [You reached the goal!] for 2 seconds
stop [all]
When the player touches gold, it shows a win message and stop [all] ends the game. Your platformer is complete!
The full player script
Putting Steps 2 to 6 together, the player has one setup script and one big forever loop:
when green flag clicked
go to x: -200 y: 100
set [y velocity] to 0
forever
if <key [right arrow] pressed?> then
change x by 5
if <touching color [dark green]?> then change x by -5
if <key [left arrow] pressed?> then
change x by -5
if <touching color [dark green]?> then change x by 5
change [y velocity] by -1
change y by (y velocity)
if <touching color [dark green]?> then
repeat until <not <touching color [dark green]?>>
change y by 1
set [y velocity] to 0
if <key [up arrow] pressed?> then
change y by -2
if <touching color [dark green]?> then set [y velocity] to 12
change y by 2
if <touching color [gold]?> then
say [You reached the goal!] for 2 seconds
stop [all]
Common mistakes and how to fix them
- Player falls through platforms. The platform colours are not identical, or the
touching coloris set wrong. Use the dropper to grab the exact platform colour. - Jump never works. The ground check is failing. Make sure your "peek below" uses the same platform colour, and that the player is actually resting on a platform when you press up.
- Player jitters or sinks slowly. Your gravity (
-1) or jump (12) numbers may need tuning. Small changes have a big effect โ experiment.
Reading your script in order to find these problems is called debugging, and every coder does it.
Try it: make it your own
You have a working platformer. Now extend it:
- Add a moving platform: make a separate sprite that slides back and forth, and stand on it.
- Add danger: draw red spikes; if the player touches red, send it back to the start.
- Add coins: scatter coin sprites, and use a
scorevariable to count the ones you collect. - Multiple levels: when the player reaches the goal,
switch backdrop toa harder level instead of stopping. - Tune the feel: change the gravity (
-1), jump strength (12), and walk speed (5) to make the game floaty or snappy.
Gravity and velocity are the foundation of countless games. Once they click, you can build all sorts of action games in Scratch. Have fun jumping! ๐ฎ
Quick quiz
Test yourself and earn XP
What is the role of the 'y velocity' variable in a platformer?
'y velocity' holds the player's vertical speed. Gravity lowers it each frame, a jump raises it, and the player's y position changes by it.
How do you make gravity in Scratch?
Each loop, you reduce 'y velocity' a little (pulling the player down faster over time) and then move y by that velocity, which is exactly how gravity feels.
Why are platforms usually drawn in one colour?
If every platform is the same colour, one 'touching colour' check detects them all, letting the player land on any of them.
When the player is touching a platform, what should happen to the falling?
Landing means the fall stops. You nudge the player up until it is no longer inside the platform and set 'y velocity' to 0 so it rests on top.
What does a jump do to 'y velocity'?
A jump sets 'y velocity' to a positive number, so the player moves up. Gravity then steadily lowers it, slowing the rise and bringing the player back down.
FAQ
The most common causes are that the platforms are not all exactly the same colour, or the 'touching colour' block is set to a different colour than the platforms. Use the dropper tool in the colour picker to grab the exact platform colour, and make sure every platform uses that one colour.
After detecting a touch, repeatedly nudge the player up by 1 (using a 'repeat until not touching' loop) so it ends up resting on top of the platform rather than buried inside it, then set 'y velocity' to 0.
Keep exploring
More in Coding