所以我读了这篇文章: How to handle several keys pressed at the same time in Kivy?
我根据 Kivy 网站 ( http://kivy.org/docs/tutorials/pong.html ) 上的教程实现了 Pong 游戏。 我的 PongGame Widget 有以下方法:
def __init__(self):
super(PongGame, self).__init__()
self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
self._keyboard.bind(on_key_down = self._on_keyboard_down)
self._keyboard.bind(on_key_up = self._on_keyboard_up)
def _keyboard_closed (self):
self._keyboard.unbind(on_key_down=self._on_keyboard_down)
self._keyboard = None
def _on_keyboard_down (self, keyboard, keycode, text, modifiers):
print('### ----------------------------------- ###')
print('The keys', keycode, 'have been pressed down')
print('You pressed the key', keycode[1], '.', sep=' ', end='\n')
#print(' - text is %r' % text)
print(' - modifiers are %r' % modifiers)
if keycode[1] == 'w':
if self.player1.center_y + 20 < self.height-85:
self.player1.center_y += 20
elif keycode[1] == 's':
self.player1.center_y -= 20
elif keycode[1] == 'up':
self.player2.center_y += 20
elif keycode[1] == 'down':
self.player2.center_y -= 20
return True
"""
def _on_keyboard_up (self, keyboard, keycode, text, modifiers):
print('### ----------------------------------- ###')
print('The keys', keycode, 'have been released.')
print('You pressed the key', keycode[1], '.', sep=' ', end='\n')
#print(' - text is %r' % text)
print(' - modifiers are %r' % modifiers)
return True
"""
def _on_keyboard_up(self, *args):
print('up', args)
这样我就可以使用“w”、“s”、“up”和“down”来控制两个玩家。但是,仅执行最后按下的按钮的操作。这是一个问题,因为当其他玩家想要移动他们的桨时,玩家可能会通过一直按下按钮来干扰彼此的输入。
我该如何实现,让两个玩家同时控制,这样他们就不会互相妨碍,除非键盘不能再按下任何按键(硬件限制)?
我目前想用Kivy开发桌面游戏,而不是智能手机游戏,所以我想用真正的键盘来控制角色。
最佳答案
我的解决方案非常相似,但更短。 这是使用 set() 作为事件按钮列表的简单示例。
因此,如果您不想在我的解决方案中添加另一个操作,您只需以“button_character”格式向 self.pressed_actions 字典添加条目:lambda 函数,该函数将使用参数运行您的函数。
此示例将向控制台提示哪些按钮在每一帧中处于事件状态。
放心使用。
*set 就像一个列表,但不重复。所以您不必担心同一按钮的更多条目。
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.clock import Clock
class Basic(Widget):
pFrame = 0
def __init__(self, **kwargs):
super(Basic, self).__init__(**kwargs)
self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
self._keyboard.bind(on_key_down=self._on_keyboard_down)
self._keyboard.bind(on_key_up=self._on_keyboard_up)
self.pressed_keys = set()
self.pressed_actions = {
'w': lambda: self.text_example('w pressed'),
's': lambda: self.text_example('s pressed'),
'a': lambda: self.text_example('a pressed'),
'd': lambda: self.text_example('d pressed'),
}
def _keyboard_closed(self):
self._keyboard.unbind(on_key_down=self._on_keyboard_down)
self._keyboard.unbind(on_key_up=self._on_keyboard_down)
self._keyboard = None
def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
self.pressed_keys.add(keycode[1])
def _on_keyboard_up(self, keyboard, keycode):
self.pressed_keys.remove(keycode[1])
def text_example(self, text):
print('Frame: %s Key %s' % (self.pFrame, text))
def update(self, dt):
for key in self.pressed_keys:
try:
self.pressed_actions[key]()
except KeyError:
print("Frame: %s Key %s. Omitted" % (self.pFrame, key))
self.pFrame += 1
class MyApp(App):
def build(self):
basic = Basic()
Clock.schedule_interval(basic.update, 1.0 / 2.0)
return basic
if __name__ == '__main__':
try:
MyApp().run()
except KeyboardInterrupt:
print('App has been closed by ^C')
关于Python Kivy - 如何对多次按键使用react?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31373783/