Beispiel #1
0
    def level_reset(self):
        pacman_entity = scene_tree.get_entity("pacman")
        pacman_entity.reset()

        for ghost_entity_id in ["blinky", "pinky", "inky", "clyde"]:
            ghost_entity = scene_tree.get_entity(ghost_entity_id)
            ghost_entity.reset()
Beispiel #2
0
    def reset_entity_positions(self):
        game.update_entity_position(
            entity_id=global_obj.level_grid.ENTITY_ID,
            position=(
                global_obj.level_grid.board_position.x,
                global_obj.level_grid.board_position.y,
            ),
        )

        pacman_entity = scene_tree.get_entity("pacman")
        if pacman_entity:
            pacman_position_x = global_obj.level_grid.board_position.x + (
                13 * 16) + 4
            pacman_position_y = global_obj.level_grid.board_position.y + (
                23 * 16) - 8
            pacman_entity.position.x = pacman_position_x
            pacman_entity.position.y = pacman_position_y

        blinky_entity = scene_tree.get_entity("blinky")
        if blinky_entity:
            blinky_position_x = global_obj.level_grid.board_position.x + (
                13 * 16) + 4
            blinky_position_y = global_obj.level_grid.board_position.y + (
                11 * 16) - 10
            blinky_entity.position.x = blinky_position_x
            blinky_entity.position.y = blinky_position_y

        pinky_entity = scene_tree.get_entity("pinky")
        if pinky_entity:
            pinky_position_x = global_obj.level_grid.board_position.x + (
                13 * 16) + 4
            pinky_position_y = global_obj.level_grid.board_position.y + (
                14 * 16) - 8
            pinky_entity.position.x = pinky_position_x
            pinky_entity.position.y = pinky_position_y

        inky_entity = scene_tree.get_entity("inky")
        if inky_entity:
            inky_position_x = global_obj.level_grid.board_position.x + (11 *
                                                                        16) + 4
            inky_position_y = global_obj.level_grid.board_position.y + (14 *
                                                                        16) - 8
            inky_entity.position.x = inky_position_x
            inky_entity.position.y = inky_position_y

        clyde_entity = scene_tree.get_entity("clyde")
        if clyde_entity:
            clyde_position_x = global_obj.level_grid.board_position.x + (
                15 * 16) + 4
            clyde_position_y = global_obj.level_grid.board_position.y + (
                14 * 16) - 8
            clyde_entity.position.x = clyde_position_x
            clyde_entity.position.y = clyde_position_y
Beispiel #3
0
 def _on_power_pellete_flash_timer_timeout(self):
     if self.power_pellete_ids:
         copied_pellete_ids = self.power_pellete_ids.copy()
         self.power_pelletes_visible = not self.power_pelletes_visible
         for pp_id in copied_pellete_ids:
             if scene_tree.get_entity(entity_id=pp_id) == None:
                 self.power_pellete_ids.remove(pp_id)
             else:
                 game.set_entity_visibility(
                     entity_id=pp_id, visible=self.power_pelletes_visible)
Beispiel #4
0
 def determine_target_tile(self):
     # TODO: Implement override for scatter
     if self.state == State.SCATTER:
         return (self.home_tile.x, self.home_tile.y)
     elif self.state == State.CHASE:
         pacman_entity = scene_tree.get_entity("pacman")
         return global_obj.level_grid.convert_position_to_grid(
             pacman_entity.position.x, pacman_entity.position.y)
     else:
         return super().determine_target_tile()
Beispiel #5
0
 def determine_target_tile(self):
     if self.state == State.CHASE:
         grid_position = global_obj.level_grid.convert_position_to_grid(
             self.position.x, self.position.y)
         pacman_entity = scene_tree.get_entity("pacman")
         pacman_grid_position = global_obj.level_grid.convert_position_to_grid(
             pacman_entity.position.x, pacman_entity.position.y)
         if Vector2.distance(grid_position, pacman_grid_position) > 8:
             return pacman_grid_position
         else:
             return (self.home_tile.x, self.home_tile.y)
     else:
         return super().determine_target_tile()
Beispiel #6
0
 def determine_target_tile(self):
     if self.state == State.CHASE:
         (
             grid_position_x,
             grid_position_y,
         ) = global_obj.level_grid.convert_position_to_grid(
             self.position.x, self.position.y
         )
         # Get pacman grid position
         pacman_entity = scene_tree.get_entity("pacman")
         (
             pacman_grid_position_x,
             pacman_grid_position_y,
         ) = global_obj.level_grid.convert_position_to_grid(
             pacman_entity.position.x, pacman_entity.position.y
         )
         if pacman_entity.direction == Direction.left:
             pacman_grid_position_x -= 2
         elif pacman_entity.direction == Direction.right:
             pacman_grid_position_x += 2
         if pacman_entity.direction == Direction.up:
             pacman_grid_position_y -= 2
         elif pacman_entity.direction == Direction.down:
             pacman_grid_position_y += 2
         # Get blinky grid position
         blinky_entity = scene_tree.get_entity("blinky")
         (
             blinky_grid_position_x,
             blinky_grid_position_y,
         ) = global_obj.level_grid.convert_position_to_grid(
             blinky_entity.position.x, blinky_entity.position.y
         )
         return (
             grid_position_x + (pacman_grid_position_x - blinky_grid_position_x) * 2,
             grid_position_y + (pacman_grid_position_y - blinky_grid_position_y) * 2,
         )
     else:
         return super().determine_target_tile()
Beispiel #7
0
 def _on_pacman_lose_life_start_timer_timeout(self):
     for ghost_entity_id in ["blinky", "pinky", "inky", "clyde"]:
         game.set_entity_visibility(entity_id=ghost_entity_id,
                                    visible=False)
     pacman_entity = scene_tree.get_entity("pacman")
     pacman_entity.lose_life()
     game.start_timer(timer_id=self.level_reset_timer_id)
     game.play_sound(sound_id="pacman-death")
     if global_obj.player_stats.lives <= 0:
         game.create_label_entity(entity_id="game_over_label",
                                  text="GAME  OVER",
                                  position=(147, 318),
                                  font_id="pacman-pixel",
                                  layer=5,
                                  fixed=False,
                                  wrap_length=600,
                                  color=(255, 0, 0))
Beispiel #8
0
 def determine_target_tile(self):
     if self.state == State.CHASE:
         pacman_entity = scene_tree.get_entity("pacman")
         (
             pacman_grid_position_x,
             pacman_grid_position_y,
         ) = global_obj.level_grid.convert_position_to_grid(
             pacman_entity.position.x, pacman_entity.position.y)
         if pacman_entity.direction == Direction.left:
             pacman_grid_position_x -= 4
         elif pacman_entity.direction == Direction.right:
             pacman_grid_position_x += 4
         if pacman_entity.direction == Direction.up:
             pacman_grid_position_y -= 4
         elif pacman_entity.direction == Direction.down:
             pacman_grid_position_y += 4
         return (pacman_grid_position_x, pacman_grid_position_y)
     else:
         return super().determine_target_tile()
Beispiel #9
0
    def __create__(self):
        self.speed = 100
        self.direction = Direction.left
        self.direction_to_turn = self.direction
        self.accumulated_delta = 0.0
        self.offset = Vector2(12, 12)
        self._state = State.IN_HOUSE
        self.previous_state = None
        self.target_tile = Vector2(0, 0)
        self.home_tile = Vector2(0, 0)
        self.ghost_house_tile = Vector2(13, 11)
        self.frightened_flash = False
        # Counters
        self.exit_pellete_counter = 0
        self.chase_state_counter = 0
        self.scatter_state_counter = 0
        # Timer
        self.chase_timer_id = f"{self.entity_id}_chase_timer"
        self.chase_timer_wait_time = 20.0
        self.scatter_timer_id = f"{self.entity_id}_scatter_timer"
        self.scatter_timer_wait_time = 9.0
        self.frightened_timer_id = f"{self.entity_id}_frightened_timer"
        self.frightened_timer_wait_time = 5.0
        self.frightened_flash_timer_id = f"{self.entity_id}_frightened_flash_timer"
        self.frightened_flash_timer_wait_time = 2.0

        game.create_timer(
            timer_id=self.chase_timer_id,
            time=self.chase_timer_wait_time,
            loops=False,
            start_on_creation=False,
        )
        game.subscribe_to_signal(
            source_id=self.chase_timer_id,
            signal_id="timeout",
            subscriber_entity_id=self.entity_id,
            function_name="_on_chase_timer_timeout",
        )

        game.create_timer(
            timer_id=self.scatter_timer_id,
            time=self.scatter_timer_wait_time,
            loops=False,
            start_on_creation=False,
        )
        game.subscribe_to_signal(
            source_id=self.scatter_timer_id,
            signal_id="timeout",
            subscriber_entity_id=self.entity_id,
            function_name="_on_scatter_timer_timeout",
        )

        game.create_timer(
            timer_id=self.frightened_timer_id,
            time=self.frightened_timer_wait_time,
            loops=False,
            start_on_creation=False,
        )
        game.subscribe_to_signal(
            source_id=self.frightened_timer_id,
            signal_id="timeout",
            subscriber_entity_id=self.entity_id,
            function_name="_on_frightened_timer_timeout",
        )

        game.create_timer(
            timer_id=self.frightened_flash_timer_id,
            time=self.frightened_flash_timer_wait_time,
            loops=False,
            start_on_creation=False,
        )
        game.subscribe_to_signal(
            source_id=self.frightened_flash_timer_id,
            signal_id="timeout",
            subscriber_entity_id=self.entity_id,
            function_name="_on_frightened_flash_timer_timeout",
        )

        pacman_entity = scene_tree.get_entity("pacman")
        game.subscribe_to_signal(
            source_id=pacman_entity.entity_id,
            signal_id="power_pellete_eaten",
            subscriber_entity_id=self.entity_id,
            function_name="_on_pacman_power_pellete_eaten",
        )
Beispiel #10
0
    def __create__(self):
        self.power_pellete_ids = []
        self.power_pelletes_visible = True
        self.one_up_visible = True
        self.start_timer_id = "start_timer"
        self.level_reset_timer_id = "level_reset_timer"
        self.level_cleared_timer_id = "level_cleared_timer"
        self.level_cleared_transtion_timer_id = "level_cleared_transtion_timer"
        power_pellete_flash_timer_id = "power_pellete_timer"
        self.pacman_lose_life_start_timer_id = "pacman_lose_life_start_timer"
        self.ghost_frightened_timer_id = "ghost_frightened_timer"
        self.show_entities_timer_id = "show_entities_timer"

        # Start Timer
        game.create_timer(timer_id=self.start_timer_id,
                          time=4.2,
                          loops=False,
                          start_on_creation=False)
        game.subscribe_to_signal(
            source_id=self.start_timer_id,
            signal_id="timeout",
            subscriber_entity_id=self.entity_id,
            function_name="_on_start_timer_timeout",
        )

        # Level Reset Timer
        game.create_timer(
            timer_id=self.level_reset_timer_id,
            time=2.0,
            loops=False,
            start_on_creation=False,
        )
        game.subscribe_to_signal(
            source_id=self.level_reset_timer_id,
            signal_id="timeout",
            subscriber_entity_id=self.entity_id,
            function_name="_on_level_reset_timer_timeout",
        )

        # Level Cleared Timer
        game.create_timer(
            timer_id=self.level_cleared_timer_id,
            time=1.5,
            loops=False,
            start_on_creation=False,
        )
        game.subscribe_to_signal(
            source_id=self.level_cleared_timer_id,
            signal_id="timeout",
            subscriber_entity_id=self.entity_id,
            function_name="_on_level_cleared_timer_timeout",
        )

        # Level Cleared Transtion Timer
        game.create_timer(
            timer_id=self.level_cleared_transtion_timer_id,
            time=2.0,
            loops=False,
            start_on_creation=False,
        )
        game.subscribe_to_signal(
            source_id=self.level_cleared_transtion_timer_id,
            signal_id="timeout",
            subscriber_entity_id=self.entity_id,
            function_name="_on_level_cleared_transition_timer_timeout",
        )

        # Pellete Flash Timer
        game.create_timer(
            timer_id=power_pellete_flash_timer_id,
            time=0.2,
            loops=True,
            start_on_creation=True,
        )
        game.subscribe_to_signal(
            source_id=power_pellete_flash_timer_id,
            signal_id="timeout",
            subscriber_entity_id=self.entity_id,
            function_name="_on_power_pellete_flash_timer_timeout",
        )

        # Pacman Lose Life Start Timer
        game.create_timer(
            timer_id=self.pacman_lose_life_start_timer_id,
            time=1.0,
            loops=False,
            start_on_creation=False,
        )
        game.subscribe_to_signal(
            source_id=self.pacman_lose_life_start_timer_id,
            signal_id="timeout",
            subscriber_entity_id=self.entity_id,
            function_name="_on_pacman_lose_life_start_timer_timeout",
        )

        # 1UP Flash Timer
        one_up_flash_timer_id = "one_up_flash_timer"
        game.create_timer(timer_id=one_up_flash_timer_id,
                          time=0.4,
                          loops=True,
                          start_on_creation=True)
        game.subscribe_to_signal(
            source_id=one_up_flash_timer_id,
            signal_id="timeout",
            subscriber_entity_id=self.entity_id,
            function_name="_on_one_up_flash_timer_timeout",
        )

        # Ghost Frightened Timer
        game.create_timer(
            timer_id=self.ghost_frightened_timer_id,
            time=7.0,
            loops=False,
            start_on_creation=False,
        )
        game.subscribe_to_signal(
            source_id=self.ghost_frightened_timer_id,
            signal_id="timeout",
            subscriber_entity_id=self.entity_id,
            function_name="_on_ghost_frightened_timer_timeout",
        )

        # Show Entities Timer
        game.create_timer(
            timer_id=self.show_entities_timer_id,
            time=2.0,
            loops=False,
            start_on_creation=False,
        )
        game.subscribe_to_signal(
            source_id=self.show_entities_timer_id,
            signal_id="timeout",
            subscriber_entity_id=self.entity_id,
            function_name="_on_show_entities_timer_timeout",
        )

        # Lose Life Signal
        pacman_entity = scene_tree.get_entity("pacman")
        game.subscribe_to_signal(
            source_id=pacman_entity.entity_id,
            signal_id="lose_life",
            subscriber_entity_id=self.entity_id,
            function_name="_on_pacman_lose_life",
        )
        # Power Pellet
        game.subscribe_to_signal(
            source_id=pacman_entity.entity_id,
            signal_id="power_pellete_eaten",
            subscriber_entity_id=self.entity_id,
            function_name="_on_pacman_power_pellete_eaten",
        )
        # Level Cleared Signal
        game.subscribe_to_signal(
            source_id=pacman_entity.entity_id,
            signal_id="level_cleared",
            subscriber_entity_id=self.entity_id,
            function_name="_on_pacman_level_cleared",
        )

        self.create_level_pellets()

        self.set_all_entities_visibility(False)

        self.reset_entity_positions()

        global_obj.player_stats.force_update_ui()

        if global_obj.game_state == GameState.BEGIN_GAME:
            global_obj.player_stats.force_update_ui()
            global_obj.game_state = GameState.PLAYING
            game.start_timer(timer_id=self.start_timer_id, time=4.2)
            game.start_timer(timer_id=self.show_entities_timer_id, time=2.0)
            game.play_sound(sound_id="pacman-begin")
            global_obj.player_stats.pelletes = 0
        elif global_obj.game_state == GameState.LEVEL_COMPLETE:
            global_obj.player_stats.level += 1
            global_obj.player_stats.force_update_ui()
            global_obj.game_state = GameState.PLAYING
            game.start_timer(timer_id=self.start_timer_id, time=1.0)
            self._on_show_entities_timer_timeout()
            global_obj.player_stats.pelletes = 0
Beispiel #11
0
    def __process__(self, delta_time):
        if global_obj.game_started and not global_obj.game_paused:
            collided_entity_id, collider_tag = game.check_entity_collision(
                entity_id=self.entity_id, offset_position=(0, 0))
            if collided_entity_id:
                if collider_tag == "pellete":
                    game.delete_entity(entity_id=collided_entity_id)
                    global_obj.player_stats.score += 10
                    global_obj.player_stats.pelletes += 1
                    self.pellete_counter -= 1
                    game.play_sound(sound_id="pacman-chomp")
                elif collider_tag == "power_pellete":
                    game.delete_entity(entity_id=collided_entity_id)
                    global_obj.player_stats.score += 50
                    global_obj.player_stats.pelletes += 1
                    self.pellete_counter -= 1
                    self.ghost_eaten_score = 0
                    game.emit_signal(
                        entity_id=self.entity_id,
                        signal_id=self.power_pellete_eaten_signal_id,
                    )
                elif collider_tag == "enemy":
                    collided_ghost = scene_tree.get_entity(collided_entity_id)
                    if collided_ghost.state == State.FRIGHTENED:
                        if self.ghost_eaten_score == 0:
                            self.ghost_eaten_score = 200
                        else:
                            self.ghost_eaten_score *= 2
                        global_obj.player_stats.score += self.ghost_eaten_score
                        collided_ghost.eaten(self.ghost_eaten_score)
                        game.start_timer(timer_id=self.eaten_freeze_timer_id)
                        global_obj.game_paused = True
                        game.play_sound(sound_id="pacman-eat-ghost")
                    elif (collided_ghost.state == State.CHASE
                          or collided_ghost.state == State.SCATTER):
                        game.stop_animation(entity_id=self.entity_id)
                        game.emit_signal(entity_id=self.entity_id,
                                         signal_id=self.lose_life_signal_id)
                elif collider_tag == "fruit":
                    fruit_entity = scene_tree.get_entity(collided_entity_id)
                    if not fruit_entity.collected:
                        global_obj.player_stats.score += global_obj.player_stats.get_fruit_score(
                            collided_entity_id)
                        fruit_entity.eaten()

            # Input - determines direction pacman wants to turn
            if game.is_action_pressed(action="left"):
                self.direction_to_turn = Direction.left
            elif game.is_action_pressed(action="right"):
                self.direction_to_turn = Direction.right
            elif game.is_action_pressed(action="up"):
                self.direction_to_turn = Direction.up
            elif game.is_action_pressed(action="down"):
                self.direction_to_turn = Direction.down

            # Determine speed
            if game.has_timer_stopped(timer_id=self.turn_speed_up_timer_id):
                self.speed = 100
            else:
                self.speed = 150
            self.accumulated_delta += delta_time * self.speed
            while self.accumulated_delta >= 1.0:
                self.accumulated_delta -= 1.0
                self.move(self.velocity, 1.0)

            self.update_animations()

            # Play movement sound
            if abs(self.velocity.x) > 0 or abs(self.velocity.y) > 0:
                if game.has_timer_stopped(timer_id=self.chomp_timer_id):
                    pass
                    # game.start_timer(timer_id=self.chomp_timer_id)
            else:
                self.accumulated_delta = 0.0
        elif (global_obj.game_paused
              and global_obj.game_state == GameState.PACMAN_LOSE_LIFE):
            pass
        else:
            game.stop_animation(entity_id=self.entity_id)

        if game.is_action_just_pressed(action="exit_game"):
            game.quit()