问题描述
我正在尝试使用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()
1楼
当您询问时,我在irc上回答了此问题,但我想您已断开连接:
03:51 <inclement>在第12行上,您创建了Game类,但是您打算将其称为GameScreen吗? 您引用的GameScreen但未声明时,粘贴的代码可能会崩溃
03:52 <inclement>在第70行,您切换到游戏,但这是一个空白屏幕,因为它没有任何行为
03:53 <inclement>如果要切换到在第73行创建的那个,请将其“当前”属性设置为“游戏”, 请不要使用switch_to
另外,bind的语法错误,应该在传递 switch_to
时调用它。
在此阶段使用functools.partial
为其提供参数(返回一个自动获取这些参数的新函数)。
我只是快速粘贴一下,因为我只有几分钟的时间,如果不清楚,我会在以后再写。