Search code examples
pythonpygamesprite

Why won't my Pygame sprite jump code work?


I am trying to make my Pygame sprite jump so I followed a tutorial and this is what it gave me. But it doesn't work for some reason. What's wrong with it

keys = pygame.key.get_pressed()
if isjump == False:
    if keys[pygame.K_UP]:
        if isjump:
            F =(1 / 2)*m*(v**2)
            player.rect.y -= F
            v = v-1
        if v<0:
            m =-1
        if v ==-6:
                isjump = False
                v = 5
                m = 1

Solution

  • You need to set somewher isjump = True. Additionally the if isjump == False: needs an else case:

    keys = pygame.key.get_pressed()
    if isjump == False:
        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