def move(self): x, y = self.position yy = min(self.destination[1], y + self.speed) if yy == self.destination[1]: sequences = [Action.move_to(x, yy), Action.call(self.die)] else: sequences = [Action.move_to(x, yy), Action.call(self.move)] self.run_action(Action.sequence(*sequences), 'moving')
def collided_with_enemy(self, other): self.invisible = True other.invisible = True self.remove_all_actions() other.run_action( Action.sequence( Action.call(lambda: Explosion(parent=self.parent, position=other.position)), Action.call(functools.partial(self.kill_enemy, other))))
def add_stars(self, count): path = self.star_path() for _ in range(count): x, y = random.uniform(0, self.size.w), random.uniform(0, self.size.h) star = ShapeNode(path=path, position=(x, y), fill_color='#fff', stroke_color='#fff') star.run_action( A.repeat( A.sequence(A.wait(random.uniform(2, 20)), A.fade_to(0.50, 1), A.fade_to(0.75, 1)), 0)) self.add_child(star)
def fire(self): path = make_path([(0, 0), (0, 1)]) path.line_width = 2 bullet = ShapeNode(path=path, fill_color='clear', stroke_color='white', parent=self) r = self.ship.rotation cannon = Point(math.cos(r), math.sin(r)) * 20 bullet.position = self.ship.position + cannon end = bullet.position + Point(math.cos(r), math.sin(r)) * 350 bullet.run_action(A.sequence(A.move_to(end.x, end.y, 1.5), A.remove())) sound.play_effect('fire.mp3', looping=False, volume=0.0)
def animate_to(self, value, d=0.2): from_value = self.value to_value = value def anim(from_value, to_value, node, p): node.set_value(from_value + p * (to_value - from_value)) animation = Action.call(partial(anim, from_value, to_value), d) self.run_action(animation)
def crash(self): self.thrust_sound.stop() sound.play_effect('explosion_large_distant.mp3') self.fuel = max(self.fuel - 50, 0) # create ship parts prevx, prevy = self.points[0] for x, y in self.points[1:]: p = Part([(prevx, prevy), Point(x, y)]) p.x, p.y, p.r = self.x + (x + prevx) / 2, self.y + ( y + prevy) / 2, self.r prevx, prevy = x, y p.vr = random.uniform(-0.3, 0.3) p.vx = self.vx + random.uniform(-1, 1) p.vy = self.vy * -0.3 + 1 p.run_action(A.sequence(A.fade_to(0.25, 30), A.remove())) self.parent.add_child(p)
def stop(self): if self.state == 'play' and self.effect: self.ramp_start = self.effect.volume self.run_action( A.sequence(A.call(self.ramp, 0.2), A.call(self.done))) self.state = 'stopping'
def touch_moved(self, touch): x, y = touch.location self.player.run_action(Action.move_to(x, self.player.position.y))
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) tex = SpriteNode(explosion, parent=self, x_scale=2, y_scale=2) self.run_action(Action.sequence(Action.wait(0.5), Action.remove()))