diff --git a/astar.py b/astar.py index cc3ca77..f61a34d 100644 --- a/astar.py +++ b/astar.py @@ -1,15 +1,5 @@ from numpy import Infinity - - -CELL = 0 -WALL = 1 -START = 2 -END = 3 - - -ROWS = 10 -COLS = 10 - +from consts import * class Node: def __init__(self, x, y) -> None: diff --git a/consts.py b/consts.py index c85c099..ed4d6e5 100644 --- a/consts.py +++ b/consts.py @@ -9,3 +9,17 @@ PADDING = 5 CELL_SIZE = WIN_WIDTH / COLS CELL_SIZE_PADDED = CELL_SIZE - PADDING * 2 + +CELL = 0 +WALL = 1 +START = 2 +END = 3 + + +BACKGROUND = (0, 0, 0) +PATH_COLOR = (128, 128, 128) + +CELL_COLOR = (255, 255, 255) +WALL_COLOR = (0, 255, 0) +START_COLOR = (255, 0, 0) +END_COLOR = (0, 0, 255) diff --git a/drawing.py b/drawing.py index 12f0d7d..e605b5d 100644 --- a/drawing.py +++ b/drawing.py @@ -5,7 +5,8 @@ from consts import * def draw_cell(display, position, color): - position = (position[0] * CELL_SIZE + PADDING, position[1] * CELL_SIZE + PADDING) + position = (position[0] * CELL_SIZE + PADDING, + position[1] * CELL_SIZE + PADDING) rect = pygame.Rect(position, (40, 40)) pygame.draw.rect(display, color, rect) @@ -26,3 +27,17 @@ def draw_path(display, color, path): n1 = center_line(gridtoscreen(path[i].g_pos())) n2 = center_line(gridtoscreen(path[i + 1].g_pos())) pygame.draw.line(display, color, n1, n2, 4) + + +def draw_grid(display, grid): + for row in range(0, ROWS): + for column in range(0, COLS): + node = grid[row][column] + if node.celltype == 0: + draw_cell(display, (row, column), CELL_COLOR) + elif node.celltype == 1: + draw_cell(display, (row, column), WALL_COLOR) + elif node.celltype == 2: + draw_cell(display, (row, column), START_COLOR) + elif node.celltype == 3: + draw_cell(display, (row, column), END_COLOR) diff --git a/main.py b/main.py index 5f01d3d..ed61d78 100644 --- a/main.py +++ b/main.py @@ -1,31 +1,44 @@ -import numpy as np import sys import pygame from pygame.locals import * -COLS = 10 -ROWS = 10 -array = np.zeros((ROWS, COLS)) +from consts import ROWS, COLS, WIN_WIDTH, WIN_HEIGHT +from astar import Node, a_star +from astar import WALL, END, START -array[0][0] = 2.0 +from drawing import draw_cell, draw_grid, draw_path + + +def create_grid(): + grid = [] + for i in range(0, ROWS): + grid.append([]) + for j in range(0, COLS): + grid[i].append(Node(i, j)) + + return grid FPS = 60 fpsClock = pygame.time.Clock() -WIN_WIDTH = 500 -WIN_HEIGHT = 500 Window = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT)) BACKGROUND = (0, 0, 0) - - -def draw_cell(display, position, color): - position = (position[0] * 50 + 5, position[1] * 50 + 5) - rect = pygame.Rect(position, (40, 40)) - pygame.draw.rect(display, color, rect) +PATH_COLOR = (128, 128, 128) def main(): + grid = create_grid() + + start = grid[0][0] + start.celltype = START + end = grid[ROWS - 1][COLS - 1] + end.celltype = END + + path = a_star(grid, start, end) + if path is None: + print("No path") + looping = True while looping: for event in pygame.event.get(): @@ -38,19 +51,15 @@ def main(): Window.fill(BACKGROUND) # Clear display # Draw grid - for row in range(0, ROWS): - for column in range(0, COLS): - value = array[row][column] - if value == 0.0: - draw_cell(Window, (row, column), (255, 255, 255)) - elif value == 1.0: - draw_cell(Window, (row, column), (0, 255, 0)) - elif value == 2.0: - draw_cell(Window, (row, column), (255, 0, 0)) + draw_grid(Window, grid) + draw_path(Window, PATH_COLOR, path) pygame.display.update() # Swap render buffer fpsClock.tick(FPS) + pygame.quit() + sys.exit() + if __name__ == "__main__": pygame.init()