One-sentence summary
We learn how to use a speed variable, gravity and collision detection so that a character in a game falls, jumps and lands on the ground in a realistic way.
Why it matters
Moving a cat in Scratch is easy: you say "move 10 steps" and the cat glides across the stage. But in real games, objects do not drift at a fixed speed. When a ball is thrown up, it slows down, stops and then speeds up as it falls. When a character jumps, it first rises and then comes back down.
To get this natural look, we keep the object's speed as a separate value and change that speed a little on every frame. The same idea is the foundation of jumping games, platform games and throwing games.
The first time I tried this, I moved the character down at a fixed speed, and the motion looked robotic. When I made the speed grow a little on each frame, the fall suddenly looked real. I share those steps below.
Speed: the engine of motion
What is a speed variable?
Position is where a character is. Speed is how much the character changes its position on each frame. Scratch redraws the stage many times per second; let us call each redraw a "frame." If we add a fixed number to the position on every frame, the object moves smoothly.
To do this, we create a new variable in the Variables category. Let us call it x speed. Then, inside a loop, we change the x position by this variable.
Example: a ball that speeds up to the right
The program below moves the ball to the right, slowly at first and then faster and faster, because x speed grows a little on every frame.
when green flag clicked
set x speed to 0
forever
change x by (x speed)
change x speed by 0.5
Two different things are happening here:
change x by (x speed)(Motion): shifts the ball by its speed.change x speed by 0.5(Variables): makes the speed bigger for the next frame.
If speed stayed the same, the ball would slide at a fixed rate. Because we grow the speed on each frame, the ball accelerates. This is where the physics feeling comes from: we change the speed itself, not just the motion.
Gravity and jumping
Gravity lowers the speed on every frame
Gravity is a force that constantly pulls objects down. In Scratch, the y axis points up: if the y position grows, the object rises; if it shrinks, the object drops. To imitate gravity, we keep a y speed variable and make that speed a little lower on every frame. Then we change the y position by this speed.
The rule is two lines:
change y by (y speed)— move the object at its current speed.change y speed by -0.5— nudge the speed downward.
Changing it by a negative number makes the speed smaller over time; after a while the speed becomes negative and the object starts to fall.
Example: a character falling under gravity
when green flag clicked
set y speed to 0
forever
change y by (y speed)
change y speed by -0.5
If you place the character at the top of the stage and run this program, it falls down, slowly at first and then faster, just like a ball you drop. The magic is in one line: making the speed a little more negative on every frame.
How does a jump work?
A jump is really giving the speed a sudden large upward value. When the space key is pressed, we set y speed to a positive number. Gravity then lowers this speed step by step; the character rises, slows, stops and falls back down.
when space key pressed
set y speed to 8
This block is from the Events category. While the falling program keeps running in the background, this single tap throws the character into the air. Make the 8 bigger and the jump goes higher; make it smaller and the jump is lower.
Collision detection
Touching the ground
The character cannot fall forever; it has to stop somewhere. For this we use the touching? block from the Sensing category. When the character touches the ground (for example a green line sprite), we stop the fall by setting y speed to 0.
when green flag clicked
set y speed to 0
forever
change y by (y speed)
if <touching color (ground)?> then
set y speed to 0
else
change y speed by -0.5
Here the if ... then ... else block (Control) makes the decision: if we are touching the ground, reset the speed and skip gravity; if not, keep applying gravity. This way the character settles on the ground and stays there.
Example: stop when hitting a sprite
The same idea works for obstacles. In a platform game we want the character to stop moving when it hits a wall or a box. Suppose the character has an x speed and is moving right:
forever
change x by (x speed)
if <touching (Wall) sprite?> then
change x by ((x speed) * -1)
set x speed to 0
If the character enters the wall, we first undo the last step (move back by the opposite of x speed) and then set the speed to zero, so the character stops in front of the wall instead of passing through it. Collision detection means asking "are these two objects sharing the same place?" and reacting to the answer.
Mini practice
Build your own jumping character. Try these steps:
- Create a variable called
y speed. - Draw a horizontal line (a ground sprite) at the bottom of the stage.
- Set up the character's fall + land program (like the "stop when hitting a sprite" example above).
- Add the block
when space key pressed, set y speed to 8. - Click the green flag and press space to make the character jump.
Experiments:
- What changes if you use
-1for gravity instead of-0.5? - If you set the jump power to 12 instead of 8, how much higher does the character rise?
- What happens if, when touching the ground, you set the speed to
-3(pushing it back up) instead of 0? You might get a bouncing ball.
Common mistakes
Forgetting to change the position
If you only change y speed and leave out the change y by ... block, the number changes but the character stays still. Speed is just a number; you have to apply it to the position on every frame.
Resetting the speed inside the loop
If you accidentally put set y speed to 0 directly inside the forever loop with no condition, the character never falls, because the speed is reset every frame. The reset should happen only when touching the ground.
Making gravity positive
If you change y speed by a positive number (for example +0.5), the character flies up instead of down. Gravity pulls down, so the value must be negative.
Checking for collisions too late
If you move the character by a very large step and then check for a collision, the character can sink into the wall. Keep the steps small and check after each one.
Safety note
This lesson runs entirely on the screen, inside Scratch, and involves no physical hazard. Even so, try not to sit at the screen for too long: take a short break every 30–40 minutes, rest your eyes and move around a little. Before sharing your Scratch project, review it with an adult; do not write personal details (address, school, phone) in the comments.
Lesson summary
- Speed is a variable that holds how much an object changes its position on each frame.
- Gravity is imitated by lowering the y speed a little each frame and adding that speed to the position.
- A jump is giving the y speed a sudden large upward value.
- Collision detection uses the
touching?block from the Sensing category and stops motion by resetting the speed. - Small steps and correctly ordered checks stop objects from sinking into walls.
Check questions
- What does a "frame" mean in Scratch, and why is speed applied on every frame?
- To imitate gravity, should you increase or decrease
y speedeach frame? Why? - To make a jump, what do we do to which main variable?
- Which block stops the character from falling forever, and which category is it in?
- If a character passes through a wall, what are two possible reasons?
Answers
- A frame is one redraw of the stage by Scratch; the stage refreshes many times per second. Because we apply speed on every frame, motion looks smooth and we can create speeding up or slowing down over time.
- Decrease it. Because the y axis points up, changing the speed in the negative direction pulls the object down, which is the natural effect of gravity.
- We set the
y speedvariable to a positive number (for exampleset y speed to 8). Gravity then lowers this speed slowly, and the character rises and comes back down. - The
touching?block in the Sensing category. When touching the ground, we sety speedto 0 to stop the fall. - (a) The step is too large, so the character enters the wall in a single frame; (b) collision is not being checked, or after a hit the speed is not reset and reversed.
Source and verification note
For “Simple Physics: Speed, Gravity and Collision”, verification focuses on whether the relationship between Speed: the engine of motion and Example: a ball that speeds up to the right remains consistent across examples. Block names are kept consistent with the current core Scratch categories. Project behaviour should be tested separately for start-up, normal play, errors and restarting.
Next lesson
Score and Levels in Game Design: We turn your jumping character into a real game by adding score, lives and levels that get harder.