0
votes

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 :)

2
Don't use event == None for comparisons with None, use event is None (or, in this case event is not None) instead. - Tordek
Additionally, Python style eschews parentheses in the if condition. - Tordek
why do you check if not (event == None): - don't you have for event in pygame.event.get() which do it automatically ? - furas
better analyze code of Platformer examples on Program Arcade Games With Python And Pygame (at the end of this page) - furas
Problem can be because you have last elif inside for event loop. - furas

2 Answers

1
votes

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.

1
votes

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")