diff --git a/Pathfinder.py b/Pathfinder.py index cbcdca1..2fe2eb1 100644 --- a/Pathfinder.py +++ b/Pathfinder.py @@ -5,6 +5,8 @@ from pygame.locals import ( K_BACKSPACE, K_RETURN, + K_e, + K_s, KEYDOWN, MOUSEBUTTONUP, MOUSEBUTTONDOWN, @@ -21,7 +23,7 @@ def __init__(self, figure, is_wall, distance_from_start, coords, visited=False, self.coords = coords self.visited = visited self.predecessor = predecessor - + def __lt__(self, other_cell): return self.distance_from_start < other_cell.distance_from_start @@ -42,7 +44,13 @@ def __lt__(self, other_cell): def setup(): - screen.fill(BLACK) + global start_coords, end_coords + start_coords = (np.random.randint(0, NUMBER_OF_ROWS), np.random.randint(0, NUMBER_OF_COLUMNS)) + end_coords = (np.random.randint(0, NUMBER_OF_ROWS), np.random.randint(0, NUMBER_OF_COLUMNS)) + + while start_coords == end_coords: + end_coords = (np.random.randint(0, NUMBER_OF_ROWS), np.random.randint(0, NUMBER_OF_COLUMNS)) + pygame.display.set_caption("Pathfinder") initialize_matrix() @@ -51,12 +59,11 @@ def setup(): def initialize_matrix(): global matrix - matrix = np.empty((NUMBER_OF_ROWS, NUMBER_OF_COLUMNS), dtype=Cell) - def draw_grid(): global start_coords, end_coords + screen.fill(BLACK) MARGIN = 1 DIMENSION = (WINDOW_WIDTH - MARGIN * NUMBER_OF_COLUMNS) // NUMBER_OF_COLUMNS @@ -66,12 +73,7 @@ def draw_grid(): rect = pygame.Rect((MARGIN + DIMENSION) * y + MARGIN, (MARGIN + DIMENSION) * x + MARGIN, DIMENSION, DIMENSION) pygame.draw.rect(screen, WHITE, rect) matrix[x][y] = Cell(rect.copy(), False, np.inf, (x, y)) - - start_coords = (np.random.randint(0, NUMBER_OF_ROWS), np.random.randint(0, NUMBER_OF_COLUMNS)) - end_coords = (np.random.randint(0, NUMBER_OF_ROWS), np.random.randint(0, NUMBER_OF_COLUMNS)) - - while start_coords == end_coords: - end_coords = (np.random.randint(0, NUMBER_OF_ROWS), np.random.randint(0, NUMBER_OF_COLUMNS)) + start_cell = matrix[start_coords[0]][start_coords[1]] end_cell = matrix[end_coords[0]][end_coords[1]] @@ -84,7 +86,7 @@ def draw_grid(): matrix[start_coords[0]][start_coords[1]] = Cell(start_rect.copy(), True, 0, start_coords, visited=True) matrix[end_coords[0]][end_coords[1]] = Cell(end_rect.copy(), False, np.inf, end_coords) - + pygame.display.update() @@ -100,7 +102,7 @@ def mark_as_wall(): matrix[row][col].is_wall = True rect = matrix[row][col].figure pygame.draw.rect(screen, BLACK, rect) - + pygame.display.update() @@ -116,9 +118,9 @@ def mark_as_visited(cell, distance, predecessor): pygame.draw.rect(screen, YELLOW, rect) pygame.display.update() - + return False - + return True @@ -140,10 +142,10 @@ def find_path(): start = matrix[start_coords[0]][start_coords[1]] queue = [start] found = True - + while queue: most_near = heapq.heappop(queue) - + if most_near.distance_from_start == np.inf: found = False break @@ -159,7 +161,7 @@ def find_path(): if mark_as_visited(above, new_distance, most_near): break heapq.heappush(queue, above) - + if below and not below.is_wall and not below.visited: if mark_as_visited(below, new_distance, most_near): break @@ -170,22 +172,23 @@ def find_path(): break heapq.heappush(queue, right) - if left and not left.is_wall and not left.visited: + if left and not left.is_wall and not left.visited: if mark_as_visited(left, new_distance, most_near): break heapq.heappush(queue, left) - + if all(value for value in [above != None, below != None, right != None, left != None]): if all(value for value in [above.is_wall, below.is_wall, right.is_wall, left.is_wall]): return - + if found: highlight_path() - + screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) matrix = None + start_coords = None end_coords = None @@ -200,20 +203,36 @@ def find_path(): for event in pygame.event.get(): if event.type == QUIT: running = False - + if event.type == MOUSEBUTTONDOWN: dragging = True - + if dragging and event.type == MOUSEMOTION: mark_as_wall() - + if event.type == MOUSEBUTTONUP: dragging = False - + if event.type == KEYDOWN: if event.key == K_RETURN: find_path() + if event.key == K_s: + position = pygame.mouse.get_pos() + col = position[0] // 10 + row = position[1] // 10 + if end_coords != (row,col) : + start_coords = (row,col) + initialize_matrix() + draw_grid() + if event.key == K_e: + position = pygame.mouse.get_pos() + col = position[0] // 10 + row = position[1] // 10 + if start_coords != (row,col) : + end_coords = (row,col) + initialize_matrix() + draw_grid() if event.key == K_BACKSPACE: setup() - -pygame.quit() \ No newline at end of file + +pygame.quit() diff --git a/README.md b/README.md index 38dfbb1..27c7389 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,11 @@ This is my first experiment with pygame, I made a pathfinding viewer that use Di ## How to use it: - To run it, after you go in the same directory of the project, write in the console: `python3 Pathfinder.py` - To make walls: drag with cursor on the grid -- To reset the grid: press **backspace** +- To reset the grid with random starting and ending points: press **backspace** - To execute the algorithm: press **enter** +- To set the mouse position as starting point: press **s** +- To set the mouse position as ending point: press **e** + ## How it looks: -![Image of how it looks](https://i.imgur.com/OiGjh21.png) +![Demo image](https://github.com/alinanto/Pathfinder/blob/master/image.PNG) diff --git a/image.PNG b/image.PNG new file mode 100644 index 0000000..c8fed10 Binary files /dev/null and b/image.PNG differ