Skip to content

Turning in the AI toolbox #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
24 changes: 15 additions & 9 deletions astar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == 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)
Expand Down Expand Up @@ -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_s:
self.add_tile_type = 'swamp'
elif event.key == pygame.K_l:
self.add_tile_type = 'lava'
#insert swamp code here

class Actor(object):
def __init__(self, cell_coordinates, world, image_loc, unremovable = False, is_obstacle = True):
Expand Down Expand Up @@ -143,9 +149,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[0],self.coordinates[1]),(self.dimensions[0],self.dimensions[1]))
pygame.draw.rect(self.draw_screen, self.color, rect, line_width)
Expand All @@ -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)
Expand Down
Binary file added costs.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 diag2.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 diag_1.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 hops.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.