2
\$\begingroup\$

I have come across a problem in my code for my game. Currently i am developing a 2D platformer game in pygame. I have gotten the character to move left and right, although i haven't been able to get him to jump. I have experimented with a number of possibilities that all haven't worked.

while True:
    #handles user input
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            if (event.key == K_LEFT or event.key == K_a) and player.direction != RIGHT:  #if the left key is pressed
                player.direction = LEFT  #change the direction to left
                player.playerPicture = player.runLeft
                player.playerPicture = player.playerPicture.convert_alpha()
                DISPLAYSURF.blit(player.playerPicture, (player.playerx, player.playery))

            elif (event.key == K_RIGHT or event.key == K_d) and player.direction != LEFT:  #if the right key is pressed
                player.direction = RIGHT #change the directions to right
                player.playerPicture = player.runRight
                player.playerPicture = player.playerPicture.convert_alpha()
                DISPLAYSURF.blit(player.playerPicture, (player.playerx, player.playery))

            elif (event.key == K_UP or event.key == K_w) and player.direction != LEFT and player.direction != RIGHT:
                player.direction = UP
                player.playerPicture = player.jump
                player.playerPicture = player.playerPicture.convert_alpha()
                DISPLAYSURF.blit(player.playerPicture, (player.playerx, player.playery))



            elif event.key == K_ESCAPE: #if the escape key is pressed
                 terminate()  #quit the game


        else: 
            player.playerPicture = player.standingStill
            player.playerPicture = player.playerPicture.convert_alpha()
            player.direction = None

    DISPLAYSURF.fill(Black)

    #update game state

    if player.direction == LEFT:
         #if the players position is greater than the left boundary
        if (player.playerx > boundaryLeft):
            player.playerx -= 8 #move the player to the left

        #if the direction is right
    elif player.direction == RIGHT:
        if (player.playerx < boundaryRight): #player is less than the right boundary
            player.playerx += 8  #move the player to the right

    elif player.direction == UP:

This code is in the mainloop of the game. It pretty much checks what key has been pressed, applies the appropriate image for the character and changes the direction. Afterwards the character is manipulated based on the direction that he is facing. If he is facing right, he will walk right etc.

class Player(pygame.sprite.Sprite):
def __init__(self): #constructor
    pygame.sprite.Sprite.__init__(self) #constructor for the sprite
    self.playerPicture = pygame.image.load('JumpLeft.png')  
    self.runRight = pygame.image.load('RunRight2.png')  
    self.runLeft = pygame.image.load('RunLeft2.png')
    self.jump = pygame.image.load('Jump.png')
    self.playerPicture = self.playerPicture.convert_alpha()
    self.rect = self.playerPicture.get_rect()           

    self.standingStill = pygame.image.load('JumpLeft.png')  
    self.playerx = windowWidth / 2                        
    self.playery = windowHeight - 200
    self.direction = None          
    self.onGround = False
    self.isJumping = False;
    self.gravity = 1.2
    self.velocity = 0


    self.rect.center = (self.playerx, self.playery)

Current attempts: So far i've tried

if direction == 'UP':
   player.playery -= 2

which the character pretty much just continuously moves up until the user releases the space button

\$\endgroup\$
2
  • \$\begingroup\$ "I haven't been able to properly get him to jump. ...all haven't worked." Can you define "properly" in more detail, or describe specifically what hasn't worked about your attempts so far? The more precise you are about the problems you need help with, the easier it is to get focused, useful advice. There are an awful lot of ways for a game feature to "not work" after all. ;) \$\endgroup\$
    – DMGregory
    Commented Aug 31, 2017 at 10:00
  • \$\begingroup\$ What i was meant to say is i wasn't able to get the character to jump. Ignore the properly \$\endgroup\$
    – Gabriel
    Commented Aug 31, 2017 at 10:16

1 Answer 1

2
\$\begingroup\$

It's actually pretty simple!

When you jump, you have upwards velocity that is greater than the force of gravity. As time progresses this upwards velocity starts to decrease because of gravity! After a while it becomes negative and you start heading towards the ground. Finally, when you hit the ground your velocity becomes zero.

(I am not a physicist so if their is a mistake please don't kill me!)

Same goes with games:

  1. When the user presses space you set the player's velocity to a number greater than the force of gravity

  2. Then, each time the game updates, you change the players velocity based on the force of gravity and the time passed since the last update (delta)

  3. When the player hits the ground, you set the players velocity to 0

Here is an example in code:

When the user presses jump:

player.isJumping = true
player.velocity = x

When the game updates:

if (player.isJumping):
   player.velocity -= player.gravity * delta

player.playery += player.velocity

When the player hits the ground:

player.isJumping = False
player.velocity = 0

(x is the upwards velocity you want the player to have when the user presses jump)

\$\endgroup\$
12
  • \$\begingroup\$ Thank you! i semi-understand, i just have trouble figuring out the code for it. So if my game runs 30 times a second, then delta is equal to a 30th of a second correct? \$\endgroup\$
    – Gabriel
    Commented Aug 31, 2017 at 11:32
  • \$\begingroup\$ @Gabriel Yes that is correct \$\endgroup\$
    – J22o
    Commented Aug 31, 2017 at 11:33
  • \$\begingroup\$ And for me to check when the player hits the ground, would i have check if the playersy position > ground? for example, IF (playery > 400). Thank you so much for your help! \$\endgroup\$
    – Gabriel
    Commented Aug 31, 2017 at 11:37
  • \$\begingroup\$ @Gabriel yes, but you could also use collision detection \$\endgroup\$
    – J22o
    Commented Aug 31, 2017 at 11:54
  • \$\begingroup\$ i have implemented the code, but unfortunately my character just travels up endlessly and doesn't come back down \$\endgroup\$
    – Gabriel
    Commented Aug 31, 2017 at 11:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .