Пример #1
0
class Ship:
    def __init__(self,
                 pars,
                 is_kinetic=False,
                 acc=0,
                 ang_acc=0,
                 fr=0,
                 lives=0,
                 life=0):
        self.image = pars["image"]
        self.image_center = pars["info"].get_center()
        self.image_size = pars["info"].get_size()
        radius = pars["info"].get_radius()
        self.up_key_down = False
        self.left_key_down = False
        self.right_key_down = False
        self.missile_image = pars["missile_image"]
        self.missile_info = pars["missile_info"]
        self.missile_sound = pars["missile_sound"]
        self.ship_thrust_sound = pars["ship_thrust_sound"]
        self.missile_group = set([])
        self.lives = lives
        self.life = life
        self.score = 0
        if is_kinetic:
            self.physics = Physics(radius, pars["WIDTH"], pars["HEIGHT"], None,
                                   is_kinetic, fr)
            self.physics.set_ang_acc(ang_acc)
            self.physics.set_acc(acc)
        else:
            self.physics = Physics(radius, WIDTH, HEIGHT)
        self.physics.set_pos(pars["pos"])
        self.physics.set_vel(pars["vel"])
        self.physics.set_ang(pars["ang"])
        self.physics.set_ang_vel(pars["ang_vel"])

    def draw(self, canvas):
        if not self.up_key_down:
            canvas.draw_image(self.image, self.image_center, self.image_size,
                              self.physics.get_pos(), self.image_size,
                              self.physics.get_angle())
            self.ship_thrust_sound.rewind()
        else:
            canvas.draw_image(self.image, [
                self.image_center[0] + self.image_size[0], self.image_center[1]
            ], self.image_size, self.physics.get_pos(), self.image_size,
                              self.physics.get_angle())
            self.ship_thrust_sound.play()

    def get_physics_component(self):
        return self.physics

    def turn(self, ang_acc=0):
        self.physics.add_torque(ang_acc)

    def thrust(self, acc=0):
        self.physics.add_force(acc)

    def process_missile_group(self, canvas):
        SpriteGroup.process_sprite_group(canvas, self.missile_group)
        SpriteGroup.process_lifespan_group(canvas, self.missile_group)

    def shoot(self):
        front = self.get_front()
        pos = self.physics.get_pos()
        vel = self.physics.get_vel()
        self.missile_group.add(
            Sprite([
                pos[0] + self.physics.get_radius() * front[0],
                pos[1] + self.physics.get_radius() * front[1]
            ], [
                vel[0] + front[0] * self.physics.get_radius() / 5,
                vel[1] + front[1] * self.physics.get_radius() / 5
            ], 0, 0, self.missile_image, self.missile_info, self.physics.WIDTH,
                   self.physics.HEIGHT, self.missile_sound))

    def collide_ship_sprites(self,
                             group,
                             explosion_group,
                             explosion_image,
                             explosion_info,
                             ship=None,
                             ship_group=None):
        group_copy = set([])
        aux_copy = group.copy()
        for sprite in aux_copy:
            if self.physics.collide(sprite) is True:
                group_copy.add(sprite)
                explosion = Sprite(sprite.get_physics_component().get_pos(),
                                   [0, 0], 0, 0, explosion_image,
                                   explosion_info, self.physics.WIDTH,
                                   self.physics.HEIGHT)
                explosion_group.add(explosion)
                explosion_group.add(
                    Sprite(self.physics.get_pos(), [0, 0], 0, 0,
                           explosion_image, explosion_info, self.physics.WIDTH,
                           self.physics.HEIGHT))
                if self.life <= 0:
                    self.lives -= 1
                if ship is not None:
                    ship.score += 1

                if ship_group is not None:
                    ship_group.add(self)

        group.difference_update(group_copy)

    def get_front(self):
        return [
            Auxiliars.angle_to_vector(self.physics.get_angle())[0],
            Auxiliars.angle_to_vector(self.physics.get_angle())[1]
        ]

    def get_right(self):
        return [
            Auxiliars.angle_to_vector(self.physics.get_angle() +
                                      math.pi / 2.0)[0],
            Auxiliars.angle_to_vector(self.physics.get_angle() +
                                      math.pi / 2.0)[1]
        ]

    def get_missile_group(self):
        return self.missile_group

    def get_pos(self):
        return self.physics.get_pos()

    def get_acc(self):
        return self.physics.get_acc()

    def get_ang_acc(self):
        return self.physics.get_ang_acc()

    def get_score(self):
        return self.score

    def set_score(self, score):
        self.score = score

    def set_inc_score(self):
        score += 1
Пример #2
0
class Sprite:
    def __init__(self,
                 pos,
                 vel,
                 angle,
                 angle_vel,
                 image,
                 info,
                 WIDTH,
                 HEIGHT,
                 sound=None,
                 is_kinetic=False,
                 acc=0,
                 ang_acc=0,
                 fr=0):
        self.pos = [pos[0], pos[1]]
        self.vel = [vel[0], vel[1]]
        self.angle = angle
        self.angle_vel = angle_vel
        self.image = image
        self.image_center = info.get_center()
        self.image_size = info.get_size()
        radius = info.get_radius()
        self.lifespan = info.get_lifespan()
        self.animated = info.get_animated()
        #self.age = 0
        if sound:
            sound.rewind()
            sound.play()
        if is_kinetic:
            if self.lifespan == None:
                self.physics = Physics(radius, WIDTH, HEIGHT, None, is_kinetic,
                                       fr)
            else:
                self.physics = Physics(radius, WIDTH, HEIGHT, self.lifespan,
                                       is_kinetic, fr)
            self.physics.set_ang_acc(ang_acc)
            self.physics.set_acc(acc)
        else:
            if self.lifespan == None:
                self.physics = Physics(radius, WIDTH, HEIGHT)
            else:
                self.physics = Physics(radius, WIDTH, HEIGHT, self.lifespan)
        self.physics.set_pos(pos)
        self.physics.set_vel(vel)
        self.physics.set_ang(angle)
        self.physics.set_ang_vel(angle_vel)

    def draw(self, canvas):
        if self.animated:
            canvas.draw_image(self.image, [
                self.image_center[0] +
                (self.physics.age * self.image_size[0]), self.image_center[1]
            ], self.image_size, self.pos, self.image_size, self.angle)
        else:
            canvas.draw_image(self.image, self.image_center, self.image_size,
                              self.physics.get_pos(), self.image_size,
                              self.physics.get_angle())

    def get_physics_component(self):
        return self.physics