0
我想用一個敵人小部件製作一個小遊戲,當你點擊它時會消失。 A使用kivy-language將敵人添加到了一個窗口小部件中,並且它工作正常,但我想添加多個enemys並且我不想添加越來越多的Enemys,所以我想使用add_widget命令來控制敵人到地方小部件,但我得到了錯誤:kivy不用kivy語言添加小部件
TypeError: unbound method add_widget() must be called with place instance as first argument (got WidgetMetaclass instance instead)
這裏是源代碼:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.properties import NumericProperty
from kivy.clock import Clock
from kivy.animation import Animation
class place(Widget):
pass
class Enemy(Widget):
velocity = NumericProperty(1)
def __init__(self, **kwargs):
super(Enemy, self).__init__(**kwargs)
Clock.schedule_interval(self.Update, 1/60.)
def Update(self, *args):
self.x -= self.velocity
if self.x < 1:
self.velocity = 0
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
print 'es geht'
self.velocity = 0
self.parent.remove_widget(self)
ROOT = Builder.load_string('''
FloatLayout:
Button:
text: 'Go Back'
size_hint: 0.3, 0.1
pos_hint: {"x": 0, 'y':0}
place:
<place>:
Enemy:
pos: 400, 100
<Enemy>:
Image:
pos: root.pos
id: myimage
source: 'enemy.png'
''')
class Caption(App):
def build(self):
place.add_widget(Enemy)
return ROOT
if __name__ == '__main__':
Caption().run()
謝謝。我這樣做,並得到錯誤錯誤按摩:文件「teil.py」,第52行 返回ROOT ^ SyntaxError:無效的語法 – Gilgamesch 2015-04-02 10:36:19
oops,錯過了上一行的右paren,修復。 – bj0 2015-04-02 16:44:29
謝謝,它的工作,但我該怎麼做,如果我有一個屏幕管理器? – Gilgamesch 2015-04-10 14:19:43