我制作了一个kivy游戏,Enemys在屏幕上运行,应该被玩家杀死。
一切都很好,但是我想给我的敌人添加一把武器,它也必须是一个散子,所以他们可以通过将它们拖走而从敌人手中夺走。
因此,敌人和武器都是分散小部件,并且包含一个超级Image的Sprite。
将武器加入敌人后,游戏仍然有效,但武器未显示。为什么会这样?
这是敌人的阶级:
class Spearman(Enemy):
def __init__(self, **kwargs):
super(Spearman, self).__init__(**kwargs)
self.lay = FloatLayout()
self.add_widget(self.lay)
self.bron = Spear(pos=self.pos)
self.lay.add_widget(self.bron) # the weapon is supposed to be added here
这是它的武器:
class Spear(Scatter):
def __init__(self, **kwargs):
super(Spear, self).__init__(**kwargs)
self.skura = Sprite(source = 'img/spear.png')
这些类是超级类的,它们工作得非常好,并且它们被添加到一个也正常工作的游戏中,所以我把这个代码留了出来。
我也省略了导入命令,因为否则会长。 此外,这是敌人的小部件:
class Enemy(Scatter):
intv = NumericProperty(0/60.)
imgp = StringProperty('img/enemy.png')
speed = NumericProperty(1)
gravity = NumericProperty(0.1)
velocity = NumericProperty()
my_start = NumericProperty()
life = NumericProperty(5)
amor = NumericProperty(0)
damage = NumericProperty(0)
def __init__(self, start=None, **kwargs):
super(Enemy, self).__init__(**kwargs)
self.My_Clock = Clock# declaring the clock for later usage
self.My_Clock.schedule_interval(self.Update, self.intv)
self.pos_hint = None, None
self.my_start = start
self.skura = Sprite(source=self.imgp)
self.add_widget(self.skura)
def Update(self, *args):# the funktion that lets the enemy move
self.x -= self.speed
if self.y > self.my_start:#cecks if the enemy is on ground
self.velocity += self.gravity
self.y -= self.velocity
else:
if self.velocity > self.amor: # checks if amor protects the enemy
self.life -= self.velocity
self.velocity = 0
if self.life <= 0: #checks if dead
self.die()
if self.x < 1:# checks if the enemy widget reached the end
self.na_pierdolic()
#self.My_Clock.unschedule(self.Update)
self.speed = 0#makes the enemy stop moving
if self.parent.zicie > 0:
self.parent.remove_widget(self)
self.My_Clock.unschedule(self.Update)
def pause1(self, *args):# the function that lets the enemy pause
if parent.pu == False:# check if it is paused
self.My_Clock.unschedule(self.Update)
elif parent.pu == True:
self.My_Clock.schedule_interval(self.Update, self.intv1)
def die(self, *args):
self.parent.e_killed += 1
self.parent.zloto += 10
self.parent.remove_widget(self)
self.My_Clock.unschedule(self.Update)
def na_pierdolic(self):
self.parent.zicie -= self.damage
这是关卡小部件:
class level(Widget):
zicie = NumericProperty(10)# the variable containg the life of the player
zloto = NumericProperty(0)
e_killed = NumericProperty(0)
intv1 = NumericProperty(2/1.)
pu = BooleanProperty(True)
e_start = NumericProperty()
def __init__(self, **kwargs):
super(level, self).__init__(**kwargs)
self.l_Clock = Clock
def Update(self, *args):# this funktion generates enemys
estart = random.randint(4, 5)
self.e_start = Window.height/estart
s_start = self.e_start + 50
pos_start = [Window.size[0],s_start]
self.add_widget(Spearman(start=self.e_start, pos=(Window.size[0], self.e_start))) # the widget enemy is added here
self.add_widget(Spear(start=pos_start, pos=pos_start))
if self.zicie < 1:
print 'you lost'
self.l_Clock.unschedule(self.Update)
self.clear_widgets()
def pause(self, *args):
if self.pu == False:
self.pu = True
self.l_Clock.unschedule(self.Update)
self.clear_widgets()
elif self.pu == True:
self.pu = False
self.l_Clock.schedule_interval(self.Update, self.intv1)