1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] How should I wait until a key is released in pygame? [duplicate]

Discussão em 'Python' iniciado por Stack, Outubro 5, 2024 às 10:12.

  1. Stack

    Stack Membro Participativo

    I am working on a Mario-style game. I have implemented collisions and jumping, but there is one problem. If I just keep holding the space bar, the character keeps jumping. I do not want this to happen. I want the player to have to let go of the spacebar and then press the spacebar again, in order to jump again. Rather than just keep jumping by keeping the spacebar held. In other words, once the player jumps and lands on the ground, even if the spacebar is held, the player should not jump.

    How exactly could I do this in a simple way? Here the part of code that is in question:

    if keys[pygame.K_w] and not in_air and collided:
    in_air = True
    y_velocity += jump_acceleration
    collisions = False


    I tried to implement a "last_jumped_time" variable and make it so that the player could only jump every 1 second. However this did not really fully answer my question. I think that I have to use the pygame.KEYUP function somewhere to wait until the up key is released.

    Continue reading...

Compartilhe esta Página