def reset(self): super().reset() self.state = State.SCATTER self.home_tile = Vector2(31, 0) self.direction = Direction.left self.direction_to_turn = self.direction game.play_animation(entity_id=self.entity_id, animation_name="left")
def reset(self): super().reset() self.state = State.IN_HOUSE self.home_tile = Vector2(0, 0) self.direction = Direction.down self.direction_to_turn = self.direction self.exit_pellete_counter = 0 game.play_animation(entity_id=self.entity_id, animation_name="down")
def reset(self): super().reset() self.state = State.IN_HOUSE self.home_tile = Vector2(31, 31) self.direction = Direction.up self.direction_to_turn = self.direction if global_obj.player_stats.level <= 1: self.exit_pellete_counter = 30 else: self.exit_pellete_counter = 0 game.play_animation(entity_id=self.entity_id, animation_name="up")
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()
def __create__(self): self.speed = 100 self.direction = Direction.left self.direction_to_turn = self.direction self.chomp_timer_id = "chomp-sound-timer" self.eaten_freeze_timer_id = "eaten-freeze-timer" self.turn_speed_up_timer_id = "turn_speed_up_timer" self.accumulated_delta = 0.0 self.offset = Vector2(12, 12) self.on_stop_animation_frame = True self.power_pellete_eaten_signal_id = "power_pellete_eaten" self.level_cleared_signal_id = "level_cleared" self.lose_life_signal_id = "lose_life" self.ghost_eaten_score = 0 self._pellete_counter = len( global_obj.level_grid.get_pellete_spaces()) + len( global_obj.level_grid.get_power_pellete_spaces()) game.create_timer(timer_id=self.chomp_timer_id, time=0.55) game.create_timer(timer_id=self.eaten_freeze_timer_id, time=1.0) game.subscribe_to_signal( source_id=self.eaten_freeze_timer_id, signal_id="timeout", subscriber_entity_id=self.entity_id, function_name="_on_eaten_freeze_timer_timeout", ) game.create_timer(timer_id=self.turn_speed_up_timer_id, time=0.1) # Animation Finished game.subscribe_to_signal( source_id=f"{self.entity_id}_animation", signal_id="animation_finished", subscriber_entity_id=self.entity_id, function_name="_on_pacman_animation_finished", ) game.create_signal(entity_id=self.entity_id, signal_id=self.power_pellete_eaten_signal_id) game.create_signal(entity_id=self.entity_id, signal_id=self.level_cleared_signal_id) game.create_signal(entity_id=self.entity_id, signal_id=self.lose_life_signal_id)
def __init__(self, rows, columns, board_position=(0, 0)): self.rows = rows self.columns = columns self.array = [[self.INVALID_SPACE for i in range(self.rows)] for j in range(self.columns)] self.turn_spaces = {} self.loop_around_spaces = self.LOOP_AROUND_SPACES.copy() self.ghost_slow_down_spaces = self.GHOST_SLOW_DOWN_SPACES.copy() self.pellete_spaces = self.PELLETE_SPACES.copy() self.power_pellete_spaces = self.POWER_PELLETE_SPACES.copy() self.board_position = Vector2(board_position[0], board_position[1]) self.ghost_house_turn_space = ( self.GHOST_HOUSE_TURN_SPACE[0] + self.board_position.x, self.GHOST_HOUSE_TURN_SPACE[1] + self.board_position.y, ) # Place valid space for x, y in self.VALID_SPACES: self.set(x, y, self.VALID_SPACE) for x, y, in self.TURN_SPACES: updated_x = x + self.board_position.x updated_y = y + self.board_position.y self.turn_spaces[(updated_x, updated_y)] = self.VALID_SPACE
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", )
def move(self, speed): self.velocity.x = 0 self.velocity.y = 0 direction_offset_x = 0 direction_offset_y = 0 if self.direction == Direction.left: self.velocity.x -= speed direction_offset_x = -1 elif self.direction == Direction.right: self.velocity.x += speed direction_offset_x = 1 elif self.direction == Direction.up: self.velocity.y -= speed direction_offset_y = -1 elif self.direction == Direction.down: self.velocity.y += speed direction_offset_y = 1 # Movement grid_space_x, grid_space_y = self.get_level_grid_position( self.position.x, self.position.y ) grid_space = global_obj.level_grid.get(grid_space_x, grid_space_y) if grid_space == LevelGrid.VALID_SPACE: if global_obj.level_grid.has_loop_around_space(grid_space_x, grid_space_y): # Left\ if grid_space_x < 0: self.position.x = ( global_obj.level_grid.board_position.x + (LevelGrid.RIGHT_LOOP_SPACE[0] * 16) - 16 ) # Right else: self.position.x = ( global_obj.level_grid.board_position.x + (LevelGrid.LEFT_LOOP_SPACE[0] * 16) + 16 ) elif ( self.state == State.DEFEATED_TRAVELING_TO_HOUSE and global_obj.level_grid.has_ghost_house_turn_space( int(self.position.x), int(self.position.y) ) ): # TODO: add proper ghost house re-entry logic self.state = State.DEFEATED_ENTERING_HOUSE else: # Update position self.position.x += self.velocity.x self.position.y += self.velocity.y # print(f"ghost valid grid_space = {(grid_space_x, grid_space_y)}, real_position = {(self.position.tuple())}") if global_obj.level_grid.has_turn_space( int(self.position.x), int(self.position.y) ): left_space = global_obj.level_grid.get( grid_space_x - 1, grid_space_y ) right_space = global_obj.level_grid.get( grid_space_x + 1, grid_space_y ) up_space = global_obj.level_grid.get(grid_space_x, grid_space_y - 1) down_space = global_obj.level_grid.get( grid_space_x, grid_space_y + 1 ) # Space selection logic (will need to be ghost independent) valid_spaces = [] if ( left_space == LevelGrid.VALID_SPACE and self.direction != Direction.right ): valid_spaces.append( (Direction.left, grid_space_x - 1, grid_space_y) ) if ( right_space == LevelGrid.VALID_SPACE and self.direction != Direction.left ): valid_spaces.append( (Direction.right, grid_space_x + 1, grid_space_y) ) if ( up_space == LevelGrid.VALID_SPACE and self.direction != Direction.down ): valid_spaces.append( (Direction.up, grid_space_x, grid_space_y - 1) ) if ( down_space == LevelGrid.VALID_SPACE and self.direction != Direction.up ): valid_spaces.append( (Direction.down, grid_space_x, grid_space_y + 1) ) self.target_tile = self.determine_target_tile() # Determine shortest path if valid_spaces and self.target_tile: closest_space = None closest_distance = 1000 for space in valid_spaces: _dir, _x, _y = space target_tile_distance = Vector2.distance( self.target_tile, (_x, _y) ) if target_tile_distance < closest_distance: closest_distance = target_tile_distance closest_space = space if closest_space: self.direction = closest_space[0] else: # print(f"invalid grid_space = {(grid_space_x, grid_space_y)}, real_position = {self.position.tuple()}") self.velocity.x = 0 self.velocity.y = 0
def __init__(self, entity_id, position_x, position_y): self.entity_id = entity_id self._position = Vector2(position_x, position_y, entity_id=entity_id) self.velocity = Vector2(0.0, 0.0) scene_tree.add_entity(self.entity_id, self)