Python 如何使用 pygame.KEYDOWN?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25494726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:18:55  来源:igfitidea点击:

How to use pygame.KEYDOWN?

pythoneventspygamekeydown

提问by Lord_GumquestriX

So, my question was most likely already asked, but I didn't know what to search for, and didnt find much. So, my problem is, I made 2 functions, which would check for an event

所以,我的问题很可能已经被问到了,但我不知道要搜索什么,也没有找到太多。所以,我的问题是,我做了 2 个函数,它们会检查一个事件

def get_pygame_events():
  pygame_events = pygame.event.get()
  return pygame_events

and

def get_keys_pressed(self):
  keys_pressed = get_pygame_events()  #pygame.event.get(pygame.KEYDOWN)
  # print(keys_pressed)
  keys_pressed_list = []
  for event in keys_pressed:
    if event.type == pygame.KEYDOWN:
      if event.key == K_LEFT:
        keys_pressed_list.append("left")
      if event.key == K_RIGHT:
        keys_pressed_list.append("right")
      if event.key == K_UP:
        keys_pressed_list.append("up")
      if event.key == K_DOWN:
        keys_pressed_list.append("down")
      if event.key == K_a:
        keys_pressed_list.append("a")
      if  event.key == K_d:
        keys_pressed_list.append("b")
      if event.key == K_w:
        keys_pressed_list.append("w")
      if event.key == K_s:
        keys_pressed_list.append("s")
      if event.key == K_SPACE:
        keys_pressed_list.append("space")
      if event.key == K_q:
        keys_pressed_list.append("q")
      if event.key == K_e:
        keys_pressed_list.append("e")
    if event.type == pygame.MOUSEBUTTONDOWN:
      keys_pressed_list.append("click")
      return (keys_pressed_list, event.pos)
  return keys_pressed_list

I expected that if I could do something similar to:

我希望如果我可以做类似的事情:

while True:
  Variable1 = get_pygame_events()
  Variable2 = get_keys_pressed()
  if Variable2 == ["w"]:
    print("w")

(P.S.: That whileloop was just a summary of what I did) then if I held down W, then W would be printed over and over and over again, instead, when I tried, it printed W once. and unless I pressed again, that is all that would happen. How can I make it so by holding down the W (or any) key, it identifies the event happening, and (in this case) prints w every time it goes through the whileloop?

(PS:那个while循环只是我所做的总结)然后如果我按住 W,那么 W 将一遍又一遍地打印,相反,当我尝试时,它打印 W 一次。除非我再次按下,否则就会发生这种情况。我怎样才能通过按住 W(或任何)键来实现它,它会识别正在发生的事件,并且(在这种情况下)每次通过while循环时都打印 w ?

回答by sloth

Use pygame.KEYDOWNand pygame.KEYUPto detect if a key is physically pressed down or released. You can activate keyboard repeat by using pygame.key.set_repeatto generate multiple pygame.KEYDOWNevents when a key is held down, but that's rarely a good idea.

使用pygame.KEYDOWNpygame.KEYUP检测按键是否被物理按下或释放。您可以通过使用在按住某个键时pygame.key.set_repeat生成多个pygame.KEYDOWN事件来激活键盘重复,但这很少是一个好主意。

Instead, you can use pygame.key.get_pressed()to check if a key is currently held down:

相反,您可以使用pygame.key.get_pressed()来检查当前是否按下了某个键:

while True:
    ...
    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_w]:
       print("w is pressed")
    if pressed[pygame.K_s]:
       print("s is pressed")

回答by xwang

event.key == chr('a') 

event.key returns the ascii of the key

event.key 返回键的 ascii

回答by pydude

Use pygame.key.set_repeat().

使用 pygame.key.set_repeat()。

set_repeat(delay, interval) -> None.

When the keyboard repeat is enabled, keys that are held down will generate multiple pygame.KEYDOWN events. The delay is the number of milliseconds before the first repeated pygame.KEYDOWN will be sent. After that another pygame.KEYDOWN will be sent every interval milliseconds. If no arguments are passed the key repeat is disabled.

set_repeat(delay, interval) -> 无。

当启用键盘重复时,按下的键会产生多个 pygame.KEYDOWN 事件。延迟是发送第一个重复的 pygame.KEYDOWN 之前的毫秒数。之后,另一个 pygame.KEYDOWN 将每间隔毫秒发送一次。如果没有传递参数,则禁用键重复。

回答by user2015735

I would advice you to stick with the event-driven approach rather than using a polling mechanism.

我建议您坚持使用事件驱动的方法,而不是使用轮询机制。

You should let the key events alter some internal state to reflect a pressed key imo.

您应该让按键事件改变一些内部状态以反映按下的按键 imo。

Example:You are controlling a spaceship with your keyboard. You want the propulsion rockets to fire when you press one of 'w', 's', 'a' or 'd' to make the ship accelerate in a certain direction:

示例:您正在使用键盘控制宇宙飞船。您希望推进火箭在您按下“w”、“s”、“a”或“d”之一以使飞船向某个方向加速时发射:

  • On pygame.KEYDOWN event, set an appropriate acceleration vector for the object if event.key in [K_w, K_s, K_a, K_d].
  • On pygame.KEYUP event, set the acceleration vector to the zero vector if event.key in [K_w, K_s, K_a, K_d].
  • 在 pygame.KEYDOWN 事件上,如果 event.key 在 [K_w, K_s, K_a, K_d] 中,则为对象设置适当的加速度向量。
  • 在 pygame.KEYUP 事件中,如果 event.key 在 [K_w, K_s, K_a, K_d] 中,则将加速度向量设置为零向量。

This will effectively make the object accelerate while a movement key is pressed, and stop accelerating when the key is released.

这将有效地使对象在按下移动键时加速,并在松开键时停止加速。