def init_big_meteors(): return [ Meteor( Vec2(randint(0, SIZE.x), -randint(8, SIZE.y)), Vec2(16, 16), SIZE, move_speed=Vec2(0, uniform(0.5, 1.5)) ) for _ in range(BIG_METEOR_COUNT) ]
def init_small_meteors(): return [ Meteor( Vec2(randint(0, SIZE.x), -randint(4, SIZE.y)), Vec2(8, 8), SIZE, move_speed=Vec2(0, uniform(0.5, 1.5)) ) for _ in range(LITTLE_METEOR_COUNT) ]
def __init__(self, position: Vec2, size: Vec2, screen_size: Vec2, move_speed: Vec2): super().__init__(position, size) self.move_speed = move_speed self.velocity = Vec2(0, 0) self.is_active = True self.screen_size = screen_size
def __init__(self, position: Vec2, size: Vec2, player: Player, app): super().__init__(position, size) self.app = app self.is_dead = False self.velocity = Vec2(0, 0) self.player = player self.initial_distance = self.player.position - self.position self.teleport_out_animation = TeleportOut(app=self.app) self.teleport_in_animation = TeleportIn(app=self.app) self.invincibilty_animation = Invincibility(app=self.app, entity=self) self.invincible_frames = 0
def death(self): pyxel.play(0, 3) if self.lives < 1: pyxel.stop() self.game_over = True if self.highscores.check_highscores(self.score): pyxel.play(0, 6) self.highscore_reached = True else: pyxel.play(0, 5) else: self.lives -= 1 self.player.position = SIZE // 2 + Vec2(0, 80) self.player_body.teleport(activated=True) self.player_body.invincible_frames = 300 self.player_body.invincibilty_animation.start() self.player_body.is_dead = False
def increase_difficulty(self): self.app.level.small_meteors = self.app.level.small_meteors + [ Meteor( Vec2(randint(0, SIZE.x), -randint(0, SIZE.y)), Vec2(8, 8), SIZE, move_speed=Vec2(0, uniform(0.5, 1.5)) ) for _ in range(int(self.multiplier * LITTLE_METEOR_COUNT - LITTLE_METEOR_COUNT)) ] self.app.level.big_meteors = self.app.level.big_meteors + [ Meteor( Vec2(randint(0, SIZE.x), -randint(0, SIZE.y)), Vec2(16, 16), SIZE, move_speed=Vec2(0, uniform(0.5, 1.5)) ) for _ in range(int(self.multiplier * BIG_METEOR_COUNT - BIG_METEOR_COUNT)) ]
def init_player(self): self.player = Player(SIZE // 2 + Vec2(0, 80), Vec2(8, 8)) self.player_body = PlayerBody(SIZE // 2 + Vec2(0, 100), Vec2(8, 8), player=self.player, app=self)
import os import string from game.vector import Vec2 ALPHABET = string.ascii_uppercase GAME_NAME = "Shifty Pilot 1: Galactic Apocalypse" SIZE = Vec2(320, 320) ASSETS_PATH = f"{os.getcwd()}/assets/sprites.pyxel" HIGHSCORE_FILEPATH = f"{os.getcwd()}/highscores.json" BUTTON_CONFIG_FILEPATH = f"{os.getcwd()}/button_config.json" START_LIVES = 3 LITTLE_METEOR_COUNT = 10 BIG_METEOR_COUNT = 5 HIGHSCORE_GAME_MODE = True
def __init__(self, position: Vec2, size: Vec2): self.position = position self.size = size self.remain = Vec2(0, 0) self.in_animation = False
class PlayerBody(Actor): MOVEMENT_SPD = 0.5 MAX_DISTANCE = Vec2(60, 60) def __init__(self, position: Vec2, size: Vec2, player: Player, app): super().__init__(position, size) self.app = app self.is_dead = False self.velocity = Vec2(0, 0) self.player = player self.initial_distance = self.player.position - self.position self.teleport_out_animation = TeleportOut(app=self.app) self.teleport_in_animation = TeleportIn(app=self.app) self.invincibilty_animation = Invincibility(app=self.app, entity=self) self.invincible_frames = 0 def velocity_x(self, direction: int): direction = sign(direction) self.velocity.x = direction * self.MOVEMENT_SPD def velocity_y(self, direction: int): direction = sign(direction) self.velocity.y = direction * self.MOVEMENT_SPD def calc_distance(self): return self.player.position - self.position def teleport(self, activated: bool): x = self.player.position.x y = self.player.position.y if activated: pyxel.play(0, 2) self.app.cam_punch = 10 self.teleport_out_animation = TeleportOut(app=self.app, start_pos_x=x, start_pos_y=y, entity=self) self.teleport_out_animation.start() def animate_teleport(self): if self.teleport_out_animation and self.teleport_out_animation.is_active: self.teleport_out_animation.animate() if self.teleport_out_animation.current_phase == self.teleport_out_animation.end_phase: start_x = self.position.x start_y = self.position.y dx = self.player.position.x - self.position.x dy = self.player.position.y - self.position.y x = self.player.position.x y = self.player.position.y self.position.x = x self.position.y = y for t in range(0, 10): t /= 10 self.app.add_particle(start_x + dx * t, start_y + dy * t) self.teleport_in_animation = TeleportIn(app=self.app, start_pos_x=x, start_pos_y=y, entity=self) self.teleport_in_animation.start() if self.teleport_in_animation and self.teleport_in_animation.is_active: self.teleport_in_animation.animate() def animate_invincibility(self): if self.invincibilty_animation and self.invincibilty_animation.is_active: self.invincibilty_animation.animate() def collision(self, projectiles): if self.invincible_frames > 0: self.invincible_frames -= 1 return False for projectile in projectiles: gap = self.position - projectile.position if (abs(gap.x) < self.size.x // 2 + projectile.size.x // 2 and abs(gap.y) < self.size.y // 2 + projectile.size.y // 2): return True return False def update(self, projectiles): if self.collision(projectiles): self.is_dead = True distance = self.calc_distance() self.velocity.x = 0 self.velocity.y = 0 self.player.MOVEMENT_SPD_X = self.player.MOVEMENT_SPD_Y = self.player.INITIAL_MOVEMENT_SPD if distance.x > self.MAX_DISTANCE.x or distance.x < -self.MAX_DISTANCE.x: self.player.MOVEMENT_SPD_X = self.MOVEMENT_SPD self.velocity_x(distance.x) if distance.y > self.MAX_DISTANCE.y or distance.y < -self.MAX_DISTANCE.y: self.player.MOVEMENT_SPD_Y = self.MOVEMENT_SPD self.velocity_y(distance.y) self.move_x(self.velocity.x) self.move_y(self.velocity.y)
def __init__(self, position: Vec2, size: Vec2): super().__init__(position, size) self.velocity = Vec2(0, 0)