我正在尝试使用Kivy制作游戏。我创建了一个开始屏幕和游戏屏幕。该应用程序将无法运行,我得到错误“TypeError:bind()正好取0位置参数(给定1)”。所以当我尝试将屏幕动作绑定到“按下开始”图像时,听起来有些问题。
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.image import Image
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.label import Label
from kivy.uix.behaviors import ButtonBehavior
#beginning of screen manager code
class MenuScreen(Screen):
pass
class Game(Screen):
pass
sm = ScreenManager()
sm.add_widget(MenuScreen(name = "menu"))
sm.add_widget(Game(name = "game"))
#end of screen manager code
class IconButton(ButtonBehavior, Image):
pass
class MoveableImage(Image):
def __init__(self, **kwargs):
super(MoveableImage, self).__init__(**kwargs)
self._keyboard = Window.request_keyboard(None, self)
if not self._keyboard:
return
self._keyboard.bind(on_key_down=self.on_keyboard_down)
self._keyboard.bind(on_key_up=self.on_keyboard_up)
def on_keyboard_down(self, keyboard, keycode, text, modifiers):
if keycode[1] == 'left':
self.x -= 6
self.source = 'selectionscreen/jeezyleft.zip'
elif keycode[1] == 'right':
self.x += 6
self.source ='selectionscreen/jeezyright.zip'
elif keycode[1] == 'down':
self.y -= 6
self.source ='selectionscreen/jeezyright.zip'
elif keycode[1] == 'up':
self.y += 6
self.source = 'selectionscreen/jeezyback.zip'
else:
return False
return True
def on_keyboard_up(self, keyboard, keycode):
if keycode[1] == 'left':
self.source = 'selectionscreen/jeezyleft1.png'
elif keycode[1] == 'right':
self.source ='selectionscreen/jeezyright1.png'
elif keycode[1] == 'down':
self.source ='selectionscreen/jeezyright1.png'
elif keycode[1] == 'up':
self.source ='selectionscreen/jeezyback2.png'
else:
return False
return True
class gameApp(App):
def build(self):
#instantiate menuscreen class
p = MenuScreen()
clickme = IconButton(source='selectionscreen/presstostart.png')
#clickme = Label(text='Press to Start [ref=Start]Start[/ref]', markup=True)
clickme.bind(sm.switch_to(Game(), direction='right'))
p.add_widget(clickme)
return p
#instantiate gamescreen class
m = Game()
wimg = MoveableImage(source='selectionscreen/jeezyright1.png', anim_available=True, anim_delay=.15)
m.add_widget(wimg)
if __name__ == '__main__':
gameApp().run()
答案 0 :(得分:0)
当你提问时,我在irc上回答了这个问题,但我猜你已断开连接了:
03:51<恶劣>在第12行,您创建了Game类,但是您打算将其称为GameScreen吗?您粘贴的代码可能会崩溃,因为 你引用GameScreen但没有声明它
03:52<恶劣>在第70行,你切换到一个游戏,但这是一个空屏幕,因为它没有行为
03:53<恶劣>如果要切换到第73行创建的属性,请将其“当前”属性设置为“游戏”,不要使用switch_to
此外,您的bind语法错误,当您传递时,调用 switch_to
。使用functools.partial
在此阶段为其提供参数(返回一个自动获取这些参数的新函数)。
我只是快速粘贴这个,因为我只有几分钟,如果不清楚,我会稍后再写。