Fininsh tidying
This commit is contained in:
53
main.py
53
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()
|
||||
|
||||
Reference in New Issue
Block a user