How to create a jump movement in pygame

Question:

I’m new to Python and I’ve been trying to make a simple game where you can move a square to the left, to the right and make it jump. For the last couple of days I’ve been trying to make the jump movement without success. Could someone help me?

import pygame
pygame.init()

xscreen, yscreen = 1000, 500
screen = pygame.display.set_mode((xscreen, yscreen))
pygame.display.set_caption("Pygame")

width, height = 50, 50
x, y = 950, 200
vel, jumping_points = 10, 10

run = True
while run:
    pygame.time.delay(100)

    # Checks if the player wants to quit the game.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # Checks if a key was pressed and moves the square accordingly.
    keys = pygame.key.get_pressed()

    if keys[pygame.K_RIGHT] and x + width < xscreen:
        x += vel
    if keys[pygame.K_LEFT] and x > 0:
        x -= vel
    # Unfinished jump movement.
    if keys[pygame.K_SPACE] and y > 0:
        while True:
            if jumping_points >= -10:
                y -= (jumping_points ** 2) / 5
                jumping_points -= 1
                animation()
            else:
                jumping_points = 10
                break

    # Updates the screen surface.
    animation()

pygame.quit()

Edit: Removed, not important to the question.

Asked By: user5318008

||

Answers:

Never try to implement a loop that controls the game within the application loop. Use the application loop. Add a variable jump = False. Set the variable when SPACE is pressed. Use the KEYDOWN event instead of pygame.key.get_pressed(). Implement the jump algorithm in the application loop, instead of an extra loop:

jump = False

run = True
while run:
    # [...]    

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                jump = True


    # Unfinished jump movement.
    if jump:
        if jumping_points >= -10:
            y -= jumping_points
            jumping_points -= 1
        else:
            jumping_points = 10
            jump = False

    # [...]

Additionally you need to change the formula used to calculate the jump. The result of (jump_points ** 2) is always a positive value and the player will never come down:

y -= (jumping_points ** 2) / 5

y -= jumping_points

or

y -= (jumping_points ** 2) / 5 * (1 if jumping_points > 0 else -1)

Use pygame.time.Clock to control the frames per second and thus the game speed.

The method tick() of a pygame.time.Clock object, delays the game in that way, that every iteration of the loop consumes the same period of time. See pygame.time.Clock.tick():

This method should be called once per frame.

That means that the loop:

clock = pygame.time.Clock()
run = True
while run:
   clock.tick(60)

runs 60 times per second.


Minimal example:

import pygame
pygame.init()

xscreen, yscreen = 1000, 500
screen = pygame.display.set_mode((xscreen, yscreen))
pygame.display.set_caption("Pygame")
clock = pygame.time.Clock()

def animation():
    screen.fill(0)
    pygame.draw.rect(screen, (255, 0, 0), (x, y, 20, 20))
    pygame.display.flip()

width, height = 50, 50
x, y = 950, 200
vel, jumping_points = 10, 10
jump = False


run = True
while run:
    clock.tick(60)

    # Checks if the player wants to quit the game.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                jump = True

    # Checks if a key was pressed and moves the square accordingly.
    keys = pygame.key.get_pressed()

    if keys[pygame.K_RIGHT] and x + width < xscreen:
        x += vel
    if keys[pygame.K_LEFT] and x > 0:
        x -= vel

    # Unfinished jump movement.
    if jump:
        if jumping_points >= -10:
            
            # either
            # y -= jumping_points
            # or
            y -= (jumping_points ** 2) / 5 * (1 if jumping_points > 0 else -1)
            
            jumping_points -= 1
        else:
            jumping_points = 10
            jump = False

    # Updates the screen surface.
    animation()

pygame.quit()
Answered By: Rabbid76
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.