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 :)
event == Nonefor comparisons withNone, useevent is None(or, in this caseevent is not None) instead. - Tordekifcondition. - Tordekif not (event == None):- don't you havefor event in pygame.event.get()which do it automatically ? - furasPlatformer exampleson Program Arcade Games With Python And Pygame (at the end of this page) - furaselifinsidefor eventloop. - furas