pythonpygamespritepygame2

How do I make my Pygame Sprite jump higher and farther?


I am making a game in Pygame and this is my code to jump:

keys = pygame.key.get_pressed()
if isjump == False:
    #Up arrow key
    if keys[pygame.K_UP]:
        isjump = True
        v = 5
else:
    m = 1 if v >= 0 else -1
    F = m * (v**2)
    player.rect.y -= F

    v -= 1
    if v == -6:
        isjump = False

I want it to jump farther and higher. How can I do it?


Solution

  • The height of the jump depends on v. Define a variabel (jump_v) for the initial value of v. Use a higher value than 5, for a higher jump:

    jump_v = 7 # try different values
    
    keys = pygame.key.get_pressed()
    if isjump == False:
        #Up arrow key
        if keys[pygame.K_UP]:
            isjump = True    
            v = jump_v              # <--- jump_v 
    else:
        m = 1 if v >= 0 else -1
        F = m * (v**2)
        player.rect.y -= F
        
        v -= 1
        if v < -jump_v:             # <--- jump_v 
            isjump = False