Platform Game Programming with AI
Senior 3 · ICT
Platform Game Programming with AI
Lesson 2 of 4
60 min
In S2 your sprites moved by a fixed number of steps when a key was pressed. Try that in a platform game and the character floats: it goes up while the key is held and hangs in the air when it is released. Real objects fall faster and faster, land, and jump in a curve. This lesson gives your player gravity, a jump, and collisions with platforms and walls, using one variable and about twenty blocks.
Definition
Gravity (in a game): a rule that pulls a sprite down by changing its vertical speed a little every frame.
Velocity: speed in a particular direction. Here the variable y speed holds the vertical velocity: positive means moving up, negative means falling.
Collision detection: checking whether two things on the screen overlap, such as the player and a platform. In Scratch: touching [sprite]? or touching color [ ]?
Hitbox: the shape used for collision checks. A simple, plain costume makes a reliable hitbox.
The stage is 480 pixels wide and 360 high: x runs from −240 (left) to 240 (right), y from −180 (bottom) to 180 (top).
Build this script on the Player sprite. The comments on the right are notes for you, not blocks.
Script drawn by Ganzaa in Scratch 3 block colours: yellow Events, blue Motion, orange Control, dark orange Variables.
| Part | Blocks | What happens each frame |
|---|---|---|
| ① Gravity | change [y speed] by (−1) | The vertical speed drops by 1 every frame, so a falling player gets faster and faster. |
| ② Move | change y by (y speed) | The player moves up or down by its current speed. |
| ③ Land | if touching [Level] → change y back, set y speed to 0 | If the move put the player inside a platform, it steps back out and stops. The player now rests on the platform. |
| ④ Jump | if key up arrow pressed → set y speed to 12 | Only checked when the player has just touched a platform, so it can jump from the ground but not in mid-air. |
| ⑤⑥ Walk | change x by (±4), then undo it if touching the Level | The player walks, but a wall stops it instead of letting it walk into the rock. |
Worked example 1: how high is a jump?
The jump sets y speed to 12. On the next frames, gravity first lowers the speed, then the player moves by it:
| Frame | 1 | 2 | 3 | … | 11 | 12 | 13 |
|---|---|---|---|---|---|---|---|
| y speed | 11 | 10 | 9 | … | 1 | 0 | −1 |
| Height gained | 11 | 21 | 30 | … | 66 | 66 | 65 |
The player rises pixels, reaching the top after about 12 frames, s at 30 frames per second, then falls back along the same curve. A platform 70 pixels above the ground is just out of reach; to reach it, raise the jump speed to 13, giving pixels.
Worked example 2: falling through the floor
The player falls from a high platform. After 20 frames of falling, y speed is −20, so it moves 20 pixels in one frame. A platform only 10 pixels thick can be skipped completely: one frame the player is above it, the next it is below it, and the touching check never sees an overlap. This is called tunnelling.
Two fixes: make platforms thicker than the fastest fall, or limit the speed. Add inside the forever loop, after the gravity block:
if <(y speed) < (-12)> then set [y speed] to (-12)
Real games call this a terminal velocity. With it, the player never moves more than 12 pixels a frame, so a 15-pixel platform always catches it.
If the player drops through a gap, it falls off the bottom of the stage. Add this second script to the Player; in the next lesson it will also cost a life.
Warning
Common bugs and their causes
griffpatch: Code a Platformer Game, part 1, The Basics. A well-known Scratch creator builds gravity, jumping and wall collisions step by step. His method is more advanced than this lesson's, but the ideas are the same.
Practice
Sources and credits
This lesson adapts CC BY-SA material and is shared under CC BY-SA 4.0.