-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
144 lines (117 loc) · 4 KB
/
main.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from tkinter import messagebox, Tk
import pygame
import sys
window_width = 500
window_height = 500
window = pygame.display.set_mode((window_width, window_height))
columns = 25
rows = 25
box_width = window_width // columns
box_height = window_height // rows
grid = []
queue = []
path = []
class Box:
def __init__(self, i, j):
self.x = i
self.y = j
self.start = False
self.wall = False
self.target = False
self.queued = False
self.visited = False
self.neighbours = []
self.prior = None
def draw(self, win, color):
pygame.draw.rect(win, color, (self.x * box_width, self.y * box_height, box_width -2, box_height -2))
def set_neighbours(self):
if self.x > 0:
self.neighbours.append(grid[self.x - 1][self.y])
if self.x < columns - 1:
self.neighbours.append(grid[self.x + 1][self.y])
if self.y > 0:
self.neighbours.append(grid[self.x][self.y - 1])
if self.y < rows - 1:
self.neighbours.append(grid[self.x][self.y + 1])
#create grid
for i in range(columns):
arr = []
for j in range(rows):
arr.append(Box(i, j))
grid.append(arr)
#set neighbours
for i in range(columns):
for j in range(rows):
grid[i][j].set_neighbours()
start_box = grid[0][0]
start_box.start = True
start_box.visited = True
queue.append(start_box)
def main():
begin_search = False
target_box_set = False
searching = True
target_box = None
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#mouse controlls
elif event.type == pygame.MOUSEMOTION:
x = pygame.mouse.get_pos()[0]
y = pygame.mouse.get_pos()[1]
# Draw wall
if event.buttons[0]:
i = x// box_width
j =y// box_height
grid[i][j].wall = True
#set target
if event.buttons[2] and not target_box_set:
i = x// box_width
j =y// box_height
target_box = grid[i][j]
target_box.target = True
target_box_set = True
# start algorithm
if event.type == pygame.KEYDOWN and target_box_set:
begin_search = True
if begin_search:
if len(queue) > 0 and searching:
current_box = queue.pop(0)
current_box.visited = True
if current_box == target_box:
searching = False
while current_box.prior != start_box:
path.append(current_box.prior)
current_box = current_box.prior
else:
for neighbour in current_box.neighbours:
if not neighbour.queued and not neighbour.wall:
neighbour.queued = True
neighbour.prior = current_box
queue.append(neighbour)
else:
if searching:
Tk().wm_withdraw()
messagebox.showinfo("No Solution", "There is no solutios!")
searching = False
window.fill((0, 0, 0))
for i in range(columns):
for j in range(rows):
box = grid[i][j]
box.draw(window,(20,20,20))
if box.queued:
box.draw(window,(200,0,0))
if box.visited:
box.draw(window,(0,200,0))
if box in path:
box.draw(window,(0,0,200))
if box.start:
box.draw(window,(0,200,200))
if box.wall:
box.draw(window,(90,90,90))
if box.target:
box.draw(window,(200,200,0))
pygame.display.flip()
main()