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
Binary file added AI toolbox text.pdf
Binary file not shown.
43 changes: 35 additions & 8 deletions astar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand Down
Binary file added screenshots/all implemented - f printed.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 screenshots/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 screenshots/jump.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 screenshots/print f.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 screenshots/print g.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 screenshots/print h new.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.