SHIFT pressing interrupts holding W in pygame

I am doing a roguelike in Pygame. I am trying to get my character to move forward with the W or UP key. When SHIFT is held down, the movement should be twice as fast. If I hold SHIFT and then press W, the program works as it should, however if I hold W and the character is already advancing and then I press SHIFT the character stops. This only happens occasionally, but it needs to be fixed.

    character_pos = [0,0]
    move_speed = 1
    mods = pygame.key.get_mods()
    if mods & KMOD_SHIFT:
        move_speed = 2
    key = pygame.key.get_pressed()
    if key[pygame.K_w] or key[pygame.K_UP]:
        w_count += move_speed
        if w_count == 20:
            w_count = 0
            if not is_wall(character_pos[0], character_pos[1]-1):
                character_pos[1] -= 1

      

Any ideas?

+3


source to share


1 answer


If it w_count

starts at 0 and increases in steps of 1

or 2

, it will always skip 20

as it increases. But if it is increased by a combination of 1

and 2

s, it can be from 19

to 21

. Could this be the problem? Try changing if w_count == 20

to if w_count >= 20

.



I apologize if I misunderstood your code, I am not 100% clearing the counter target.

+2


source







All Articles