简体   繁体   中英

In pygame/python, is there a way to detect which key are pressed earlier or latter?

For example, if I hold down w then hold down a, is there a way for pygame to tell me which key i holded down first?

I need to know this because for the game im trying to make using pygame, the character doesnt have smooth movements. The example code of movement is below.

I first detect the and set the direction, then set the xchange and y change for each direction.

Then I add it to the x and y of the player which is then blit to the screen.

The problem is, if I hold down(s) then hold right(d), I want the character to move down and then moveright, but I have to release the down(s) button for that to happen. This is because in my code, the if keys[k_s] is placed at the bottom of the four directions and evaluated last, which will replace the direction value into down. The movement is smooth however if i hold down right(d) then down(s) to change direction because of the same reason.

Thanks for the help!

    keys = pygame.key.get_pressed()
if keys[K_a] or keys[K_d] or keys[K_w] or keys[K_s]:


    if keys[K_d] and keys[K_a]:
        direction = "none"

    if keys[K_w] and keys[K_s]:
        direction = "none"

    else:
        #if direction == "none":
        if keys[K_a]:
            direction = "left"
        if keys[K_d]:
            direction = "right"
        if keys[K_w]:
            direction = "up"
        if keys[K_s]:
            direction = "down"
else:
    direction = "none"

currentarea.putbackground()            
currentarea.putdoor()


if direction == "none":
    ychange = 0
    xchange = 0 

elif direction == "up":
    xchange = 0
    ychange = -3

elif direction == "down":
    xchange = 0
    ychange = 3

elif direction == "left":
    xchange = -3
    ychange = 0

elif direction == "right":
    xchange = 3
    ychange = 0

If you loop through the event loop you'll receive the events in the order they were created. What you then could do is create a list that queues the keys you press and removes them when you release them. The first element in the list will always be the first key you've pressed and not yet released.

pressed_keys = []
while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                pressed_keys.append("left")
            elif event.key == pygame.K_d:
                pressed_keys.append("right")
            elif event.key == pygame.K_w:
                pressed_keys.append("up")
            elif event.key == pygame.K_s:
                pressed_keys.append("down")
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_a:
                pressed_keys.remove("left")
            elif event.key == pygame.K_d:
                pressed_keys.remove("right")
            elif event.key == pygame.K_w:
                pressed_keys.remove("up")
            elif event.key == pygame.K_s:
                pressed_keys.remove("down")

    try:
        print(pressed_keys[0])  # Will give IndexError if list is empty
        #  print(pressed_keys)  # Uncomment to see it in action
    except IndexError:
        pass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM