def __init__(self, image_file, grav, grav_acc, jump, frames = 1): Sprite.__init__(self, image_file, frames) self.grav = grav self.grav_acc = grav_acc self.jump = jump self.groundCol = False self.speed = 0
def __init__(self, hud: HudManager): self.hud = hud self.window = hud.get_window() self.mouse = hud.get_window().get_mouse() self.fundo = GameImage(BACKGROUND_HOME) self.time = 0 self.select = False points_background = pygame.transform.scale( GameImage(SOUND_BACKGROUND).image, (1100, 800)) self.point_background = GameImage(SOUND_BACKGROUND) self.point_background.image = points_background self.point_background.set_position( self.window.width / 2 - points_background.get_width() / 2, 0) self.text = CenterText(hud.get_window(), WIDTH_DIV, 300, text="Escolha qual será sua próxima nave") self.jet_g_bool = False self.jet_green = Sprite(*JET_GREEN_FLY) self.jet_green.set_position( WIDTH_DIV - WIDTH_DIV / 2 - self.jet_green.width + 200, 350) self.jet_y_bool = False self.jet_yellow = Sprite(*JET_YELLOW_FLY) self.jet_yellow.set_position(WIDTH_DIV + WIDTH_DIV / 2 - 200, 350)
class SpecialHud: def __init__(self): self.bar = GameImage(SPECIAL_HUD) self.bar.set_position(4, 76) self.value = Sprite(*SPECIAL_POINTS) self.value.set_position(-6, 65) self.value.set_curr_frame(0) def draw(self): self.bar.draw() self.value.draw() def addSpecial(self): point = self.value.curr_frame + 1 self.value.set_curr_frame(self.__checkLife(point)) def loseSpecial(self): self.value.set_curr_frame(0) def current_special(self): return self.value.curr_frame def __checkLife(self, point): if point < 0: return 0 elif point > 4: return 4 else: return point
def setSprite(self, name): if name is None: self.sprite = None self.size = Vector2.zero() else: self.sprite = Sprite(name + ".png") self.size = Vector2(self.sprite.width, self.sprite.height)
def spawn(self, initial_x, initial_y, was_alien=False): bullet = Sprite("./assets/actors/bullet.png") bullet.set_position(initial_x - bullet.width / 2, initial_y - bullet.height) if was_alien == False: self.bullets.append(bullet) else: self.bullets_alien.append(bullet)
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 __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)
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 __init__(self, bar_x=4, bar_y=0, life_hud=LIFE_HUD, life_points=LIFE_POINTS): self.bar = GameImage(life_hud) self.bar.set_position(bar_x, bar_y) self.value = Sprite(*life_points) self.value.set_position(bar_x - 10, bar_y - 10) self.value.set_curr_frame(4)
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 __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 __init__(self, window, bullet, alien, multiplier=1): self.window = window self.bullet = bullet self.alien = alien self.sprite = Sprite("./assets/actors/spaceship.png") self.player_was_shot = False self.speed = globals.SPACESHIP_VEL self.reload_cron = 0 self.reload_time = globals.RELOAD_TIME * multiplier self.__set_pos()
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()
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 __draw_jet(self, jet: Sprite, enabled: bool): if enabled: if self.time >= 0.0: self.time -= self.window.delta_time() if 1.7 <= self.time <= 2.0: jet.draw() elif 1.1 <= self.time <= 1.4: jet.draw() elif 0.5 <= self.time <= 0.8: jet.draw() elif self.time <= 0.0: jet.draw() else: jet.draw()
class GameObject(object): def __init__(self): self.position = Vector2.zero() self.size = Vector2.zero() self.velocity = Vector2.zero() self.sprite = None self.destroyed = False return def base(self): GameObject.__init__(self) return def __str__(self): return str(self.__class__.__name__) def setSprite(self, name): if name is None: self.sprite = None self.size = Vector2.zero() else: self.sprite = Sprite(name + ".png") self.size = Vector2(self.sprite.width, self.sprite.height) def onUpdate(self): return def onDraw(self): return def baseUpdate(self): self.onUpdate() self.position = self.position.sumWith(self.velocity) return def baseDraw(self): if self.sprite is not None: self.sprite.set_position(self.position.x, self.position.y) self.sprite.draw() self.onDraw() return def destroy(self): self.destroyed = True Game.activeScene.toDestroy.append(self) return
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]
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
def setBotoes(self): #'Nesta parte vamos criar todos os sprites e definir suas posições na janela' self.play = Sprite("img/PLAY.png") self.play.set_position(((2 * globais.WIDTH / 5) - self.play.width), ((globais.HEIGHT / 2) - self.play.height)) self.dificuldade = Sprite("img/DIFICULDADE.png") self.dificuldade.set_position( ((4 * globais.WIDTH / 5) - self.play.width), ((globais.HEIGHT / 2) - self.play.height)) self.rank = Sprite("img/RANK.png") self.rank.set_position(((2 * globais.WIDTH / 5) - self.rank.width), ((globais.HEIGHT / 2) + self.rank.height)) self.quit = Sprite("img/QUIT.png") self.quit.set_position(((4 * globais.WIDTH / 5) - self.quit.width), ((globais.HEIGHT / 2) + self.quit.height))
def __init__(self, amount=1, x=0, y=0, z=1): self.z = z if amount == 1: Sprite.__init__(self, 'assets/hud/floor_heart_1.png') if amount == 2: Sprite.__init__(self, 'assets/hud/floor_heart_2.png') if amount >= 3: Sprite.__init__(self, 'assets/hud/floor_heart_3.png') self.x = x self.y = y self.amount = amount
def setBotoes(self): self.facil = Sprite("img/FACIL.png") self.facil.set_position((globais.WIDTH / 2) - (self.facil.width / 2), (3 * (globais.HEIGHT / 7))) self.medio = Sprite("img/MEDIO.png") self.medio.set_position((globais.WIDTH / 2) - (self.medio.width / 2), (4 * (globais.HEIGHT / 7))) self.dificil = Sprite("img/DIFICIL.png") self.dificil.set_position( (globais.WIDTH / 2) - (self.dificil.width / 2), (5 * (globais.HEIGHT / 7)))
class SpaceShip(object): def __init__(self, window, bullet, alien, multiplier=1): self.window = window self.bullet = bullet self.alien = alien self.sprite = Sprite("./assets/actors/spaceship.png") self.player_was_shot = False self.speed = globals.SPACESHIP_VEL self.reload_cron = 0 self.reload_time = globals.RELOAD_TIME * multiplier self.__set_pos() def update(self): if keyboard.key_pressed("left") and self.sprite.x > globals.SCREEN_BORDER: self.sprite.x -= self.speed * self.window.delta_time() if ( keyboard.key_pressed("right") and self.sprite.x + self.sprite.width < self.window.width - globals.SCREEN_BORDER ): self.sprite.x += self.speed * self.window.delta_time() if keyboard.key_pressed("space"): if self.reload_cron <= 0: self.bullet.spawn( self.sprite.x + self.sprite.width / 2, self.sprite.y + 5 ) self.reload_cron = self.reload_time for bullet in self.bullet.bullets_alien: if self.sprite.collided(bullet): self.bullet.bullets_alien.remove(bullet) self.player_was_shot = True for ali in self.alien.aliens: if self.sprite.collided(ali): globals.GAME_STATE = 0 globals.PLAY_INIT = True break self.reload_cron -= 1 self.sprite.draw() def __set_pos(self): self.sprite.set_position( self.window.width / 2 - self.sprite.width / 2, self.window.height - self.sprite.height - 55, )
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 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 Menu(object): def __init__(self, window): self.window = window #Aqui vamos declarar que a variavel window criada no main.py será enviada ao menu.py, de modo que ela seja igual a janela criada def setMenu(self): #'Nesta parte irá rodar a página menu, teremos outras defs para criar e desenhar os Sprites, mas é nesta def que vamos colocar o que realmente deve rodar no programa' self.setBotoes() self.setDraw() self.setClique() self.setReturn() def setBotoes(self): #'Nesta parte vamos criar todos os sprites e definir suas posições na janela' self.play = Sprite("img/PLAY.png") self.play.set_position(((2 * globais.WIDTH / 5) - self.play.width), ((globais.HEIGHT / 2) - self.play.height)) self.dificuldade = Sprite("img/DIFICULDADE.png") self.dificuldade.set_position( ((4 * globais.WIDTH / 5) - self.play.width), ((globais.HEIGHT / 2) - self.play.height)) self.rank = Sprite("img/RANK.png") self.rank.set_position(((2 * globais.WIDTH / 5) - self.rank.width), ((globais.HEIGHT / 2) + self.rank.height)) self.quit = Sprite("img/QUIT.png") self.quit.set_position(((4 * globais.WIDTH / 5) - self.quit.width), ((globais.HEIGHT / 2) + self.quit.height)) def setDraw(self): #'Nesta parte vamos desenhar todos os sprites que criamos no setBotoes(), mas antes temos que chamar o setBotoes dentro do setDraw para que ele crie as Sprites' self.window.draw_text("Space Invaders", globais.WIDTH / 4, globais.HEIGHT / 4, size=60, color=(255, 255, 255), font_name="Arial", bold=True, italic=False) self.play.draw() self.dificuldade.draw() self.rank.draw() self.quit.draw() def setClique(self): self.mouse = Mouse() if (self.mouse.is_over_object(self.play)): if (self.mouse.is_button_pressed(1)): globais.PAGINA_ATUAL = 1 if (self.mouse.is_over_object(self.dificuldade)): if (self.mouse.is_button_pressed(1)): globais.PAGINA_ATUAL = 2 if (self.mouse.is_over_object(self.rank)): if (self.mouse.is_button_pressed(1)): print("RANK") if (self.mouse.is_over_object(self.quit)): if (self.mouse.is_button_pressed(1)): globais.JOGO_RODANDO = False def setReturn(self): self.teclado = Keyboard() if (self.teclado.key_pressed("ESC")): globais.JOGO_RODANDO = False
class Dificuldade(object): def __init__(self, window): self.window = window def setDificuldade(self): self.setBotoes() self.setDraw() self.setClique() self.setReturn() def setBotoes(self): self.facil = Sprite("img/FACIL.png") self.facil.set_position((globais.WIDTH / 2) - (self.facil.width / 2), (3 * (globais.HEIGHT / 7))) self.medio = Sprite("img/MEDIO.png") self.medio.set_position((globais.WIDTH / 2) - (self.medio.width / 2), (4 * (globais.HEIGHT / 7))) self.dificil = Sprite("img/DIFICIL.png") self.dificil.set_position( (globais.WIDTH / 2) - (self.dificil.width / 2), (5 * (globais.HEIGHT / 7))) def setDraw(self): self.window.draw_text("Escolha a dificuldade:", (3 * globais.WIDTH / 9), (2 * (globais.HEIGHT / 7)), size=30, color=(255, 255, 255), font_name="Arial", bold=True, italic=False) self.facil.draw() self.medio.draw() self.dificil.draw() def setClique(self): self.mouse = Mouse() if (self.mouse.is_over_object(self.facil)): if (self.mouse.is_button_pressed(1)): globais.DIFICULDADE = 1 globais.PAGINA_ATUAL = 0 if (self.mouse.is_over_object(self.medio)): if (self.mouse.is_button_pressed(1)): globais.DIFICULDADE = 2 globais.PAGINA_ATUAL = 0 if (self.mouse.is_over_object(self.dificil)): if (self.mouse.is_button_pressed(1)): globais.DIFICULDADE = 3 globais.PAGINA_ATUAL = 0 def setReturn(self): self.teclado = Keyboard() if (self.teclado.key_pressed("ESC")): globais.PAGINA_ATUAL = 0 self.window.delay(100)
def setParadoE(self): self.parado_e = Sprite("img/parado_e2.png") self.personagem = self.parado_e
def setAbaixadoD(self): self.abaixado_d = Sprite("img/abaixado_d.png") self.personagem = self.abaixado_d
def setAbaixadoE(self): self.abaixado_e = Sprite("img/abaixado_e.png") self.personagem = self.abaixado_e
class SelectPlaneScene(SceneInterface): def __init__(self, hud: HudManager): self.hud = hud self.window = hud.get_window() self.mouse = hud.get_window().get_mouse() self.fundo = GameImage(BACKGROUND_HOME) self.time = 0 self.select = False points_background = pygame.transform.scale( GameImage(SOUND_BACKGROUND).image, (1100, 800)) self.point_background = GameImage(SOUND_BACKGROUND) self.point_background.image = points_background self.point_background.set_position( self.window.width / 2 - points_background.get_width() / 2, 0) self.text = CenterText(hud.get_window(), WIDTH_DIV, 300, text="Escolha qual será sua próxima nave") self.jet_g_bool = False self.jet_green = Sprite(*JET_GREEN_FLY) self.jet_green.set_position( WIDTH_DIV - WIDTH_DIV / 2 - self.jet_green.width + 200, 350) self.jet_y_bool = False self.jet_yellow = Sprite(*JET_YELLOW_FLY) self.jet_yellow.set_position(WIDTH_DIV + WIDTH_DIV / 2 - 200, 350) def handle_event(self, speed: int, scene: bool): if self.mouse.is_over_object(self.jet_green): self.jet_g_bool = True self.jet_y_bool = False if self.mouse.is_button_pressed(mouse.BUTTON_LEFT): self.hud.set_ship_look(JET_GREEN_FLY) self.select = True elif self.mouse.is_over_object(self.jet_yellow): self.jet_y_bool = True self.jet_g_bool = False if self.mouse.is_button_pressed(mouse.BUTTON_LEFT): self.hud.set_ship_look(JET_YELLOW_FLY) self.select = True else: self.time = 2 self.jet_g_bool = False self.jet_y_bool = False def __draw_jet(self, jet: Sprite, enabled: bool): if enabled: if self.time >= 0.0: self.time -= self.window.delta_time() if 1.7 <= self.time <= 2.0: jet.draw() elif 1.1 <= self.time <= 1.4: jet.draw() elif 0.5 <= self.time <= 0.8: jet.draw() elif self.time <= 0.0: jet.draw() else: jet.draw() def draw(self, state: bool): self.fundo.draw() self.point_background.draw() self.__draw_jet(self.jet_yellow, self.jet_y_bool) self.__draw_jet(self.jet_green, self.jet_g_bool) self.text.draw() def update(self, state: bool): if self.select: self.window.main_scene.change_scene('Desert')