去除带有 kivy 条件的物体粒子

我想要的是两件事:计算屏幕上敌人的数量。并通过一个动作将所有敌人从屏幕上移除。第二个选择是让敌人随机射击


class Enemy(Particle):

    active = False

    tex_name = 'ufo'

    v = 0


    def reset(self, created=False):

        self.active = False

        self.x = -100

        self.y = -100

        self.v = 0


    def advance(self, nap):

        if self.active:

            if self.check_hit():


                self.reset()

                return


            self.x -= 200 * nap

            if self.x < -50:

                self.reset()

                return


            self.y += self.v * nap

            if self.y <= 0:

                self.v = abs(self.v)

            elif self.y >= self.parent.height:

                self.v = -abs(self.v)


        elif self.parent.spawn_delay <= 0:

            self.active = True

            self.x = self.parent.width + 50

            self.y = self.parent.height * random()

            self.v = randint(-100, 100)

            self.parent.spawn_delay += 1


    def check_hit(self):

        if math.hypot(self.parent.player_x - self.x,

                      self.parent.player_y - self.y) < 60:

            return True


        for b in self.parent.bullets:

            if not b.active:

                continue


            if math.hypot(b.x - self.x, b.y - self.y) < 30:

                b.reset()

                return True

我想要的是两件事:计算屏幕上敌人的数量。并通过一个动作将所有敌人从屏幕上移除。第二个选择是让敌人随机射击


慕娘9325324
浏览 109回答 1
1回答

大话西游666

您可以通过修改类来完成前两个问题PSWidget。通过向类添加两个属性enemy_count和:killallclass PSWidget(Widget):&nbsp; &nbsp; indices = []&nbsp; &nbsp; vertices = []&nbsp; &nbsp; particles = []&nbsp; &nbsp; enemy_count = 0&nbsp; &nbsp; killall = False并在方法中使用这些属性update_glsl():def update_glsl(self, nap):&nbsp; &nbsp; self.enemy_count = 0&nbsp; # initialize enemy count&nbsp; &nbsp; for p in self.particles:&nbsp; &nbsp; &nbsp; &nbsp; if isinstance(p, Enemy) and p.active:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.killall:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p.reset()&nbsp; &nbsp; # kill this enemy&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.enemy_count += 1&nbsp; &nbsp; # increment enemy count&nbsp; &nbsp; &nbsp; &nbsp; p.advance(nap)&nbsp; &nbsp; &nbsp; &nbsp; p.update()&nbsp; &nbsp; self.canvas.clear()&nbsp; &nbsp; with self.canvas:&nbsp; &nbsp; &nbsp; &nbsp; Mesh(fmt=self.vfmt, mode='triangles',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;indices=self.indices, vertices=self.vertices,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;texture=self.texture)&nbsp; &nbsp; # reset killall&nbsp; &nbsp; self.killall = False因此,任何时候你想要统计活跃敌人的数量,只需使用实例enemy_count的属性即可PSWidget。每当你想杀死所有活跃的敌人时,只需设置实例killall的属性即可PSWidget。至于你的第三个问题,请向我们展示你对此进行编码的尝试。如果您使用的是我在另一个问题中给出的答案,我建议使用该on_enter()方法来启动游戏更新,那么您可以使用on_leave()它来停止更新。每次进入时游戏动作都会更快,MainScreen因为每次进入都会安排更多更新。MainScreen这是处理该问题的修改:class MainScreen(Screen):&nbsp; &nbsp; started = BooleanProperty(False)&nbsp; &nbsp; def on_enter(self, *args):&nbsp; &nbsp; &nbsp; &nbsp; if not self.started:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.ids.game.initialize()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.started = True&nbsp; &nbsp; &nbsp; &nbsp; self.update_event = Clock.schedule_interval(self.ids.game.update_glsl, 1.0/60.0)&nbsp; &nbsp; def on_leave(self, *args):&nbsp; &nbsp; &nbsp; &nbsp; if self.update_event is not None:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.update_event.cancel()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python