A Platform Game with Gravity
π― A character runs, jumps, falls and lands on platforms.
π‘ The idea behind it
Gravity in a game is one variable that is constantly added to the y position and reset on landing. Understanding this is the step from making toys to making games.
π§± Blocks you will need
variables (yv)foreverif β¦ thentouching colour?change y bykey pressed?
π¨ Build it
- Make a variable yv β the vertical speed.
- forever: change yv by β1 (that is gravity) β change y by yv.
- If touching the platform colour: repeat until not touching β change y by 1, then set yv to 0. That pushes you out of the floor and stops the falling.
- Jump: if (key up pressed) and (touching platform colour) β set yv to 12. The second condition is what stops jumping in mid-air.
- Left and right: change x by 5 or β5, with a similar push-out if you walk into a wall.
π Make it harder
- Add moving platforms and make the player ride them.
- Add a scrolling level by moving everything else instead of the player.
π Bugs you will hit
- The player sinks through the floor: the fall speed per frame is larger than the platform is thick. Either thicken the platform or cap yv.
- You can jump forever in mid-air: the ground check is missing from the jump condition.
- The player judders on the ground: yv is not being reset to 0 on landing.
β¬ Need Scratch itself?
Free from MIT, in a browser or as an offline app that needs no connection at all.