-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
127 lines (102 loc) · 4.24 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import pygame
from Node import Node
from global_var import Win, WIDTH
from colors import *
from a_star import a_star, heuristic_func
from tkinter import *
from tkinter import messagebox
Tk().wm_withdraw() #to hide the main window
def create_grid(rows, width):
grid = []
gap = width // rows #integer division
for i in range(rows):
grid.append([])
for j in range(rows): #used rows here because A square grid is required
node = Node(i, j, gap, rows)
grid[i].append(node)
return grid
def draw_gridlines(win, rows, width):
gap = width // rows
for i in range(rows):
pygame.draw.line(win, grey, (0, i * gap), (width, i * gap))
for j in range(rows):
pygame.draw.line(win, grey, (j * gap, 0), (j * gap, width))
def draw(win, grid, rows, width):
win.fill(white)
for row in grid:
for node in row:
node.draw(win)
draw_gridlines(win, rows, width)
pygame.display.update()
def mouse_clicked_position(position, rows, width):
gap = width // rows
x = position[1]
y = position[0]
col = x // gap
row = y // gap
return row, col
def main(win, width):
rows = 40
#last: try to create a dynamic grid
grid = create_grid(rows, width)
start_position = None
end_position = None
run = True
Started = False
while run:
draw(win, grid, rows, width)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
grid = []
start_position = None
end_position = None
Started = False
grid = create_grid(rows, width)
draw(win, grid, rows, width)
if Started:
continue
if pygame.mouse.get_pressed()[0]:
position = pygame.mouse.get_pos()
row, col = mouse_clicked_position(position, rows, width)
node = grid[row][col]
if not start_position and node != end_position:
start_position = node
start_position.create_start_node()
elif not end_position and node != start_position:
end_position = node
end_position.create_end_node()
elif node != start_position and node != end_position:
node.create_barrier()
elif pygame.mouse.get_pressed()[2]:
position = pygame.mouse.get_pos()
row, col = mouse_clicked_position(position, rows, width)
node = grid[row][col]
node.reset()
if node == start_position:
start_position = None
if node == end_position:
end_position = None
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and start_position and end_position:
for row in grid:
for node in row:
node.update_neighbours(grid)
solution = a_star(lambda: draw(win, grid, rows, width), grid, start_position, end_position) #lambda is an anonymous funtion
#-----------------------------------------
# x = def func():
# print("hello")
# x()
#-----------------------------------------
# this can be summarized using lambda:
# x = lambda: print("hello")
if solution == False:
messagebox.showinfo(title='Path Doesnt Exist', message="No Path Found")
if not start_position and event.key == pygame.K_SPACE:
messagebox.showerror(title='Start-Position-404', message='Start Position not selected')
if not end_position and event.key == pygame.K_SPACE:
messagebox.showerror(title='End-Position-404', message='End Position not selected')
pygame.quit()
main(Win, WIDTH)