Fininsh tidying

This commit is contained in:
__
2024-04-29 01:19:47 +02:00
parent 614e5393e3
commit 40e719aa11
4 changed files with 62 additions and 34 deletions

View File

@@ -1,15 +1,5 @@
from numpy import Infinity from numpy import Infinity
from consts import *
CELL = 0
WALL = 1
START = 2
END = 3
ROWS = 10
COLS = 10
class Node: class Node:
def __init__(self, x, y) -> None: def __init__(self, x, y) -> None:

View File

@@ -9,3 +9,17 @@ PADDING = 5
CELL_SIZE = WIN_WIDTH / COLS CELL_SIZE = WIN_WIDTH / COLS
CELL_SIZE_PADDED = CELL_SIZE - PADDING * 2 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)

View File

@@ -5,7 +5,8 @@ from consts import *
def draw_cell(display, position, color): 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)) rect = pygame.Rect(position, (40, 40))
pygame.draw.rect(display, color, rect) pygame.draw.rect(display, color, rect)
@@ -26,3 +27,17 @@ def draw_path(display, color, path):
n1 = center_line(gridtoscreen(path[i].g_pos())) n1 = center_line(gridtoscreen(path[i].g_pos()))
n2 = center_line(gridtoscreen(path[i + 1].g_pos())) n2 = center_line(gridtoscreen(path[i + 1].g_pos()))
pygame.draw.line(display, color, n1, n2, 4) 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)

53
main.py
View File

@@ -1,31 +1,44 @@
import numpy as np
import sys import sys
import pygame import pygame
from pygame.locals import * from pygame.locals import *
COLS = 10 from consts import ROWS, COLS, WIN_WIDTH, WIN_HEIGHT
ROWS = 10 from astar import Node, a_star
array = np.zeros((ROWS, COLS)) 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 FPS = 60
fpsClock = pygame.time.Clock() fpsClock = pygame.time.Clock()
WIN_WIDTH = 500
WIN_HEIGHT = 500
Window = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT)) Window = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
BACKGROUND = (0, 0, 0) BACKGROUND = (0, 0, 0)
PATH_COLOR = (128, 128, 128)
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)
def main(): 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 looping = True
while looping: while looping:
for event in pygame.event.get(): for event in pygame.event.get():
@@ -38,19 +51,15 @@ def main():
Window.fill(BACKGROUND) # Clear display Window.fill(BACKGROUND) # Clear display
# Draw grid # Draw grid
for row in range(0, ROWS): draw_grid(Window, grid)
for column in range(0, COLS): draw_path(Window, PATH_COLOR, path)
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))
pygame.display.update() # Swap render buffer pygame.display.update() # Swap render buffer
fpsClock.tick(FPS) fpsClock.tick(FPS)
pygame.quit()
sys.exit()
if __name__ == "__main__": if __name__ == "__main__":
pygame.init() pygame.init()