def move_pacman(self, position, direction, image): """Move pacman to given position; this is called by animate_pacman.""" screen_position = self.to_screen(position) endpoints = self.get_endpoints(direction, position) r = PACMAN_SCALE * self.grid_size gu.move_circle(image[0], screen_position, r, endpoints) gu.refresh()
def update_distributions(self, distributions): """Draw an agent's belief distributions.""" # copy all distributions so we don't change their state distributions = [x.copy() for x in distributions] if self.distribution_images is None: self.draw_distributions(self.previous_state) for x in range(len(self.distribution_images)): for y in range(len(self.distribution_images[0])): image = self.distribution_images[x][y] weights = [dist[(x, y)] for dist in distributions] if sum(weights) != 0: pass # Fog of war color = [0.0, 0.0, 0.0] colors = GHOST_VEC_COLORS[1:] # With Pacman if self.capture: colors = GHOST_VEC_COLORS for weight, gcolor in zip(weights, colors): color = [ min(1.0, c + 0.95 * g * weight**.3) for c, g in zip(color, gcolor) ] gu.change_color(image, gu.format_color(*color)) gu.refresh()
def animate_pacman(self, pacman, prev_pacman, image): """Move pacman based on position update and enforce frame rate.""" if self.frame_time < 0: print('Press any key to step forward, "q" to play') keys = gu.wait_for_keys() if 'q' in keys: self.frame_time = 0.1 if abs(self.frame_time) > 0.00001: fx, fy = self.get_position(prev_pacman) px, py = self.get_position(pacman) frames = 4.0 for i in range(1, int(frames) + 1): pos = (px * i / frames + fx * (frames - i) / frames, py * i / frames + fy * (frames - i) / frames) self.move_pacman(pos, self.get_direction(pacman), image) gu.refresh() elapsed = time.time() - self.time if elapsed <= (abs(self.frame_time) / frames): gu.sleep((abs(self.frame_time) / frames) - elapsed) self.time = time.time() else: self.move_pacman(self.get_position(pacman), self.get_direction(pacman), image) gu.refresh()
def draw_static_objects(self, state): """Draw walls, food, capsules in state.""" layout = self.layout self.draw_walls(layout.walls) self.food = self.draw_food(layout.food) self.capsules = self.draw_capsules(layout.capsules) gu.refresh()
def draw_agent_objects(self, state): """Draw agents (pacman, ghosts) from state.""" self.agent_images = [] # (agent_state, image) for index, agent in enumerate(state.agent_states): if agent.is_pacman: image = self.draw_pacman(agent, index) self.agent_images.append((agent, image)) else: image = self.draw_ghost(agent, index) self.agent_images.append((agent, image)) gu.refresh()
def swap_images(self, agent_index, new_state): """Change an image from a ghost to a pacman or vice versa. Used for capture the flag. """ prev_state, prev_image = self.agent_images[agent_index] for item in prev_image: gu.remove_from_screen(item) if new_state.is_pacman: image = self.draw_pacman(new_state, agent_index) self.agent_images[agent_index] = (new_state, image) else: image = self.draw_ghost(new_state, agent_index) self.agent_images[agent_index] = (new_state, image) gu.refresh()
def draw_expanded_cells(self, cells): """Draw an overlay of expanded grid positions for search agents.""" n = float(len(cells)) base_color = [1.0, 0.0, 0.0] self.clear_expanded_cells() self.expanded_cells = [] for k, cell in enumerate(cells): screen_pos = self.to_screen(cell) cell_color = gu.format_color(*[(n - k) * c * .5 / n + .25 for c in base_color]) block = gu.square(screen_pos, 0.5 * self.grid_size, color=cell_color, filled=1, behind=2) self.expanded_cells.append(block) if self.frame_time < 0: gu.refresh()
def debug_draw(self, cells, color=[1.0, 0.0, 0.0], clear=False): """Draw debug information to screen.""" if clear: self.clear_debug() self.expanded_cells = [] for k, cell in enumerate(cells): screen_pos = self.to_screen(cell) cell_color = gu.format_color(*color) block = gu.square(screen_pos, 0.5 * self.grid_size, color=cell_color, filled=1, behind=2) self.expanded_cells.append(block) if self.frame_time < 0: gu.refresh()
def move_ghost(self, ghost, ghost_index, prev_ghost, ghost_image_parts): """Move given ghost.""" old_x, old_y = self.to_screen(self.get_position(prev_ghost)) new_x, new_y = self.to_screen(self.get_position(ghost)) delta = new_x - old_x, new_y - old_y for ghost_image_part in ghost_image_parts: gu.move_by(ghost_image_part, delta) gu.refresh() if ghost.scared_timer > 0: color = SCARED_COLOR else: color = GHOST_COLORS[ghost_index] gu.edit(ghost_image_parts[0], ('fill', color), ('outline', color)) self.move_eyes(self.get_position(ghost), self.get_direction(ghost), ghost_image_parts[-4:]) gu.refresh()