1. <legend id='9VKag'><style id='9VKag'><dir id='9VKag'><q id='9VKag'></q></dir></style></legend>
  2. <small id='9VKag'></small><noframes id='9VKag'>

      <i id='9VKag'><tr id='9VKag'><dt id='9VKag'><q id='9VKag'><span id='9VKag'><b id='9VKag'><form id='9VKag'><ins id='9VKag'></ins><ul id='9VKag'></ul><sub id='9VKag'></sub></form><legend id='9VKag'></legend><bdo id='9VKag'><pre id='9VKag'><center id='9VKag'></center></pre></bdo></b><th id='9VKag'></th></span></q></dt></tr></i><div id='9VKag'><tfoot id='9VKag'></tfoot><dl id='9VKag'><fieldset id='9VKag'></fieldset></dl></div>
        <bdo id='9VKag'></bdo><ul id='9VKag'></ul>
      <tfoot id='9VKag'></tfoot>

    1. 您如何使用 kivy 检查键盘事件?

      时间:2023-10-10

        <i id='SGoQf'><tr id='SGoQf'><dt id='SGoQf'><q id='SGoQf'><span id='SGoQf'><b id='SGoQf'><form id='SGoQf'><ins id='SGoQf'></ins><ul id='SGoQf'></ul><sub id='SGoQf'></sub></form><legend id='SGoQf'></legend><bdo id='SGoQf'><pre id='SGoQf'><center id='SGoQf'></center></pre></bdo></b><th id='SGoQf'></th></span></q></dt></tr></i><div id='SGoQf'><tfoot id='SGoQf'></tfoot><dl id='SGoQf'><fieldset id='SGoQf'></fieldset></dl></div>
          <tbody id='SGoQf'></tbody>
        1. <tfoot id='SGoQf'></tfoot>

          <small id='SGoQf'></small><noframes id='SGoQf'>

          <legend id='SGoQf'><style id='SGoQf'><dir id='SGoQf'><q id='SGoQf'></q></dir></style></legend>
            <bdo id='SGoQf'></bdo><ul id='SGoQf'></ul>

                本文介绍了您如何使用 kivy 检查键盘事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                所以,不久前,我开始自学 kivy.我从主要的 kivy 网站开始,浏览了它的 pong 制作教程,完成后我决定尝试给它关键输入.我似乎找不到任何关于 kivy 键输入的指南!任何人都知道某种教程或可以提供一些易于理解的代码?我确实在 kivy 的示例文件夹中查看了键盘侦听器,但如果我应该使用它,我不太确定如何使用它.

                So, awhile ago, I started teaching myself kivy. I started with the main kivy website and went through its pong making tutorial and upon finishing that I decided to try and give it key input. I just can't seem to find any kind of guide to key input with kivy! Anyone know some kind of tutorial or can provide some easy to understand code? I did look at the Keyboard Listener in the examples folder of kivy, but I'm not quite sure how to use that if I'm supposed to.

                感谢您的帮助.

                推荐答案

                我猜你在问如何用键盘控制桨.我假设您的计算机上运行了最终的乒乓球代码(如果没有,您可以在 本节).

                I guess you are asking how to control the paddles with the keyboard. I assume you have the final ping pong codes running on your computer (If not, you can find them at the end of this section).

                1 - 在 main.py 中导入 Window 类:

                1 - In the main.py import the Window class:

                from kivy.core.window import Window
                

                2 - 重新定义 PongGame 类的开头,使其如下所示:

                2 - Redefine the beginning of the PongGame class so it looks like the following:

                class PongGame(Widget):
                    ball = ObjectProperty(None)
                    player1 = ObjectProperty(None)
                    player2 = ObjectProperty(None)
                
                    def __init__(self, **kwargs):
                        super(PongGame, self).__init__(**kwargs)
                        self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
                        self._keyboard.bind(on_key_down=self._on_keyboard_down)
                
                    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):
                        if keycode[1] == 'w':
                            self.player1.center_y += 10
                        elif keycode[1] == 's':
                            self.player1.center_y -= 10
                        elif keycode[1] == 'up':
                            self.player2.center_y += 10
                        elif keycode[1] == 'down':
                            self.player2.center_y -= 10
                        return True
                

                瞧!按 ws 为左桨,按 updown 为右桨.

                Voilà! Press w and s for the left paddle and up and down for the right paddle.

                这篇关于您如何使用 kivy 检查键盘事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:无法获得窗口,中止 下一篇:在 Kivy 中更改按钮的背景颜色

                相关文章

                最新文章

                <i id='zREnn'><tr id='zREnn'><dt id='zREnn'><q id='zREnn'><span id='zREnn'><b id='zREnn'><form id='zREnn'><ins id='zREnn'></ins><ul id='zREnn'></ul><sub id='zREnn'></sub></form><legend id='zREnn'></legend><bdo id='zREnn'><pre id='zREnn'><center id='zREnn'></center></pre></bdo></b><th id='zREnn'></th></span></q></dt></tr></i><div id='zREnn'><tfoot id='zREnn'></tfoot><dl id='zREnn'><fieldset id='zREnn'></fieldset></dl></div>
              • <small id='zREnn'></small><noframes id='zREnn'>

                  • <bdo id='zREnn'></bdo><ul id='zREnn'></ul>
                  <tfoot id='zREnn'></tfoot>

                  <legend id='zREnn'><style id='zREnn'><dir id='zREnn'><q id='zREnn'></q></dir></style></legend>