PyGameExamplesAndAnswers

StackOverflow            reply.it reply.it

“You must not fool yourself and you are the easiest person to fool.”
Richard P. Feynman


Jump

Related Stack Overflow questions:

How to make a character jump in Pygame?

To make a character jump you have to use the KEYDOWN event, but not pygame.key.get_pressed(). pygame.key.get_pressed () is for continuous movement when a key is held down. The keyboard events are used to trigger a single action or to start an animation such as a jump. See alos How to get keyboard input in pygame?

pygame.key.get_pressed() returns a sequence with the state of each key. If a key is held down, the state for the key is True, otherwise False. Use pygame.key.get_pressed() to evaluate the current state of a button and get continuous movement.

while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
            jump = True

Use pygame.time.Clock (“This method should be called once per frame.”) you control the frames per second and thus the game speed and the duration of the jump.

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

The jumping should be independent of the player’s movement or the general flow of control of the game. Therefore, the jump animation in the application loop must be executed in parallel to the running game.

When you throw a ball or something jumps, the object makes a parabolic curve. The object gains height quickly at the beginning, but this slows down until the object begins to fall faster and faster again. The change in height of a jumping object can be described with the following sequence:

[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10]

Such a series can be generated with the following algorithm (y is the y coordinate of the object):

jumpMax = 10
if jump:
    y -= jumpCount
    if jumpCount > -jumpMax:
        jumpCount -= 1
    else:
        jump = False 

📁 Minimal example - Jump (integral)
repl.it/@Rabbid76/PyGame-Jump

📁 Minimal example - Jump (floating point)
repl.it/@Rabbid76/PyGame-JumpFloat

A more sophisticated approach is to define constants for the gravity and player’s acceleration as the player jumps:

acceleration = 10
gravity = 0.5

The acceleration exerted on the player in each frame is the gravity constant, if the player jumps then the acceleration changes to the “jump” acceleration for a single frame:

acc_y = gravity
for event in pygame.event.get():
    if event.type == pygame.KEYDOWN: 
        if vel_y == 0 and event.key == pygame.K_SPACE:
            acc_y = -acceleration

In each frame the vertical velocity is changed depending on the acceleration and the y-coordinate is changed depending on the velocity. When the player touches the ground, the vertical movement will stop:

vel_y += acc_y
y += vel_y
if y > ground_y:
    y = ground_y
    vel_y = 0
    acc_y = 0

📁 Minimal example - Jump with acceleration
repl.it/@Rabbid76/PyGame-JumpAcceleration