Been learning my first platformer game based on this tutorial.

I decided I wanted to go off book and make it so the player could jump a second time before hitting the ground. I used the blit function to show a jump counter under the score as well.

I was using if player_rect.bottom == 300: player.jump.counter = 2 (where 300 is my ground height) to reset the jump counter. However this seems to mean the first time the player jumps the counter does not reduce, I believe because they still == 300. On the second jump (once the player is in the air) the code works as intended and resets when the player lands again. I have tried adding a player_jumped = False variable but that hasnt helped and when I tried time.sleep(0.01) it slowed the whole code and the frame rate dropped.

So when you run the code the counter is at 2 to start, first space and the counter stays as 2 (this is the issue), second space and it reduces to 1, third space and it reduces to 0 and any more spaces and nothing happens until the player touches down and the counter resets.

I believe I somehow need to make it so that there is a delay between the player jumping and then the code checking if the player is on the ground and resetting the counter to 2, I have tried moving the line of code to different positions inside the loop but that hasnt helped.

Please see code below, mainly from the keyboard input heading onwards:

import pygame
from sys import exit

screen = pygame.display.set_mode((800,400))
pygame.display.set_caption("First game")
clock = pygame.time.Clock()
font = pygame.font.Font('Assets/font/Pixeltype.ttf', 50)
pink_colour = pygame.Color(242,172,170)

sky_surface = pygame.image.load("Assets/graphics/Sky.png").convert()

ground_surface = pygame.image.load("Assets/graphics/ground.png").convert()

score_surface = font.render("This is a test", False, 'Black').convert()
score_rect = score_surface.get_rect(center = (400, 50))

snail_surface = pygame.image.load('Assets/graphics/snail/snail1.png').convert_alpha()
snail_rect = snail_surface.get_rect(midbottom = (600,300))

player_surface = pygame.image.load('Assets/graphics/Player/player_walk_1.png').convert_alpha()
player_rect = player_surface.get_rect(midbottom = (80, 300))

player_gravity = 0
player_jump_counter = 2
player_has_jumped = False

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:

Keyboard input

        if event.type == pygame.KEYDOWN:
            if player_jump_counter > 0:
                if event.key == pygame.K_SPACE:
                    player_jump_counter -= 1
                    player_gravity = -20
                    player_has_jumped = True

        # Mouse input
        if event.type == pygame.MOUSEBUTTONDOWN:
            if player_rect.collidepoint(event.pos):
                player_gravity = -20

    if player_has_jumped == True:
        if player_rect.bottom == 300:
            player_jump_counter = 2
            player_has_jumped = False

    screen.blit(sky_surface, (0, 0))
    screen.blit(ground_surface, (0,300))
    pygame.draw.rect(screen, pink_colour, score_rect)
    pygame.draw.rect(screen, pink_colour, score_rect, 50)
    jump_surface = font.render(str(player_jump_counter), False, "Black")
    jump_rect = jump_surface.get_rect(midbottom = (400, 110))
    screen.blit(score_surface, score_rect)
    screen.blit(jump_surface, jump_rect)

# snail movement
    snail_rect.x -= 4
    if snail_rect.right <= 0:
        snail_rect.left = 800

    screen.blit(snail_surface, snail_rect)

# player movement
    player_gravity += 1
    player_rect.y += player_gravity
    if player_rect.bottom >= 300:
        player_rect.bottom = 300
    screen.blit(player_surface, player_rect)

  • \$\begingroup\$ Your code says that if the player jumps, they get 2 more jumps. Instead of making player_jump_counter=2 when the player jumped, perhaps make it 1? \$\endgroup\$
    – user253751
    Jul 19, 2022 at 17:57

1 Answer 1


The code logic of each frame can be simplified to:

  1. Read keyboard input and set gravity.
  2. Hit ground check (using position).
  3. Player move (using gravity to set position).

See what's wrong? When the jump command is in effect, the character is considered to be on the ground during the hitting ground check (because the character has not yet taken off at this time), and the jump count is reset. Then gravity takes effect and the character jumps. So the jump count is still 2 after the first jump.

You can adjust the order to:

Read keyboard input and set gravity -> Player move -> Hit ground check


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .