class EnemyAirPlaneModel(AirPlaneModel): def __init__(self, x, y): self.sprites = [ PLUS_JET_ENEMY_PINK_FLY, PLUS_JET_ENEMY_GREEN_FLY, PLUS_JET_ENEMY_YELLOW_FLY, PLUS_JET_ENEMY_RED_FLY ] super().__init__(x, y, sprite=self.sprites[random.randint(0, 3)]) self.move_plane = 0 self.is_hidden = False self.time = 0 self.lifeModel = EnemyLifeHud(x, y, LIFE_HUD_ENEMY, LIFE_POINTS_ENEMY) self.shotModel = ShotEnemyModel(2000, 2000) def backward(self, fps): self.animation.x += fps def forward(self, fps): self.animation.x -= fps def shot(self, shot=None): if self.shotModel.shotAnimation.x == 2000: shot_x = self.animation.x - 100 shot_y = self.animation.y + (self.animation.height / 2) self.shotModel.set_position(shot_x, shot_y) def hidden(self): self.animation = Sprite(*self.sprites[random.randint(0, 3)]) self.animation.set_loop(True) self.animation.set_total_duration(1000) self.animation.set_position(2000, 2000) self.is_hidden = True self.lifeModel.hidden() self.animation.y = random.randint(0, HEIGHT_SCREEN) def move(self, speed): movement = [self.up, self.down, self.backward, self.forward] if not self.is_hidden: self.time += 1 if self.time % 60 == 0: self.move_plane = random.randint(0, len(movement) - 1) self.time = 0 else: if self.animation.x < WIDTH_SCREEN / 2: self.move_plane = 2 move_to = movement[self.move_plane] move_to(speed) super(EnemyAirPlaneModel, self).move(speed) self.lifeModel.move(self.animation.x + 15, self.animation.y - 15) else: self.forward(speed) if self.animation.x + self.animation.width < WIDTH_SCREEN: self.lifeModel.full_life() self.is_hidden = False def get_life(self): return self.lifeModel def can_shot(self, airplane: AirPlaneModel): if abs(self.animation.y - airplane.animation.y) < 150 and not self.is_hidden: PLANE_LASER_SHOTS.play() self.shot() def get_shot(self): return self.shotModel
class AirPlaneModel(GameObjectInterface): def __init__(self, x=300, y=HEIGHT_SCREEN / 2, sprite=JET_BLUE_FLY, shoot=FIRE_BALL_BLUE): self.animation = Sprite(*sprite) self.animation.set_loop(True) self.animation.set_total_duration(1000) self.animation.set_position(x, y) self.ground_limit = HEIGHT_SCREEN - self.animation.height self.shotModel = ShotModel(2000, 2000) self.shotModel_special = ShotModel(2000, 2000, shoot) def set_special_look(self, sprite: Sprite): self.shotModel_special.shot.animation = sprite def draw(self): self.animation.draw() def update(self): self.animation.update() def move(self, speed): if self.animation.y < 20: self.animation.y = 20 if self.animation.y > self.ground_limit: self.animation.y = self.ground_limit if self.animation.x + self.animation.width > WIDTH_SCREEN: self.animation.x = WIDTH_SCREEN - self.animation.width if self.animation.x < 0: self.animation.x = 0 def up(self, fps): self.animation.y -= fps def down(self, fps): self.animation.y += fps def backward(self, fps): self.animation.x -= fps def forward(self, fps): self.animation.x += fps def shot(self, shot=None): PLANE_LASER_SHOTS.play() if shot is None: shot = self.shotModel if shot.shotAnimation.x == 2000: shot_x = self.animation.x + self.animation.width shot_y = self.animation.y + (self.animation.height / 2) shot.set_position(shot_x, shot_y) def get_shot(self): return self.shotModel def get_shot_special(self): return self.shotModel_special def shot_special(self): self.shot(self.shotModel_special)
class CatModel(GameObjectInterface): def __init__(self): self.ground_limit = GROUND self.animation = Sprite(*CAT_SPRITE_IDLE) self.animation.set_loop(False) self.animation.set_total_duration(1000) self.animation.set_position(0, self.ground_limit) self.x = self.animation.x self.y = self.animation.y self.point = 0 self.antx = 0 self.anty = 0 self.looking_to = True self.jumping = False self.original_y = 0 def jump(self): if self.animation.y == self.ground_limit: self.__set_sprite(CAT_SPRITE_JUMP, CAT_SPRITE_JUMP_FLIPED, self.looking_to, 500) self.original_y = self.animation.y self.jumping = True def idle(self): if self.animation.total_duration == 980 or self.animation.total_duration == 970: self.__set_sprite(CAT_SPRITE_IDLE, CAT_SPRITE_IDLE_FLIPED, self.looking_to, 1000) elif not self.is_playing(): self.__set_sprite(CAT_SPRITE_IDLE, CAT_SPRITE_IDLE_FLIPED, self.looking_to, 1000) def walk(self): if self.animation.total_duration == 1000 or self.animation.total_duration == 970: self.__set_sprite(CAT_SPRITE_WALK, CAT_SPRITE_WALK_FLIPED, True, 980) elif not self.is_playing(): self.__set_sprite(CAT_SPRITE_WALK, CAT_SPRITE_WALK_FLIPED, True, 980) def walk_fliped(self): if self.animation.total_duration == 1000 or self.animation.total_duration == 980: self.__set_sprite(CAT_SPRITE_WALK, CAT_SPRITE_WALK_FLIPED, False, 970) elif not self.is_playing(): self.__set_sprite(CAT_SPRITE_WALK, CAT_SPRITE_WALK_FLIPED, False, 970) def is_playing(self): return self.animation.is_playing() def move(self, speed): if self.jumping: self.animation.move_key_y(speed) self.animation.move_key_x(speed) if self.animation.y < self.ground_limit: self.animation.y += speed * 1.8 if self.animation.y > self.ground_limit: self.animation.y = self.ground_limit if self.animation.x + self.animation.width > WIDTH_SCREEN: self.animation.x = WIDTH_SCREEN - self.animation.width if self.animation.x < 0: self.animation.x = 0 def draw(self): self.animation.draw() def update(self): if self.jumping: self.y -= 2 self.animation.y = self.y if self.original_y - self.y >= JUMP_MAX: self.__set_sprite(CAT_SPRITE_FALL, CAT_SPRITE_FALL_FLIPED, self.looking_to, 500) self.jumping = False self.animation.update() def __set_sprite(self, sprite, sprite_fliped, looking_to, duration=980): self.looking_to = looking_to if self.looking_to: self.__change_sprite(*sprite, total_duration=duration, loop=False) else: self.__change_sprite(*sprite_fliped, total_duration=duration, loop=False) def __change_sprite(self, path, frame, total_duration=980, loop=True): self.x = self.animation.x self.y = self.animation.y self.animation = Sprite(path, frame) self.animation.x = self.x self.animation.y = self.y self.animation.set_total_duration(total_duration) self.animation.set_loop(loop)