diff --git a/AI toolbox text.pdf b/AI toolbox text.pdf new file mode 100644 index 0000000..8f903d5 Binary files /dev/null and b/AI toolbox text.pdf differ diff --git a/astar.py b/astar.py index f017985..a30f782 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 + 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: + if event.key == pygame.K_l: self.add_tile_type = 'lava' - # insert swamp code here + if event.key == pygame.K_s: + self.add_tile_type = 'swamp' class Actor(object): @@ -168,9 +176,9 @@ def f_cost(self): 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.g_cost + #COST_TO_DRAW = self.h_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,33 @@ 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)] + directions_diagonal = [(1,1), (1,-1), (-1,1), (-1,-1)] + directions_jump = [(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 directions_diagonal] + all_adj_jump = [self.world._add_coords(coords, d) for d in directions_jump] + 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_jump = [self.is_valid(c) for c in all_adj_jump] + 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_jump): + if(in_bounds_jump[i]): + costs.append(8 + self.world.get_terrain_cost(coord)) + open_adj.append(coord) return open_adj, costs def is_valid(self, coord): diff --git a/screenshots/all implemented - f printed.PNG b/screenshots/all implemented - f printed.PNG new file mode 100644 index 0000000..5a267ea Binary files /dev/null and b/screenshots/all implemented - f printed.PNG differ diff --git a/screenshots/diagonal.PNG b/screenshots/diagonal.PNG new file mode 100644 index 0000000..6537b82 Binary files /dev/null and b/screenshots/diagonal.PNG differ diff --git a/screenshots/jump.PNG b/screenshots/jump.PNG new file mode 100644 index 0000000..00c362a Binary files /dev/null and b/screenshots/jump.PNG differ diff --git a/screenshots/print f.PNG b/screenshots/print f.PNG new file mode 100644 index 0000000..c57af9e Binary files /dev/null and b/screenshots/print f.PNG differ diff --git a/screenshots/print g.PNG b/screenshots/print g.PNG new file mode 100644 index 0000000..6391df4 Binary files /dev/null and b/screenshots/print g.PNG differ diff --git a/screenshots/print h new.PNG b/screenshots/print h new.PNG new file mode 100644 index 0000000..15c9a2e Binary files /dev/null and b/screenshots/print h new.PNG differ