diff --git a/astar.py b/astar.py index 802abe5..53d8587 100644 --- a/astar.py +++ b/astar.py @@ -25,7 +25,7 @@ def _init_cells(self): for i in range(self.height): for j in range(self.width): self.cells[(i,j)] = Cell(self.screen,(i*self.cell_size, j*self.cell_size),(self.cell_size,self.cell_size)) - + def _add_coords(self,a,b): return tuple(map(sum,zip(a,b))) @@ -57,8 +57,12 @@ def _is_occupied(self,cell_coord): return False def _add_swamp(self, mouse_pos): - #insert swamp code here. - pass + swamp_coord = (mouse_pos[0]/50, mouse_pos[1]/50) + if self._is_occupied(swamp_coord): + if self.actors[swamp_coord].unremovable is False: + self.actors.pop(swamp_coord, None) + else: + self.actors[swamp_coord] = ObstacleTile( swamp_coord, self, './images/swamp.jpg', is_unpassable=False, terrain_cost=3) def _add_lava(self, mouse_pos): lava_coord = (mouse_pos[0]/50, mouse_pos[1]/50) @@ -91,14 +95,16 @@ def main_loop(self): elif event.type is pygame.MOUSEBUTTONDOWN: if self.add_tile_type == 'lava': self._add_lava(event.pos) - #insert swamp code here + if self.add_tile_type == 'swamp': + self._add_swamp(event.pos) elif event.type is pygame.KEYDOWN: if event.key == pygame.K_SPACE: self.paul.run_astar(self.cake.cell_coordinates, self) self.paul.get_path() elif event.key == pygame.K_l: self.add_tile_type = 'lava' - #insert swamp code here + elif event.key == pygame.K_s: + self.add_tile_type = 'swamp' class Actor(object): def __init__(self, cell_coordinates, world, image_loc, unremovable = False, is_obstacle = True): @@ -125,7 +131,7 @@ class ObstacleTile(Actor): def __init__(self, cell_coordinates, world, image_loc, terrain_cost=0, is_unpassable = True): super(ObstacleTile, self).__init__(cell_coordinates, world, image_loc, unremovable = False, is_obstacle = is_unpassable) self.terrain_cost = terrain_cost - + class Cell(): def __init__(self, draw_screen, coordinates, dimensions): self.draw_screen = draw_screen @@ -167,8 +173,8 @@ def get_h_cost(self, coord_a,coord_b): def get_open_adj_coords(self, coords): """returns list of valid coords that are adjacent to the argument, open, and not in the closed list.""" #modify directions and costs as needed - directions = [(1,0),(0,1),(-1,0),(0,-1)] - costs = [1,1,1,1] + directions = [(1,0),(0,1),(-1,0),(0,-1),(1,1),(1,-1),(-1,1),(-1,-1),(2,0),(0,2),(-2,0),(0,-2)] + costs = [1,1,1,1,3,3,3,3,8,8,8,8] adj_coords = map(lambda d: self.world._add_coords(coords,d), directions) for i, coord in enumerate(adj_coords): costs[i] += self.world.get_terrain_cost(coord) @@ -226,7 +232,7 @@ def run_astar(self, destination_coord, world): walkable_open_coords, costs = self.get_open_adj_coords(coord_s) for idx,coord in enumerate(walkable_open_coords): cell = self.cells[coord] - g_cost = cell_s.g_cost + costs[idx] + g_cost = cell_s.g_cost + costs[idx] h_cost = self.get_h_cost(coord, destination_coord) f_cost = g_cost + h_cost if coord in self.open_list: diff --git a/screenshots/allow_diagonals.png b/screenshots/allow_diagonals.png new file mode 100644 index 0000000..3f162a9 Binary files /dev/null and b/screenshots/allow_diagonals.png differ diff --git a/screenshots/fcost_uncomment.png b/screenshots/fcost_uncomment.png new file mode 100644 index 0000000..1fcf5bc Binary files /dev/null and b/screenshots/fcost_uncomment.png differ diff --git a/screenshots/gcost_uncomment.png b/screenshots/gcost_uncomment.png new file mode 100644 index 0000000..ab2907a Binary files /dev/null and b/screenshots/gcost_uncomment.png differ diff --git a/screenshots/hcost_uncomment.png b/screenshots/hcost_uncomment.png new file mode 100644 index 0000000..f7434b4 Binary files /dev/null and b/screenshots/hcost_uncomment.png differ diff --git a/screenshots/i_made_swamp.png b/screenshots/i_made_swamp.png new file mode 100644 index 0000000..07db466 Binary files /dev/null and b/screenshots/i_made_swamp.png differ diff --git a/screenshots/paul_jumps.png b/screenshots/paul_jumps.png new file mode 100644 index 0000000..0ed9cdf Binary files /dev/null and b/screenshots/paul_jumps.png differ diff --git a/written.md b/written.md new file mode 100644 index 0000000..0882bbf --- /dev/null +++ b/written.md @@ -0,0 +1,9 @@ +0. Depth-first search simply tries the next thing; it will usually take a long time and will not find the most efficient path. The advantage of A* over dfs is that A* takes into account the location of the goal. Breadth-first search works by examining each of the nodes around the position node, but still does not take into consideration the location of the goal. A* uses the distance from the start to current position and projected current position to goal to explore the most promising path. + +1. When you uncomment the g_cost line, the values printed in the cells are the distance from the start point. This is because g_cost is the distance from the starting position. When you uncomment the h_cost line, the values printed in the cells are the distance from the goal. This is because h_cost is the estimated distance to the goal. When you uncomment f_cost, the values printed in the cells are the final distance, except for the very isolated cells. In all cases, the values printed in inaccessible or lava cells are None. The screenshots of the pygame windows are in a screenshots folder in this repository. + +2. Paul is now capable of moving diagonally. A screenshot is included in the screenshots folder in this repository. + +3. Paul is now capable of moving through swamps at four times the cost of moving to unoccupied adjacent squares. A screenshot is included in the screenshots folder in this repository (it was difficult to coerce Paul into moving through a swamp). + +4. Paul is now capable of jumping lava at eight times the cost of moving to unoccupied adjacent squares. A screenshot is included in the screenshots folder in this repository.