Alex D'ago
Alex D'ago

Reputation: 162

pygame jump animation seems more like a rocket launch

using this code everything seems right to me. First the player should go up, then once jumpcount turns negative, it should go down, but the movement seems more like a rocket that is never going to land. Could someone tell me what’s wrong with my code?

def movement():
    jump = False
    jumpcount = 10
    keyspressed = pygame.key.get_pressed()
    if keyspressed[pygame.K_w]:
        jump = True
    if jump == True:
        if jumpcount>=-10:
            player.y -= (jumpcount * abs(jumpcount))*0.5
            jumpcount-=1
        else:
            jump = False
            jumpcount = 10

Upvotes: 1

Views: 75

Answers (1)

Rabbid76
Rabbid76

Reputation: 211135

jump and jumpcount need to be variables in global namespace. These variables need to keep their value beyond frames. Use the global statement if you want to change variables in global namespace within a function (see Using global variables in a function):

jump = False
jumpcount = 10

def movement():
    global jump, jumpcount
   
    keyspressed = pygame.key.get_pressed()
    if not jump and keyspressed[pygame.K_w]:
        jump = True
        jumpcount = 10
    
    if jump == True:
        if jumpcount>=-10:
            player.y -= (jumpcount * abs(jumpcount))*0.5
            jumpcount-=1
        else:
            jump = False
            jumpcount = 10

See alos How to make a character jump in Pygame?

Upvotes: 1

Related Questions