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.