Skip to content

Commit

Permalink
Enable keyboard support with explict config option input keyboard
Browse files Browse the repository at this point in the history
The keyboard listening code is taken from Kivy example [1].

[1]: http://kivy.org/docs/api-kivy.core.window.html
  • Loading branch information
livibetter committed May 12, 2014
1 parent c70babb commit 81aed42
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
15 changes: 14 additions & 1 deletion FlappyBird.py
Expand Up @@ -9,6 +9,8 @@
from kivy.vector import Vector
from kivy.app import App
from kivy.clock import Clock
from kivy.config import Config
from kivy.core.window import Window
from kivy.uix.widget import Widget

class Background(Widget):
Expand Down Expand Up @@ -51,6 +53,14 @@ class Mcnay(Widget):

def __init__(self, **kwargs):
super(Mcnay, self).__init__(**kwargs)
if Config.getdefault('input', 'keyboard', False):
self._keyboard = Window.request_keyboard(
self._keyboard_closed, self, 'text')
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 switch_to_normal(self, dt):
self.bird_image.source = "images/flappyup.png"
Expand All @@ -68,6 +78,9 @@ def on_touch_down(self, touch):
Clock.unschedule(self.stop_jumping)
Clock.schedule_once(self.switch_to_normal, self.jump_time / 5.0)

def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
self.on_touch_down(None)

def update(self):
self.pos = Vector(*self.velocity) + self.pos

Expand Down Expand Up @@ -157,4 +170,4 @@ def build(self):
return game

if __name__ == "__main__":
FlappyBirdApp().run()
FlappyBirdApp().run()
10 changes: 9 additions & 1 deletion README.md
Expand Up @@ -5,4 +5,12 @@ FlappyKivy

Flappy Bird clone programmed in Python + Kivy!

[<img src="https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif">](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=TGRE7G6ASVFVS)
[<img src="https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif">](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=TGRE7G6ASVFVS)

## Controls

Tap the touch screen, click using mouse, or start FlappyKivy with keyboard support:

```sh
$ python FlappyBird.py -c input:keyboard:True
```

0 comments on commit 81aed42

Please sign in to comment.