class BackgroundModel(GameObjectInterface): def __init__(self, sprite, x=0, y=0): self.x = x self.y = y self.background_1 = Sprite(sprite) self.background_1.set_total_duration(1000) self.background_1.set_position(x, y) self.background_2 = Sprite(sprite) self.background_2.set_total_duration(1000) self.background_2.set_position(self.background_1.width, y) def draw(self): self.background_1.draw() self.background_2.draw() def update(self): self.background_1.update() self.background_2.update() def move(self, fps: int): self.background_1.set_position(self.background_1.x - fps, self.y) self.background_2.set_position(self.background_2.x - fps, self.y) if self.background_1.x < -self.background_1.width: self.background_1.x = self.background_1.width if self.background_2.x < -self.background_2.width: self.background_2.x = self.background_2.width if self.background_1.x > 0: self.background_2.x = -self.background_2.width + self.background_1.x if self.background_2.x > 0: self.background_1.x = -self.background_1.width + self.background_2.x
class BattleSceneFinal(BattleSceneFirst): def __init__(self, hud: HudManager): super().__init__(hud) self.background = Sprite(BACKGROUND_WAR) self.background.set_total_duration(1000) self.background.set_position(0, 0) self.enemy_plane_two = EnemyAirPlaneModel(*ENEMY_PLANE_SECCOND_POSITION) self.enemy_plane_three = EnemyAirPlaneModel(*ENEMY_PLANE_THREE_POSITION) self.enemy_plane_four = EnemyAirPlaneModel(*ENEMY_PLANE_FOUR_POSITION) self.game_objects = [self.enemy_plane, self.enemy_plane_two, self.enemy_plane_three, self.enemy_plane_four, self.air_plane, self.coin, self.life, self.special, self.air_plane.get_shot(), self.air_plane.get_shot_special(), self.enemy_plane.get_shot(), self.enemy_plane_two.get_shot(), self.enemy_plane_three.get_shot(), self.enemy_plane_four.get_shot()] self.enemys = [self.enemy_plane, self.enemy_plane_two, self.enemy_plane_three, self.enemy_plane_four] self.enemy_shot_times = [0.0, 0.0, 0.0, 0.0] def handle_event(self, speed, state): super().handle_event(speed, state) self.background.draw() def update(self, state): if not state: return self.background.update() if self.point >= POINTS * 2: self.hud.get_window().main_scene.change_scene('EndGame') for game_object in self.game_objects: game_object.update()
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)