Beispiel #1
0
    def __init__(self, fps: int, difficulty, game_over_score: int,
                 record_progress: bool):
        self._fps = fps
        self._clock = pygame.time.Clock()

        self._score = [0, 0]  # 1P, 2P
        self._game_over_score = game_over_score
        self._scene = Scene(difficulty)
        self._keyboard_action_1P = KeyCommandMap(
            {
                pygame.K_PERIOD: PlatformAction.SERVE_TO_LEFT,
                pygame.K_SLASH: PlatformAction.SERVE_TO_RIGHT,
                pygame.K_LEFT: PlatformAction.MOVE_LEFT,
                pygame.K_RIGHT: PlatformAction.MOVE_RIGHT,
            }, PlatformAction.NONE)
        self._keyboard_action_2P = KeyCommandMap(
            {
                pygame.K_q: PlatformAction.SERVE_TO_LEFT,
                pygame.K_e: PlatformAction.SERVE_TO_RIGHT,
                pygame.K_a: PlatformAction.MOVE_LEFT,
                pygame.K_d: PlatformAction.MOVE_RIGHT,
            }, PlatformAction.NONE)

        self._record_handler = get_record_handler(record_progress,
                                                  "manual_" + str(difficulty))
        self._screen = Screen(Scene.area_rect.size,
                              self._scene.draw_gameobjects)
Beispiel #2
0
    def __init__(self, fps: int, level: int, record_progress, one_shot_mode):
        self._init_pygame()

        self._fps = fps
        self._scene = Scene(level)
        self._keyboard = KeyCommandMap({
                pygame.K_LEFT:  PlatformAction.MOVE_LEFT,
                pygame.K_RIGHT: PlatformAction.MOVE_RIGHT,
            }, PlatformAction.NONE)

        self._record_handler = get_record_handler(record_progress, {
                "status": (GameStatus.GAME_OVER, GameStatus.GAME_PASS)
            }, get_log_dir())
        self._one_shot_mode = one_shot_mode
Beispiel #3
0
    def __init__(self, fps, one_shot_mode, record_progress):
        self._init_pygame()

        self._scene = Scene()

        self._fps = fps
        self._keyboard_action = KeyCommandMap({
            pygame.K_UP:    SnakeAction.UP,
            pygame.K_DOWN:  SnakeAction.DOWN,
            pygame.K_LEFT:  SnakeAction.LEFT,
            pygame.K_RIGHT: SnakeAction.RIGHT,
        }, SnakeAction.NONE)

        self._one_shot_mode = one_shot_mode
        self._record_handler = get_record_handler(record_progress, "manual")
Beispiel #4
0
    def __init__(self, fps, one_shot_mode, record_progress):
        self._init_pygame()

        self._scene = Scene()

        self._fps = fps
        self._keyboard_action = KeyCommandMap(
            {
                pygame.K_UP: SnakeAction.UP,
                pygame.K_DOWN: SnakeAction.DOWN,
                pygame.K_LEFT: SnakeAction.LEFT,
                pygame.K_RIGHT: SnakeAction.RIGHT,
            }, SnakeAction.NONE)

        self._one_shot_mode = one_shot_mode
        self._record_handler = get_record_handler(
            record_progress, {"status":
                              (GameStatus.GAME_OVER, )}, get_log_dir())
Beispiel #5
0
    def __init__(self, fps: int, difficulty, level: int, record_progress,
                 one_shot_mode):
        self._fps = fps
        self._clock = pygame.time.Clock()

        self._scene = Scene(difficulty, level)
        self._keyboard = KeyCommandMap(
            {
                pygame.K_a: PlatformAction.SERVE_TO_LEFT,
                pygame.K_d: PlatformAction.SERVE_TO_RIGHT,
                pygame.K_LEFT: PlatformAction.MOVE_LEFT,
                pygame.K_RIGHT: PlatformAction.MOVE_RIGHT,
            }, PlatformAction.NONE)

        self._record_handler = get_record_handler(record_progress, \
            "manual_" + str(difficulty) + "_" + str(level))
        self._one_shot_mode = one_shot_mode

        self._screen = Screen(Scene.area_rect.size,
                              self._scene.draw_gameobjects)
Beispiel #6
0
    def __init__(self, fps: int, game_over_score: int, record_progress: bool):
        self._init_pygame()

        self._fps = fps
        self._score = [0, 0]  # 1P, 2P
        self._game_over_score = game_over_score
        self._scene = gamecore.Scene(True)
        self._keyboard_action_1P = KeyCommandMap(
            {
                pygame.K_LEFT: PlatformAction.MOVE_LEFT,
                pygame.K_RIGHT: PlatformAction.MOVE_RIGHT,
            }, PlatformAction.NONE)
        self._keyboard_action_2P = KeyCommandMap(
            {
                pygame.K_a: PlatformAction.MOVE_LEFT,
                pygame.K_d: PlatformAction.MOVE_RIGHT,
            }, PlatformAction.NONE)

        self._record_handler = get_record_handler(
            record_progress,
            {"status": (GameStatus.GAME_1P_WIN, GameStatus.GAME_2P_WIN)},
            get_log_dir())
Beispiel #7
0
class Arkanoid:
    def __init__(self, fps: int, level: int, record_progress, one_shot_mode):
        self._init_pygame()

        self._fps = fps
        self._scene = Scene(level)
        self._keyboard = KeyCommandMap({
                pygame.K_LEFT:  PlatformAction.MOVE_LEFT,
                pygame.K_RIGHT: PlatformAction.MOVE_RIGHT,
            }, PlatformAction.NONE)

        self._record_handler = get_record_handler(record_progress, {
                "status": (GameStatus.GAME_OVER, GameStatus.GAME_PASS)
            }, get_log_dir())
        self._one_shot_mode = one_shot_mode

    def _init_pygame(self):
        pygame.display.init()
        pygame.display.set_caption("Arkanoid")
        self._screen = pygame.display.set_mode(Scene.area_rect.size)
        self._clock = pygame.time.Clock()

    def game_loop(self):
        while not quit_or_esc():
            command = self._keyboard.get_command()
            self._record_scene_info(command)
            game_status = self._scene.update(command)

            if game_status == GameStatus.GAME_OVER or \
               game_status == GameStatus.GAME_PASS:
                print(game_status.value)
                self._record_scene_info(None)

                if self._one_shot_mode:
                    return

                self._scene.reset()

            self._screen.fill((0, 0, 0))
            self._scene.draw_gameobjects(self._screen)
            pygame.display.flip()

            self._clock.tick(self._fps)

    def _record_scene_info(self, command):
        scene_info = self._scene.get_scene_info()
        if command:
            scene_info.command = command.value
        self._record_handler(scene_info)
Beispiel #8
0
class Arkanoid:
    def __init__(self, fps: int, difficulty, level: int, record_progress,
                 one_shot_mode):
        self._fps = fps
        self._clock = pygame.time.Clock()

        self._scene = Scene(difficulty, level)
        self._keyboard = KeyCommandMap(
            {
                pygame.K_a: PlatformAction.SERVE_TO_LEFT,
                pygame.K_d: PlatformAction.SERVE_TO_RIGHT,
                pygame.K_LEFT: PlatformAction.MOVE_LEFT,
                pygame.K_RIGHT: PlatformAction.MOVE_RIGHT,
            }, PlatformAction.NONE)

        self._record_handler = get_record_handler(record_progress, \
            "manual_" + str(difficulty) + "_" + str(level))
        self._one_shot_mode = one_shot_mode

        self._screen = Screen(Scene.area_rect.size,
                              self._scene.draw_gameobjects)

    def game_loop(self):
        while not quit_or_esc():
            command = self._keyboard.get_command()
            self._record_scene_info(command)
            game_status = self._scene.update(command)

            if game_status == GameStatus.GAME_OVER or \
               game_status == GameStatus.GAME_PASS:
                print(game_status.value)
                self._record_scene_info(None)

                if self._one_shot_mode:
                    return

                self._scene.reset()

            self._screen.update(self._scene.catch_ball_times)
            self._clock.tick(self._fps)

    def _record_scene_info(self, command):
        scene_info = self._scene.get_scene_info()
        if command:
            scene_info.command = command
        self._record_handler(scene_info)
Beispiel #9
0
class PingPong:
    def __init__(self, fps: int, difficulty, game_over_score: int,
                 record_progress: bool):
        self._fps = fps
        self._clock = pygame.time.Clock()

        self._score = [0, 0]  # 1P, 2P
        self._game_over_score = game_over_score
        self._scene = Scene(difficulty)
        self._keyboard_action_1P = KeyCommandMap(
            {
                pygame.K_PERIOD: PlatformAction.SERVE_TO_LEFT,
                pygame.K_SLASH: PlatformAction.SERVE_TO_RIGHT,
                pygame.K_LEFT: PlatformAction.MOVE_LEFT,
                pygame.K_RIGHT: PlatformAction.MOVE_RIGHT,
            }, PlatformAction.NONE)
        self._keyboard_action_2P = KeyCommandMap(
            {
                pygame.K_q: PlatformAction.SERVE_TO_LEFT,
                pygame.K_e: PlatformAction.SERVE_TO_RIGHT,
                pygame.K_a: PlatformAction.MOVE_LEFT,
                pygame.K_d: PlatformAction.MOVE_RIGHT,
            }, PlatformAction.NONE)

        self._record_handler = get_record_handler(record_progress,
                                                  "manual_" + str(difficulty))
        self._screen = Screen(Scene.area_rect.size,
                              self._scene.draw_gameobjects)

    def game_loop(self):
        while not quit_or_esc():
            command_1P = self._keyboard_action_1P.get_command()
            command_2P = self._keyboard_action_2P.get_command()

            scene_info = self._scene.get_scene_info()
            scene_info.command_1P = command_1P
            scene_info.command_2P = command_2P
            self._record_handler(scene_info)

            game_status = self._scene.update(command_1P, command_2P)

            if game_status != GameStatus.GAME_ALIVE:
                print(game_status.value)
                self._record_handler(self._scene.get_scene_info())
                if self._game_over(game_status):
                    break

                self._scene.reset()

            self._screen.update(self._score, self._scene._ball.speed)
            self._clock.tick(self._fps)

        self._print_result()

    def _game_over(self, status):
        if status == GameStatus.GAME_1P_WIN:
            self._score[0] += 1
        elif status == GameStatus.GAME_2P_WIN:
            self._score[1] += 1
        else:  # Draw game
            self._score[0] += 1
            self._score[1] += 1

        is_game_over = (self._score[0] == self._game_over_score
                        or self._score[1] == self._game_over_score)

        return is_game_over

    def _print_result(self):
        if self._score[0] > self._score[1]:
            win_side = "1P"
        elif self._score[0] == self._score[1]:
            win_side = "No one"
        else:
            win_side = "2P"

        print("{} wins! Final score: {}-{}".format(win_side, *self._score))
Beispiel #10
0
class Snake:
    """
    The game execution manager
    """
    def __init__(self, fps, one_shot_mode, record_progress):
        self._init_pygame()

        self._scene = Scene()

        self._fps = fps
        self._keyboard_action = KeyCommandMap(
            {
                pygame.K_UP: SnakeAction.UP,
                pygame.K_DOWN: SnakeAction.DOWN,
                pygame.K_LEFT: SnakeAction.LEFT,
                pygame.K_RIGHT: SnakeAction.RIGHT,
            }, SnakeAction.NONE)

        self._one_shot_mode = one_shot_mode
        self._record_handler = get_record_handler(record_progress, "manual")

    def _init_pygame(self):
        """
        Initialize the required pygame module
        """
        pygame.display.init()
        pygame.display.set_caption("Snake")
        self._screen = pygame.display.set_mode( \
            (Scene.area_rect.width, Scene.area_rect.height + 25))

        self._clock = pygame.time.Clock()

        pygame.font.init()
        self._font = pygame.font.Font(None, 22)
        self._font_pos = (1, Scene.area_rect.width + 5)

    def game_loop(self):
        """
        The game execution loop
        """
        while not quit_or_esc():
            # Get the command from the keyboard
            command = self._keyboard_action.get_command()

            # Record the scene information
            self._record_scene(command)

            # Update the scene
            game_status = self._scene.update(command)

            # If the game is over, reset the scene or
            # quit the game loop if one shot mode is set.
            if game_status == GameStatus.GAME_OVER:
                # Record the scene info with the game over status
                self._record_scene(None)

                print("Score: {}".format(self._scene.score))

                if self._one_shot_mode:
                    return

                self._scene.reset()

            # Draw the scene to the display
            self._draw_scene()

            # Wait for the next frame
            self._clock.tick(self._fps)

    def _draw_scene(self):
        """
        Draw the scene to the display
        """
        self._screen.fill((50, 50, 50))
        self._screen.fill((0, 0, 0), Scene.area_rect)
        self._scene.draw_gameobjects(self._screen)

        # Draw score
        font_surface = self._font.render( \
            "Score: {}".format(self._scene.score), True, (255, 255, 255))
        self._screen.blit(font_surface, self._font_pos)

        pygame.display.flip()

    def _record_scene(self, command):
        """
        Record the scene information
        """
        scene_info = self._scene.get_scene_info()
        scene_info.command = command
        self._record_handler(scene_info)
Beispiel #11
0
class PingPong:
    def __init__(self, fps: int, game_over_score: int, record_progress: bool):
        self._init_pygame()

        self._fps = fps
        self._score = [0, 0]  # 1P, 2P
        self._game_over_score = game_over_score
        self._scene = gamecore.Scene(True)
        self._keyboard_action_1P = KeyCommandMap(
            {
                pygame.K_LEFT: PlatformAction.MOVE_LEFT,
                pygame.K_RIGHT: PlatformAction.MOVE_RIGHT,
            }, PlatformAction.NONE)
        self._keyboard_action_2P = KeyCommandMap(
            {
                pygame.K_a: PlatformAction.MOVE_LEFT,
                pygame.K_d: PlatformAction.MOVE_RIGHT,
            }, PlatformAction.NONE)

        self._record_handler = get_record_handler(
            record_progress,
            {"status": (GameStatus.GAME_1P_WIN, GameStatus.GAME_2P_WIN)},
            get_log_dir())

    def _init_pygame(self):
        pygame.display.init()
        pygame.display.set_caption("PingPong")
        self._screen = pygame.display.set_mode(gamecore.display_area_size)
        self._clock = pygame.time.Clock()

        pygame.font.init()
        self._font = pygame.font.Font(None, 22)
        self._font_pos_1P = (1, gamecore.display_area_size[1] - 21)
        self._font_pos_2P = (1, 4)
        self._font_pos_speed = (gamecore.display_area_size[0] - 75, \
            gamecore.display_area_size[1] - 21)

    def game_loop(self):
        while not quit_or_esc():
            command_1P = self._keyboard_action_1P.get_command()
            command_2P = self._keyboard_action_2P.get_command()

            scene_info = self._scene.fill_scene_info_obj(SceneInfo())
            scene_info.command_1P = command_1P.value
            scene_info.command_2P = command_2P.value
            self._record_handler(scene_info)

            game_status = self._scene.update(command_1P, command_2P)

            if game_status == GameStatus.GAME_1P_WIN or \
               game_status == GameStatus.GAME_2P_WIN:
                print(game_status.value)
                self._record_handler(
                    self._scene.fill_scene_info_obj(SceneInfo()))
                if self._game_over(game_status):
                    break

                self._scene.reset()

            self._screen.fill((0, 0, 0))
            self._scene.draw_gameobjects(self._screen)
            self._draw_game_status()
            pygame.display.flip()

            self._clock.tick(self._fps)

        if self._score[0] > self._score[1]:
            print("1P wins!")
        else:
            print("2P wins!")
        print("Final score: {}-{}".format(*self._score))

        pygame.quit()

    def _draw_game_status(self):
        font_1P_surface = self._font.render( \
            "1P score: {}".format(self._score[0]), True, gamecore.color_1P)
        font_2P_surface = self._font.render( \
            "2P score: {}".format(self._score[1]), True, gamecore.color_2P)
        font_speed_surface = self._font.render( \
            "Speed: {}".format(abs(self._scene._ball._speed[0])), True, (255, 255, 255))
        self._screen.blit(font_1P_surface, self._font_pos_1P)
        self._screen.blit(font_2P_surface, self._font_pos_2P)
        self._screen.blit(font_speed_surface, self._font_pos_speed)

    def _game_over(self, status):
        if status == GameStatus.GAME_1P_WIN:
            self._score[0] += 1
        else:
            self._score[1] += 1

        is_game_over = self._score[0] == self._game_over_score or \
            self._score[1] == self._game_over_score

        return is_game_over