Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 41 ยท Game Physics and Animation

Gravity, Jumping and Platforms in Pygame

Make a character fall and jump in Pygame: gravity as acceleration, jump impulse, ground and platform collision, coyote time, jump buffering and variable jump height.

Gravity and jumping are the core of every platformer, and they are simpler than they sound. A character has a vertical velocity. Gravity changes that velocity a little each frame. A jump sets the velocity to a negative number (up is negative in screen coordinates). Collision with the floor stops the fall and resets the velocity. Everything else, from double jumps to ice floors, is a variation on those four sentences.

The real Pygame loop is read-only here. All physics below also runs as headless simulations so you can tune numbers and see the arc of a jump as text.

Falling: acceleration, not speed

Gravity is an acceleration: it changes velocity, and velocity changes position.

Python
GRAVITY = 1800        # pixels per second per second
vy += GRAVITY * dt    # velocity changes first
y  += vy * dt         # then position changes

Updating velocity before position (called semi-implicit Euler integration) is stable and good enough for games. With a fixed step, as in Movement and Delta Time, results are the same on every computer.

Jumping: an impulse

A jump gives the character an instant upward velocity, but only when standing on something:

Python
JUMP_SPEED = -650     # negative = up

if on_ground and jump_pressed:
    vy = JUMP_SPEED
    on_ground = False

How high will that go? The rise ends when velocity hits zero: v^2 / (2 * g). With these numbers: 650^2 / 3600 = 117 pixels. The time to the top is v / g, about 0.36 s. Rather than guess, designers pick the height and duration they want and compute the constants:

Python
jump_height = 120                       # pixels
time_to_apex = 0.35                     # seconds
GRAVITY = 2 * jump_height / time_to_apex ** 2
JUMP_SPEED = -GRAVITY * time_to_apex
Python Plot the arc of a jump as text

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Compare the printed peak with your design target of 120 pixels. The stepwise simulation is slightly off because it samples time discretely, which is normal.

Landing: floor and platform collision

The floor is just a number: if the character's feet pass below it, snap them back and zero the velocity. Real levels have platforms (rects). The standard recipe is to move and resolve one axis at a time:

Python
# horizontal
player.rect.x += round(vx * dt)
for p in platforms:
    if player.rect.colliderect(p):
        if vx > 0: player.rect.right = p.left
        if vx < 0: player.rect.left = p.right

# vertical
vy += GRAVITY * dt
player.rect.y += round(vy * dt)
on_ground = False
for p in platforms:
    if player.rect.colliderect(p):
        if vy > 0:                       # falling: landed on top
            player.rect.bottom = p.top
            on_ground = True
        elif vy < 0:                     # rising: bumped head
            player.rect.top = p.bottom
        vy = 0

Resolving X and Y separately means you always know from which side the collision came, which is impossible if you move both and check once. More on rect collisions is in the earlier sprites and collision chapter; the same ideas scale to tile maps in Tilemaps and Scrolling Camera.

Python A jump landing on a platform

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The character starts above the small platform, falls and lands on it (on_ground=True, vy=0). At frame 20 it jumps, rises, falls back and lands on the same platform again. If you start it below the platform instead (y=150), a jump that reaches the underside hits the vy < 0 branch and the head bumps the platform.

Making jumps feel good

Raw physics feels stiff. Three tricks used in nearly every polished platformer:

Variable jump height. Release the jump key early to cut the jump short:

Python
if event.type == pygame.KEYUP and event.key == pygame.K_SPACE and vy < 0:
    vy *= 0.5

Coyote time. Allow a jump for a few milliseconds after walking off a ledge. Keep a timer that resets to about 0.1 s while on the ground and counts down in the air; jump if the timer is above zero.

Jump buffering. If the player presses jump just before landing, remember it for about 0.1 s and jump as soon as you land. Players think they pressed on time.

Heavier fall. Apply stronger gravity while falling so that the jump feels snappy: g = GRAVITY * (1.6 if vy > 0 else 1.0).

Python Coyote time and jump buffering

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first press happens 4 frames after leaving the ledge (about 0.07 s), inside the coyote window, so it jumps. The second press happens late in the air and is remembered; it triggers the jump when the character touches ground again, a few frames later.

Other useful variations

  • Double jump: count air jumps and reset the count on landing.
  • Wall jump: while touching a wall, jump pushes away from it horizontally.
  • Moving platforms: add the platform's velocity to a player standing on it.
  • One-way platforms: only collide when vy > 0 and the feet were above the top last frame.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In Pygame screen coordinates, which sign of velocity.y moves a character upward?

    The origin is at the top-left, and y grows downward, so up is negative.
  2. 2. Why resolve horizontal and vertical collisions separately?

    After moving along one axis, any overlap must come from that axis, so you can snap to the correct edge.
  3. 3. What does coyote time allow?

    A short grace period after leaving the ground makes jumps feel fair.
  4. 4. How can you make a jump end earlier when the player releases the key?

    Cutting an upward velocity (for example halving it) shortens the rise, producing variable jump height.

Summary

  • Gravity is an acceleration added to vertical velocity each step; velocity then moves the position.
  • A jump sets a negative velocity while on the ground; derive constants from the height and time you want.
  • Resolve horizontal then vertical movement against platforms, snapping to edges and zeroing velocity.
  • Variable jump height, coyote time, jump buffering and heavier falling make controls feel responsive.

Next: Vectors and Angles