Python 2.7.10 Kivyにてon_key_up、_on_keyboard_up等が使用できない?
Kivyにてこのプログラムを実行すると、
TypeError: _on_keyboard_up() takes exactly 5 arguments (3 given)
と出てしまうのですが、何か間違っているところがあれば指摘お願い致します。
on_key_up、_on_keyboard_up等が使用できないのであれば、キーを押している間やまたはキーを離した後という事をKivy内で判定できる他の方法を教えていただければ幸いです。
main.py
#-*- coding: utf-8 -*-
from kivy.config import Config
Config.set('graphics', 'width', '800')
Config.set('graphics', 'height', '600')
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import NumericProperty, ReferenceListProperty,\
ObjectProperty, ListProperty
from kivy.clock import Clock
sm = ScreenManager()
keyrpush = 0 #右キーが押されているか
class Player(Widget):
pass
class FirstScreen(Screen):
player = ObjectProperty(None)
shots = ListProperty()
def __init__(self, **kwargs):
super(FirstScreen, self).__init__(**kwargs)
self._keyboard = Window.request_keyboard(
self._keyboard_closed, self, 'text')
if self._keyboard.widget:
self._keyboard.bind(on_key_down=self._on_keyboard_down)
self._keyboard.bind(on_key_up=self._on_keyboard_up)
def _keyboard_closed(self):
print('My keyboard have been closed!')
self._keyboard.unbind(on_key_down=self._on_keyboard_down)
self._keyboard.unbind(on_key_up=self._on_keyboard_up)
self._keyboard = None
def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
print('The key', keycode, 'have been pressed')
print(' - text is %r' % text)
if keycode == (275, 'right') :
keyrpush = 1
print self.player.center_x
if keycode == (276, 'left') :
print "左"
self.player.center_x = self.player.center_x - 1
if keycode == (273, 'up') :
print "上"
self.player.center_y = self.player.center_y + 1
if keycode == (274, 'down') :
self.player.center_y = self.player.center_y - 1
print "下"
if keycode == (122, 'z'):
if len(modifiers) == 1:
if modifiers[0] == "shift":
print "アイテムショット"
else:
print "通常ショット"
return True
def _on_keyboard_up(self, keyboard, keycode, text, modifiers):
print('終わり')
return True
def update(self, dt):
pass
class MainApp(App):
def build(self):
game = FirstScreen()
sm.add_widget(FirstScreen(name="first"))
Clock.schedule_interval(game.update, 1.0 / 60.0)
return sm
if __name__ == '__main__':
MainApp().run()
main.kv
<player>:
canvas:
Rectangle:
size: 21,38
pos: self.pos
source: 'img/player.png'
<Shot>:
size: 10, 30
canvas:
Ellipse:
pos: self.pos
size: self.siz
<FirstScreen>:
player: player
canvas:
Rectangle:
pos: 500, -42.5
size: 303,640
source: 'img/frame.png'
Label:
font_size: 20
pos: 350, 120
text: "/1000"
font_name: 'fonts/Optical A Normal.ttf'
Label:
font_size: 20
pos: 350, 172.5
text: "0"
font_name: 'fonts/Optical A Normal.ttf'
Label:
font_size: 20
pos: 350, 220
text: "未実装"
font_name: 'fonts/NotoSansCJKjp-Thin.ttf'
Label:
font_size: 20
pos: 275, 120
text: "100"
font_name: 'fonts/Optical A Normal.ttf'
Player:
id: player
center_x: root.center_x - 87.5
center_y: 150
使用環境
macOS 10.13.2
pyenvのPython 2.7.10(Kivy on iOS等を使用する為)