Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Screenshots_ explanations.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
g_cost.png
g_cost is the cost of Paul's path to the cake.

h_cost.png
h_cost is the cost of the path from the cake to Paul.

f_cost.png
f_cost is the sum of g_cost and h_cost.

diagonal.png
the path will go through diagonals

hop.png
if surrounded by lava, will hop over lava at extra cost

swamp.png
when swamped, will hop over and if given the option to hop over lava or swamp, will hop over lava because swamp is more expensive
23 changes: 17 additions & 6 deletions astar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand Down Expand Up @@ -58,7 +58,14 @@ def _is_occupied(self,cell_coord):

def _add_swamp(self, mouse_pos):
#insert swamp code here.
pass
#pass
swamp_coord = (mouse_pos[0]/50, mouse_pos[1]/50)
if self._is_occupied(swamp_coord):
if self.actors[swamp_coord].unremovable == 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)
self.actors[swamp_coord].is_obstacle = True

def _add_lava(self, mouse_pos):
lava_coord = (mouse_pos[0]/50, mouse_pos[1]/50)
Expand Down Expand Up @@ -92,13 +99,17 @@ def main_loop(self):
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):
Expand All @@ -125,7 +136,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
Expand Down Expand Up @@ -167,8 +178,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)
Expand Down Expand Up @@ -226,7 +237,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:
Expand Down
Binary file added diagonal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added f_cost.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added g_cost.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added h_cost.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added hop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added swamp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.