-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
155 lines (124 loc) · 5.24 KB
/
main.py
File metadata and controls
155 lines (124 loc) · 5.24 KB
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
145
146
147
148
149
150
151
152
153
154
155
from settings import *
import UIfunc as uf
import PathFinder.PathFindingFunc as pf
"""
col and row are mostly for position on display
x and y are mostly for idx of the grid list or for cell position
"""
class DISPLAY:
def __init__(self):
self.walls = []
self.grid = uf.createGrid()
self.start = self.grid[20][20] # random position on the grid
self.end = self.grid[20][21] # random position on the grid
# init position
pf.initStartEnd(self.start, self.end)
self.alg = 'BFS' # default
def run(self): # main loop
run = True
clock = pygame.time.Clock()
delay = 0
# events
startEnd = (False, -1) # drag, id--> 0=start,1=end
startEndPos = (0, 0)
wallDrag = False
removeDrag = False
# init buttons
uf.loadBUttons()
# init sliders
uf.loadSlider()
while run:
# update events
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
uf.buttonHover()
if event.type == pygame.MOUSEBUTTONDOWN: # mouse button pressed
mouseY, mouseX = uf.getMousePos()
# left click down
if event.button == 1:
if uf.valid(mouseX, mouseY):
state = self.grid[mouseY][mouseX].state
if state == 1: # current state is the start
startEnd = (True, 0)
startEndPos = (mouseY, mouseX)
elif state == 2: # current state is the end
startEnd = (True, 1)
startEndPos = (mouseY, mouseX)
elif state == 0 or state == 3:
wallDrag = True
# check if button is clicked
self.alg = uf.buttonClicked(self.alg)
uf.sliderClicked()
# right click down
if event.button == 3:
mouseY, mouseX = uf.getMousePos()
current = self.grid[mouseY][mouseX]
if current.state == 3 or current.state == 0:
removeDrag = True
if event.type == pygame.MOUSEBUTTONUP:
# left click up
if event.button == 1:
startEnd = (False, -1)
wallDrag = False
# right click up
if event.button == 3:
removeDrag = False
# unclick sliders
for s in sliders:
s.hit = False
if event.type == pygame.KEYDOWN:
# z key click -> clear all walls/paths
if event.key == pygame.K_z:
uf.clearALL(self.grid, self.walls)
# space key click -> start alg
if event.key == pygame.K_SPACE:
a = pf.drawPath(self.alg, self.start, self.end, self.walls, self.grid, delay)
if not a:
run = False
# x key click -> clear the paths created
if event.key == pygame.K_x:
uf.clearPath(self.grid)
# move start/end
if startEnd[0]:
mouseY, mouseX = uf.getMousePos() # new position
if uf.valid(mouseX, mouseY) and startEndPos != (mouseY, mouseX):
new = self.grid[mouseY][mouseX]
if new.state == 0:
SE = self.grid[startEndPos[0]][startEndPos[1]]
new.setCell(SE.state, SE.color, SE.fill)
SE.resetCell()
startEndPos = (mouseY, mouseX)
if startEnd[1] == 0:
self.start = new
elif startEnd[1] == 1:
self.end = new
# add walls
if wallDrag:
mouseY, mouseX = uf.getMousePos()
if uf.valid(mouseX, mouseY):
current = self.grid[mouseY][mouseX]
if current.state == 0 and current not in self.walls:
current.setWall()
self.walls.append(current)
# remove walls
if removeDrag:
mouseY, mouseX = uf.getMousePos()
if uf.valid(mouseX, mouseY):
current = self.grid[mouseY][mouseX]
if current.state == 3:
current.setCell(0, -1, False)
self.walls.remove(current)
# move sliders
for s in sliders:
if s.hit:
s.move()
if s.name == "speed":
# change speed
delay = 1 - s.val
# draw
uf.reDrawScreen(self.grid)
clock.tick(120)
pygame.display.quit()
if __name__ == '__main__':
DISPLAY().run()