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 ItemModel: def __init__(self, *sprite, x=0, y=0): self.animation = Sprite(*sprite[0]) self.animation.set_total_duration(1000) self.__setPosition_x = x self.__setPosition_y = y self.animation.set_position(self.__setPosition_x, self.__setPosition_y) self.x = self.animation.x self.y = self.animation.y self.collided = False def collide(self, obj): l2 = Point(self.animation.x, self.animation.y) r2 = Point(self.animation.x + self.animation.width, self.animation.y + self.animation.height) l1 = Point(obj.animation.x + (obj.animation.width * 0.2), obj.animation.y + (obj.animation.height * 0.12)) r1 = Point( obj.animation.x + obj.animation.width - (obj.animation.width * 0.32), obj.animation.y + obj.animation.height - (obj.animation.height * 0.1)) if r1.x < l2.x or r2.x < l1.x: return False if r1.y < l2.y or r2.y < l1.y: return False self.animation.set_position(2000, 2000) self.collided = True return self.collided
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 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 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)