简体   繁体   English

在pygame / python中,有没有一种方法可以检测早先或晚按的键?

[英]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? 例如,如果我按住w然后按住a,pygame是否可以告诉我我首先按住哪个键?

I need to know this because for the game im trying to make using pygame, the character doesnt have smooth movements. 我需要知道这一点,因为对于试图使用pygame制作的游戏,角色没有流畅的动作。 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. 我首先检测并设置方向,然后为每个方向设置xchange和y change。

Then I add it to the x and y of the player which is then blit to the screen. 然后,将其添加到播放器的x和y中,然后将其放到屏幕上。

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. 这是因为在我的代码中,if keys [k_s]放置在四个方向的底部并最后求值,它将把方向值替换为下。 The movement is smooth however if i hold down right(d) then down(s) to change direction because of the same reason. 但是,由于相同的原因,如果我按住right(d),然后按住down(s)改变方向,则运动很平稳。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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