diff --git a/Screenshots/Complete.png b/Screenshots/Complete.png new file mode 100644 index 0000000..2b4a335 Binary files /dev/null and b/Screenshots/Complete.png differ diff --git a/Screenshots/diagonal.png b/Screenshots/diagonal.png new file mode 100644 index 0000000..463bffd Binary files /dev/null and b/Screenshots/diagonal.png differ diff --git a/Screenshots/f score.png b/Screenshots/f score.png new file mode 100644 index 0000000..6f79bd5 Binary files /dev/null and b/Screenshots/f score.png differ diff --git a/Screenshots/g score.png b/Screenshots/g score.png new file mode 100644 index 0000000..4cf17de Binary files /dev/null and b/Screenshots/g score.png differ diff --git a/Screenshots/h score.png b/Screenshots/h score.png new file mode 100644 index 0000000..db2c3a4 Binary files /dev/null and b/Screenshots/h score.png differ diff --git a/Screenshots/hop.png b/Screenshots/hop.png new file mode 100644 index 0000000..4bd6281 Binary files /dev/null and b/Screenshots/hop.png differ diff --git a/astar.py b/astar.py index f017985..38efca1 100644 --- a/astar.py +++ b/astar.py @@ -73,8 +73,14 @@ def _is_occupied(self, cell_coord): def _add_swamp(self, mouse_pos): """ Adds a swamp tile in the cell that mouse_pos indicates """ - # 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].removable: + self.actors.pop(swamp_coord, None) + elif swamp_coord != self.cake.cell_coordinates: + swamp = ObstacleTile(swamp_coord, self, './images/swamp.jpg', + is_unpassable=False, terrain_cost=3) + self.actors[swamp_coord] = swamp def _add_lava(self, mouse_pos): """ Adds a lava tile in the cell that mouse_pos indicates """ @@ -108,14 +114,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 + elif 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): @@ -170,7 +178,7 @@ def draw(self): COST_TO_DRAW = '' # COST_TO_DRAW = self.g_cost # COST_TO_DRAW = self.h_cost - # COST_TO_DRAW = self.f_cost + COST_TO_DRAW = self.f_cost line_width = 2 rect = pygame.Rect(self.coordinates, self.dimensions) pygame.draw.rect(self.draw_screen, self.color, rect, line_width) @@ -197,14 +205,34 @@ def get_open_adj_coords(self, coords): open, and not in the closed list. """ # modify directions and costs as needed directions = [(1, 0), (0, 1), (-1, 0), (0, -1)] + diagonal = [(1,1), (1,-1), (-1,1), (-1,-1)] + hops = [(2, 0), (0, 2), (-2, 0), (0, -2)] + all_adj = [self.world._add_coords(coords, d) for d in directions] + all_adj_diagonal = [self.world._add_coords(coords, d) for d in diagonal] + all_adj_hops = [self.world._add_coords(coords, d) for d in hops] + in_bounds = [self.is_valid(c) for c in all_adj] + in_bounds_diagonal = [self.is_valid(c) for c in all_adj_diagonal] + in_bounds_hops = [self.is_valid(c) for c in all_adj_hops] + costs = [] open_adj = [] for i, coord in enumerate(all_adj): if(in_bounds[i]): costs.append(1 + self.world.get_terrain_cost(coord)) open_adj.append(coord) + + for i, coord in enumerate(all_adj_diagonal): + if(in_bounds_diagonal[i]): + costs.append(3 + self.world.get_terrain_cost(coord)) + open_adj.append(coord) + + for i, coord in enumerate(all_adj_hops): + if(in_bounds_hops[i]): + costs.append(8 + self.world.get_terrain_cost(coord)) + open_adj.append(coord) + return open_adj, costs def is_valid(self, coord):