28

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.

Thanks for any assistance.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Alex
  • 341
  • 2
  • 4
  • 6

1 Answers1

34

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 - In the main.py import the Window class:

from kivy.core.window import Window

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

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

illright
  • 3,991
  • 2
  • 29
  • 54
toto_tico
  • 17,977
  • 9
  • 97
  • 116
  • Ah, that seems to work for the time being, but what would I do if I wanted each class to have their own key checking? Would I only have to use the _on_keyboard_down func in each class? Or would I have to initialize the keyboard multiple times? – Alex Jun 25 '13 at 16:42
  • @Alex, that is a different question. Feel free to ask a new question instead of using the comments. – toto_tico Jun 25 '13 at 16:54
  • 9
    You would have to use _keyboard.bind() in all the widgets that need it. – Tshirtman Jun 25 '13 at 18:44
  • It gives an error to me `_on_keyboard_down() takes exactly 5 arguments (6 given) ` – Guglie Dec 12 '17 at 13:49
  • 1
    I'm trying this in kivy `v1.10.1.dev0, git-5f62b6b, 20180103` and no joy :( – CpILL Feb 04 '18 at 01:25
  • pretty sweet additions, thanks! I changed 10 to 50 and it seemed to be a pretty playable game. Though i don't know why, but after making these changes the ball keeps accelerating faster and faster. If left alone, it will eventually go through the left paddle. – FistOfFury Sep 04 '20 at 18:45