Workshop › Mario-style Platformer › Step 8 of 37
The player gets a speed: vx sideways, vy up and down. Gravity adds a little to vy every frame, so falling gets faster
and faster, up to MAX_FALL. update changes the game before each draw.
```js
const GRAVITY = 0.5
const MAX_FALL = 12 // must stay below TILE, or a fast fall could skip over a whole tile
if (line[col] === 'P') player = { x: x + 4, y: y + 2, w: 24, h: 30, vx: 0, vy: 0, grounded: false }
function updatePlayer() {
player.vy = Math.min(MAX_FALL, player.vy + GRAVITY)
player.y += player.vy
}
function update() {
updatePlayer()
}
update()
```vx, vy are the speeds; grounded will say whether the player stands on something.Math.min(MAX_FALL, vy + GRAVITY) adds gravity but never lets the speed pass 12.y += vy moves the player down by its speed.loop now calls update() before draw().COLORS, leave an empty line and write GRAVITY and MAX_FALL.loadLevel, add vx: 0, vy: 0, grounded: false to the player.function draw() { write updatePlayer and update.loop, call update() before draw(). Press Run and watch.Skills: Velocity and gravity, The game loop
← Draw the player Landing on tiles →