dedew84
dedew84

Reputation: 23

Pygame sprite jump with pressed key

I'm currently making a 2D platformer, I want the player sprite to jump when it touches the ground and the jump key is being pressed.

I had this at the begging, seeing it wasn't doing what I wanted, I wrote a new code using boolean values.

Original code:

if not (event == None):
        if (event.type == pygame.KEYDOWN):
            if (event.key == pygame.K_UP or event.key == pygame.K_SPACE):
                if (self.vspeed == 0):
                    self.vspeed = -(self.speed)*2

New code:

if not (event == None):
        if(event.type == pygame.KEYDOWN):
            if (event.key == pygame.K_UP or event.key == pygame.K_SPACE):
                jump_pressed = True

        elif (event.type == pygame.KEYUP):
            if (event.key == pygame.K_UP or event.key == pygame.K_SPACE):
                jump_pressed = False

        elif (jump_pressed == True and self.vspeed == 0):
            self.vspeed = -(self.speed)*2
            print("jump is pressed")

Sadly, the new code doesn't work and I don't understand why. I did alot of research and tests over the past week without any success. The player jumps when I press the key but it doesn't jump again when it touches the ground.

I need to release the key and press it again to make the player jump. "jump is pressed" only gets printed when I press down.

Looking forward to your help :)

Upvotes: 0

Views: 501

Answers (2)

furas
furas

Reputation: 142734

problem can be because you have last elif inside for event loop.

You need something like this:

for event in pygame.event.get():

    if event.type == pygame.KEYDOWN:
        if event.key in (pygame.K_UP, pygame.K_SPACE):
            jump_pressed = True

    elif event.type == pygame.KEYUP:
        if event.key in (pygame.K_UP, pygame.K_SPACE):
            jump_pressed = False

# after `for` loop
if jump_pressed and self.vspeed == 0:
    self.vspeed = -(self.speed)*2
    print("jump is pressed")

Upvotes: 1

Tordek
Tordek

Reputation: 10882

Your code correctly sets the jump_pressed variable here:

if not (event == None):
    if(event.type == pygame.KEYDOWN):
        if (event.key == pygame.K_UP or event.key == pygame.K_SPACE):
            jump_pressed = True
    elif (event.type == pygame.KEYUP):
        if (event.key == pygame.K_UP or event.key == pygame.K_SPACE):
            jump_pressed = False

However, this case

    elif (jump_pressed == True and self.vspeed == 0):
        self.vspeed = -(self.speed)*2
        print("jump is pressed")

is (almost) never true: the only way it can enter in that condition is that

  1. event is not None.
  2. event.type is neither KEYDOWN nor `KEYUP
  3. jump_pressed is True and self.vspeed is 0.

You have to change the structure into

if event is not None:
    if event.type == pygame.KEYDOWN:
        if event.key in [pygame.K_UP, pygame.K_SPACE]:
            jump_pressed = True
    elif event.type == pygame.KEYUP:
        if event.key in [pygame.K_UP, pygame.K_SPACE]:
            jump_pressed = False

if jump_pressed and self.vspeed == 0:
    self.vspeed = -(self.speed)*2
    print("jump is pressed")

So that the jump_pressed check is performed even when no events are triggered.

Upvotes: 1

Related Questions