Esempio n. 1
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
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()
Esempio n. 3
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
Esempio n. 4
0
 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)
Esempio n. 5
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,
        )
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
Esempio n. 7
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
Esempio n. 8
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)
Esempio n. 9
0
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)
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
Esempio n. 11
0
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
Esempio n. 12
0
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()
Esempio n. 13
0
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')
Esempio n. 14
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)
Esempio n. 15
0
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()
Esempio n. 16
0
 def setTiro(self, inicial_x, inicial_y):
     tiro = Sprite("img/tiro.png")
     tiro.set_position(inicial_x, inicial_y)
     self.tiro.append(tiro)
Esempio n. 17
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)