Ejemplo n.º 1
0
def main():
    """
    The main function. Welcome to the beginning of the end.
    """
    # start pygame init
    pygame.init()
    game_clock = pygame.time.Clock()

    settings = settings_parse.parse_settings("../config/config.cfg")

    print settings

    font = pygame.font.SysFont(None, 48)

    ui_tree = ET.parse("../config/ui_data.xml")
    ui_root = ui_tree.getroot()
    ui_ingame = ui_root.find("ingame")
    ui_inmenu = ui_root.find("inmenu")

    # Bootstrap the GUI

    window_details = ui_root.find("window")
    name = window_details.get("label")
    window_x = int(window_details.find("width").text)
    window_y = int(window_details.find("height").text)

    # Full screen size
    # window_x, window_y = 1920, 1080

    window_surface = pygame.display.set_mode((window_x, window_y), 0, 32)

    pygame.display.set_caption(name)

    BLACK = (0, 0, 0)

    game_object = game.Game(ui_root)

    while True:

        # This whole while loops in while loops may be better as a set of ifs
        # you can then control the frame rate with one function

        game_object.handle_events(pygame.event.get())

        # game_object.update()

        for inter in game_object.state.interface_list:
            window_surface.blit(inter.surface, (0, 0))

        draw.draw_fps(window_surface, game_clock.get_fps())
        pygame.display.update()
        game_clock.tick()

        window_surface.fill(BLACK)

        # End of main game loop

    # End of main function
    return True
Ejemplo n.º 2
0
def main():
    """
    The main game function. Welcome to the beginning of the end.
    :return:
    """

    # First we must initialize the pygame module and game clock before most things happen
    pygame.init()
    game_clock = pygame.time.Clock()

    # Grab the settings from the settings file. They are placed in a dictionary.
    settings = settings_parse.parse_settings('settings.cfg')

    window_name = 'Pong'
    window_resolution = settings['x_resolution'], settings['y_resolution']

    # Check to see if the game should run in full screen
    flags = 0
    if settings['full_screen']:
        flags = pygame.FULLSCREEN

    # Build the main game window according to the settings provided
    pygame.display.set_caption(window_name)
    window = pygame.display.set_mode(window_resolution, flags, 32)
    window_rect = window.get_rect()

    # Get the joysticks/gamepads connected
    joysticks = [pygame.joystick.Joystick(x) for x in range(pygame.joystick.get_count())]
    for joystick in joysticks:
        joystick.init()

    # Create the game and score area
    # The score area is along the top with a height 1/10th of the window
    score_height = window_rect.height / 10
    game_height = window_rect.height - score_height
    score_surface = pygame.Surface((window_rect.width, score_height))
    game_surface = pygame.Surface((window_rect.width, game_height))

    game_resolution = (settings['x_resolution'], game_height)

    # Font used for FPS counter
    fps_font = pygame.font.SysFont(None, 48)

    # Font used for score
    score_font = pygame.font.SysFont(None, score_height - 4)

    # The number of players in the game
    number_players = 2

    # Determine the players of the game
    if settings['player_one'] == 'human':

        pass

    else:

        pass

    if settings['player_two'] == 'human':

        pass

    else:

        pass

    # Create the starting game objects
    balls = [Ball(10, 200, window.get_rect().center)]
    players = [Player(0, (0, window.get_rect().centery - 50)),
               skynet.AiPlayer(0, (window.get_rect().right - 10, window.get_rect().centery - 50))]
    winner = players[1]

    # Initialize the variable game loop time step
    last_time = time.time()

    while True:

        # The heart of the variable game loop. Keeps track of how long a frame takes to complete
        current_time = time.time()
        time_elapsed = current_time - last_time
        last_time = current_time

        # Game clock used for fps calculation
        game_clock.tick()

        # Handle the pygame events
        # TODO: Move this to a more sensible place
        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                pygame.quit()

            if event.type == pygame.KEYDOWN:

                if event.key == pygame.K_ESCAPE:
                    pygame.quit()

                elif event.key == pygame.K_UP:

                    [player.paddle.move_up() for player in players]

                elif event.key == pygame.K_DOWN:

                    [player.paddle.move_down() for player in players]

                elif event.key == pygame.K_SPACE:

                    balls.append(Ball(10, 200, window.get_rect().center))

            if event.type == pygame.KEYUP:

                [player.paddle.stop() for player in players]

            if (event.type == pygame.JOYAXISMOTION) and (event.axis == 1):

                players[event.joy].paddle.direction = -event.value

            if (event.type == pygame.JOYBUTTONUP) and (event.button == 0):

                player_paddle = players[event.joy].paddle

                ball = Ball(10, 200, player_paddle.position)
                ball.direction = Direction(True, event.joy)
                ball.position = [player_paddle.get_front(), player_paddle.get_center()]
                if not event.joy:
                    ball.position[0] = player_paddle.get_front() + player_paddle.size[0]
                balls.append(ball)

        # Update game objects
        for player in players:
            player.paddle.update(time_elapsed, game_resolution)

            for ball in balls:

                if player.paddle.collision(ball):
                    ball.paddle_bounce()

                if not (ball.update(time_elapsed, game_resolution, players)):

                    balls.remove(ball)

        if not balls:

            balls.append(Ball(10, 200, game_surface.get_rect().center))

        # Run AI
        for player in players:

            try:
                player.target_ball(balls)
                player.update()
            except AttributeError:
                pass

        # Render all game objects
        window.fill((0, 0, 0))

        # Draw the playing field
        board_colour = (232, 221, 203)
        line_colour = (205, 179, 128)
        width = game_surface.get_rect().width
        height = game_surface.get_rect().height
        center_x = game_surface.get_rect().centerx
        center_y = game_surface.get_rect().centery
        game_surface.fill(board_colour)
        pygame.draw.line(game_surface, line_colour, (0, 0), (width, 0), 4)
        pygame.draw.line(game_surface, line_colour, (width - 2, 0), (width - 2, height), 4)
        pygame.draw.line(game_surface, line_colour, (0, 0), (0, height), 4)
        pygame.draw.line(game_surface, line_colour, (0, height - 2), (width, height - 2), 4)
        pygame.draw.line(game_surface, line_colour, (center_x, 0), (center_x, height), 2)
        gfxdraw.aacircle(game_surface, center_x, center_y, 100, line_colour)
        gfxdraw.aacircle(game_surface, center_x, center_y, 101, line_colour)

        for ball in balls:
            game_surface.blit(ball.surface, ball.position)

        for player in players:
            game_surface.blit(player.paddle.surface, player.paddle.position)

        window.blit(game_surface, (0, score_height))

        # Draw The score
        score_string = str(players[0].score) + '   ' + str(players[1].score)
        score = score_font.render(score_string, True, board_colour, (0, 0, 0))
        score_surf_rect = score_surface.get_rect()
        score_rect = score.get_rect()
        score_surface.blit(score, (score_surf_rect.centerx - score_rect.centerx,
                                   score_surf_rect.centery - score_rect.centery))
        pygame.draw.line(score_surface, line_colour, (score_surf_rect.centerx, 0),
                         (score_surf_rect.centerx, score_surf_rect.height), 2)
        window.blit(score_surface, (0, 0))

        if settings['show_fps']:
            fps = fps_font.render(str(int(game_clock.get_fps())), True, (255, 255, 255), (0, 0, 0))
            window.blit(fps, (0, 0))

        # Update the display
        pygame.display.update()
Ejemplo n.º 3
0
def main():

    """
    The main game function. Welcome to the beginning of the end.
    :return:
    """

    # First we must initialize the pygame module and game clock before most things happen
    pygame.init()
    game_clock = pygame.time.Clock()

    # Grab the settings from the settings file. They are placed in a dictionary.
    settings = settings_parse.parse_settings('settings.cfg')

    window_name = 'Taniwha'
    window_resolution = settings['x_resolution'], settings['y_resolution']

    # Check to see if the game should run in full screen
    flags = 0
    if settings['full_screen']:
        flags = pygame.FULLSCREEN

    # Build the main game window according to the settings provided
    pygame.display.set_caption(window_name)
    window = pygame.display.set_mode(window_resolution, flags, 32)

    try:
        joystick = pygame.joystick.Joystick(0)
        joystick.init()
    except pygame.error:
        joystick = None

    world_limits = Position(settings['x_resolution'], settings['y_resolution'])

    # Init world objects
    taniwha = Taniwha(world_limits)
    number_fish = settings['number_fish']
    number_divers = settings['number_divers']

    bubbles = []
    flash = []

    def create_bubble(pos):

        bubbles.append(Bubble(pos))

    def create_flash(pos):

        flash.append(Flash(pos))

    fish = [Fish(world_limits, create_bubble, True) for i in range(number_fish)]
    divers = [Diver(world_limits, create_bubble, create_flash) for i in range(number_divers)]

    background = Background(world_limits)
    menu_background = MenuBackground(world_limits)

    energy_bar = EnergyBar(world_limits)

    # Bubble particle check
    bubble_check = time.time()

    # Initialize the variable game loop time step
    last_time = time.time()

    in_menu = True
    in_game = False

    while True:

        while in_menu:

            current_time = time.time()
            time_elapsed = current_time - last_time
            last_time = current_time

            window.blit(menu_background.menu_surface, (0, 0))

            pygame.display.update()

            for event in pygame.event.get():

                if event.type == pygame.QUIT:
                    pygame.quit()

                if event.type == pygame.KEYDOWN:

                    if event.key == pygame.K_ESCAPE:
                        pygame.quit()

                    else:
                        in_menu = False
                        in_game = True

        while in_game:

            # The heart of the variable game loop. Keeps track of how long a frame takes to complete
            current_time = time.time()
            time_elapsed = current_time - last_time
            last_time = current_time

            window.blit(background.surface, (0, 0))

            keys = pygame.key.get_pressed()
            for event in pygame.event.get():

                if event.type == pygame.QUIT:
                    pygame.quit()

                if event.type == pygame.KEYDOWN:

                    if event.key == pygame.K_ESCAPE:
                        in_game = False
                        in_menu = True

                if event.type == pygame.JOYAXISMOTION:

                    taniwha.joystick = joystick

                if event.type == pygame.MOUSEMOTION:

                    taniwha.joystick = None

            # Update game objects
            taniwha.update(time_elapsed, keys)
            taniwha.update_focal(time_elapsed)

            fish_copy = fish

            for f in fish:
                f.update(time_elapsed, taniwha)

            for f in fish_copy:

                if point_outside(f.position, world_limits):
                    fish.remove(f)

            if len(fish) < number_fish:
                fish.append(Fish(world_limits, create_bubble))

            divers_copy = divers

            for diver in divers:
                diver.update(time_elapsed, taniwha)

            for diver in divers_copy:
                if diver.health_points < 0:
                    divers.remove(diver)

                if point_outside(diver.position, world_limits):
                    divers.remove(diver)
                    in_game = False
                    in_menu = True

            if len(divers) < number_divers:
                divers.append(Diver(world_limits, create_bubble, create_flash))

            # Update Bubbles
            bubbles_copy = bubbles

            for bubble in bubbles:
                bubble.update(time_elapsed)

            for bubble in bubbles_copy:
                if bubble.position.y < 0:
                    bubbles.remove(bubble)

            # Update flash
            flash_copy = flash

            for f in flash:
                f.update(time_elapsed)

            for f in flash_copy:
                if f.time_left < 0:
                    flash.remove(f)

            # Render all game objects
            window.blit(taniwha.surface, taniwha.position.get_center(taniwha.surface))

            focal_point = (int(taniwha.focal_point.x), int(taniwha.focal_point.y))

            pygame.draw.circle(window, (255, 64, 0), focal_point, 5)
            for f in fish:
                window.blit(f.surface, f.position.get())

            for diver in divers:
                window.blit(diver.surface, diver.position.get())

            if taniwha.lasers_on:
                for laser in taniwha.lasers:
                    pygame.draw.line(window, (245, 50, 77), laser.start.get(), laser.focal.get(), 8)
                    pygame.draw.line(window, (215, 114, 94), laser.start.get(), laser.focal.get(), 3)

                    if current_time > (bubble_check + random.uniform(0.02, 0.05)):
                        bubble_check = current_time

                        b_pos = Position(taniwha.focal_point.x, taniwha.focal_point.y)
                        create_bubble(b_pos)

            for bubble in bubbles:
                window.blit(bubble.surface, bubble.position.get())

            for f in flash:
                window.blit(f.surface, f.position.get())

            pygame.display.update()