コード例 #1
0
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()
コード例 #2
0
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
コード例 #3
0
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
コード例 #4
0
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,
        )
コード例 #5
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()
コード例 #6
0
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
コード例 #7
0
class LifeHud:
    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 draw(self):
        self.bar.draw()
        self.value.draw()

    def add_life(self):
        point = self.value.curr_frame + 1
        self.value.set_curr_frame(self.__checkLife(point))

    def full_life(self):
        self.value.set_curr_frame(4)

    def lose_life(self):
        point = self.value.curr_frame - 1
        if point >= 0:
            self.value.set_curr_frame(self.__checkLife(point))
        return point

    def is_empty_life(self):
        return self.value.curr_frame == 0

    def empty_life(self):
        self.value.set_curr_frame(0)

    def __checkLife(self, point):
        if point < 0:
            return 0
        elif point > 4:
            return 4
        else:
            return point

    def move(self, x, y):
        self.bar.set_position(x, y)
        self.value.set_position(x - 10, y - 10)
コード例 #8
0
ファイル: dificuldade.py プロジェクト: bdsp59/SpaceInvadors
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)
コード例 #9
0
ファイル: menu.py プロジェクト: bdsp59/SpaceInvadors
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
コード例 #10
0
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)
コード例 #11
0
ファイル: main.py プロジェクト: geraldofada/gamelab
def main():
    HEIGHT = 600
    WIDTH = 800
    VEL_X = 200
    VEL_Y = 200

    window = Window(WIDTH, HEIGHT)
    keyboard = Keyboard()

    ball = Sprite("./assets/ball.png")
    ball.set_position((window.width / 2 - ball.width / 2),
                      (window.height / 2 - ball.height / 2))

    bat_player = Sprite("./assets/bat.png")
    bat_player.set_position(15, (window.height / 2 - bat_player.height / 2))

    bat_npc = Sprite("./assets/bat.png")
    bat_npc.set_position((window.width - 15) - bat_npc.width,
                         (window.height / 2 - bat_npc.height / 2))

    game_started = False
    player_score = 0
    npc_score = 0

    window.set_background_color((0, 0, 0))
    window.draw_text("{} {}".format(player_score, npc_score), window.width / 2,
                     15, 25, (255, 255, 255))
    ball.draw()
    bat_player.draw()
    bat_npc.draw()

    while True:
        # to start the game
        if keyboard.key_pressed("space") or game_started:
            ball.x += (VEL_X * window.delta_time())
            ball.y += (VEL_Y * window.delta_time())
            game_started = True

        # scoring and reset after left and right window collision
        if (ball.x < 0) or (ball.x + ball.width > window.width):
            if ball.x < 0:
                npc_score += 1
            elif ball.x + ball.width > window.width:
                player_score += 1

            ball.set_position((window.width / 2 - ball.width / 2),
                              (window.height / 2 - ball.height / 2))
            bat_player.set_position(
                15, (window.height / 2 - bat_player.height / 2))
            bat_npc.set_position((window.width - 15) - bat_npc.width,
                                 (window.height / 2 - bat_npc.height / 2))

            game_started = False

            window.set_background_color((0, 0, 0))
            window.draw_text("{} {}".format(player_score, npc_score),
                             window.width / 2, 15, 25, (255, 255, 255))
            ball.draw()
            bat_player.draw()
            bat_npc.draw()
            window.update()
            continue

        # bottom and top window collision
        if ball.y < 0:
            VEL_Y *= -1
            ball.set_position(ball.x, ball.y + 1)
        elif ball.y + ball.height > window.height:
            VEL_Y *= -1
            ball.set_position(ball.x, ball.y - 1)

        # bat collision
        if bat_player.collided(ball):
            VEL_X *= -1
            ball.set_position(ball.x + 1, ball.y)
        elif bat_npc.collided(ball):
            VEL_X *= -1
            ball.set_position(ball.x - 1, ball.y)

        # controls
        if keyboard.key_pressed("up") and bat_player.y > 15:
            bat_player.y -= 0.5
        if (keyboard.key_pressed("down")
                and (bat_player.y + bat_player.height < window.height - 15)):
            bat_player.y += 0.5

        # AI
        if (ball.y < window.height / 2 and bat_npc.y > 15 and game_started):
            bat_npc.y -= 0.5
        elif (ball.y > window.height / 2
              and bat_npc.y + bat_npc.height < window.height - 15
              and game_started):
            bat_npc.y += 0.5

        # reset
        window.set_background_color((0, 0, 0))
        window.draw_text("{} {}".format(player_score, npc_score),
                         window.width / 2, 15, 25, (255, 255, 255))
        ball.draw()
        bat_player.draw()
        bat_npc.draw()
        window.update()
コード例 #12
0
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)
コード例 #13
0
ファイル: play.py プロジェクト: bdsp59/SpaceInvadors
class Play(object):
    def __init__(self, window):
        self.window = window
        self.setNave()
        self.tie = []
        self.linha = []
        self.setInimigos()
        self.setLinhaInimigos()
        self.tiro = []
        self.cronometro = 0

    def setPlay(self):
        self.setDraw()
        self.setMovimento()
        self.setReturn()
        self.setTiroDado()
        self.getInimigos()

    def setNave(self):
        self.nave = Sprite("img/ywing.png")
        self.nave.set_position((globais.WIDTH / 2) - self.nave.width / 2,
                               (globais.HEIGHT) - self.nave.height)

    def setMovimento(self):
        teclado = Keyboard()
        if (teclado.key_pressed("RIGHT")
                and (self.nave.x + self.nave.width - 15) < globais.WIDTH):
            self.nave.x += (globais.VEL_JOGADOR * self.window.delta_time())
        if (teclado.key_pressed("LEFT") and (self.nave.x + 15) > 0):
            self.nave.x -= (globais.VEL_JOGADOR * self.window.delta_time())
        if (teclado.key_pressed("SPACE")):
            if (self.cronometro <= 0):
                self.setTiro(self.nave.x + self.nave.width / 2,
                             self.nave.y + 20)
                self.cronometro = globais.RECARGA
        self.cronometro -= 1

    def setDraw(self):
        self.nave.draw()

    def setInimigos(self):
        self.tieU = Sprite("img/tie.png")

    def setLinhaInimigos(self):
        i = 0
        j = 0
        for i in range(1):
            for j in range(10):
                self.tieU.set_position(self.tieU.width * j, 10)
                self.tie.append(self.tieU)
            self.linha.append(self.tie)
            self.tie = []

    def getInimigos(self):
        for i in range(len(self.tie)):
            self.linha[i].draw()

    def setReturn(self):
        self.teclado = Keyboard()
        if (self.teclado.key_pressed("ESC")):
            globais.PAGINA_ATUAL = 0
        self.window.delay(100)

    def setTiro(self, inicial_x, inicial_y):
        tiro = Sprite("img/tiro.png")
        tiro.set_position(inicial_x, inicial_y)
        self.tiro.append(tiro)

    def setTiroDado(self):
        if len(self.tiro) > 0:
            if self.tiro[-1].y + self.tiro[-1].height < 0:
                self.tiro = []
        for i in range(len(self.tiro)):
            self.tiro[i].y -= globais.VEL_TIRO * self.window.delta_time()
            self.tiro[i].draw()