Menu
CoddyTech

Jump Buffer

Part of the Worlds section of Coddy's Pygame journey. Lesson 14 of 35.

Players press jump when they see the landing coming, often a few frames too early. With the jump in the KEYDOWN event, that press finds the player in the air and is lost. The player feels the game ignored them.

A jump buffer remembers the press for a few frames. The event only starts a timer:

elif event.type == pygame.KEYDOWN and event.key in (pygame.K_SPACE, pygame.K_UP):
    jump_timer = JUMP_BUFFER

Each frame, before gravity, jump if the timer is still running and the player is on the ground, and count the timer down:

if jump_timer > 0 and on_ground:
    vel_y = JUMP
    jump_timer = 0
if jump_timer > 0:
    jump_timer -= 1

With JUMP_BUFFER = 6 a press counts for 6 frames, a tenth of a second: long enough to catch an early press, short enough that a press long before the landing does not jump by surprise. A press on the ground still jumps on the next frame.

challenge icon

Challenge

Hard
  1. Add JUMP_BUFFER = 6 at the top and jump_timer = 0 before the loop.
  2. On a KEYDOWN of Space or Up, set jump_timer = JUMP_BUFFER instead of jumping.
  3. Each frame, before gravity: when jump_timer > 0 and on_ground, set vel_y = JUMP and jump_timer = 0. Then, if jump_timer > 0, take 1 off it.

Try it yourself

import pygame

TILE = 36
FLOOR = 324
GRAVITY = 1
JUMP = -15
MAX_FALL = 12

pygame.init()
screen = pygame.display.set_mode((576, 360))
pygame.display.set_caption("Jump Buffer")
clock = pygame.time.Clock()

background = pygame.image.load("assets/backgrounds/clouds.png").convert_alpha()
grass_img = pygame.image.load("assets/images/grass.png").convert_alpha()
player_img = pygame.image.load("assets/images/player.png").convert_alpha()

player = player_img.get_rect(topleft=(264, 40))
vel_y = 0
on_ground = False

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN and event.key in (pygame.K_SPACE, pygame.K_UP):
            if on_ground:
                vel_y = JUMP
        elif event.type == pygame.KEYUP and event.key in (pygame.K_SPACE, pygame.K_UP):
            if vel_y < -4:
                vel_y = -4

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        player.x -= 4
    if keys[pygame.K_RIGHT]:
        player.x += 4

    vel_y = min(vel_y + GRAVITY, MAX_FALL)
    player.y += vel_y
    on_ground = False
    if player.bottom > FLOOR:
        player.bottom = FLOOR
        vel_y = 0
        on_ground = True

    for y in range(0, 360, 72):
        for x in range(0, 576, 72):
            screen.blit(background, (x, y))
    for x in range(0, 576, TILE):
        screen.blit(grass_img, (x, FLOOR))
    screen.blit(player_img, player)
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Worlds

Practice on your own: Pygame playground