我想与一个敌人的班级进行一场游戏。
为了使其更容易理解,我想为我的Enemy
类创建一个名为enemy.py
的文件。
但我收到错误:kivy.factory.FactoryException: Unknown class <Level>
以下是我的主文件的代码:
from enemy import *
class Level(Widget):
def __init__(self, **kwargs):
super(Level, self).__init__(**kwargs)
self.l_Clock = Clock
self.l_Clock.schedule_interval(self.Update, 2/1.)
def Update(self, *args):
self.add_widget(Enemy(pos=(800, 300)))
root = Builder.load_file('main.kv')
class app(App):
def build(self):
Window.clearcolor = (1, 1, 1, 1)
return root
if __name__ == "__main__":
app().run()
这是我的main.kv文件:
FloatLayout:
canvas.before:
Color:
rgba: 1, 1, 1, 1
Level:
id: level
这是我的enemy.py文件:
root = Builder.load_file('main.kv')
class Sprite(Image):
def __init__(self, **kwargs):
super(Sprite, self).__init__(**kwargs)
self.size = self.texture_size
class Enemy(Widget):
droga = StringProperty('feind.png')
velocity = ListProperty([1, 0])
def __init__(self, **kwargs):
super(Enemy, self).__init__(**kwargs)
Clock.schedule_interval(self.Update, 1/60.)
self.skin = Sprite(source=self.droga)
self.add_widget(self.skin)
def Update(self, *args):
self.x -= self.velocity[0]
if self.x < 1:
self.velocity[0] = 0
def on_touch_down(self, touch):
obj = root.ids.level
obj.remove_widget(self)
为什么我的程序找不到Enemy Widget?