Ejemplo n.º 1
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()
Ejemplo n.º 2
0
        counter += 1

        # Shows any changes made
        pygame.display.flip()
        window.update()

    elif game_state == 2:
        # Menu screen

        background.draw()
        if counter < 500:
            # Initially display instructions for some time
            menu_bg.draw()
            window.draw_text(str(score_manager.get_records()),
                             512 - 260,
                             320,
                             color=(255, 255, 255),
                             font_file='font.TTF',
                             size=20)
        else:
            # Then show 'press enter to play' button
            button.draw()
        pygame.display.flip()
        counter += 1

        # If enter is pressed, game starts
        if pygame.key.get_pressed()[pygame.K_RETURN]:
            game_state = 1
            counter = 1  # Time resets

            # Initial graphics
            paintEverything()
Ejemplo n.º 3
0
        bambam.draw()
        pontuador.update()
        if not pontuador.bambam_alive():
            game_state = 1
        pontuador.draw()

    elif game_state == 1:
        if not record_checked:
            if pontuador.get_pontuacao() > score_manager.get_recorde():
                score_manager.set_new_record(pontuador.get_pontuacao())
            record_checked = True

        background.draw()
        arvore.draw()
        tela_final.draw()
        janela.draw_text(str(score_manager.get_recorde()), 260, 205, color=(20, 200, 50), font_file='font.TTF',
                         size=30)
        janela.draw_text(str(pontuador.get_pontuacao()), 260, 240, color=(20, 200, 50), font_file='font.TTF',
                         size=30)
        if teclado.key_pressed('enter'):
            game_state = 0
            arvore = Arvore(janela)
            bambam = BodyBuilder(janela, 'esquerda')
            pontuador = Pontuador(janela)
            record_checked = False

    elif game_state == 2:
        menu_bg.draw()
        botao_inicial.draw()
        janela.draw_text(str(score_manager.get_recorde()), 512 - 56, 274, color=(20, 200, 50), font_file='font.TTF', size=30)
        if teclado.key_pressed('enter'):
            game_state = 0