-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathFinder.py
More file actions
245 lines (132 loc) · 6.25 KB
/
PathFinder.py
File metadata and controls
245 lines (132 loc) · 6.25 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import pygame, random, math, time
WIDTH = 600
HEIGHT = 600
scale = 25 ## size of blocks in pixels
pygame.init()
screen = pygame.display.set_mode((WIDTH,HEIGHT))
## A couple colors
WHITE = (255,255,255)
RED = (255, 0, 0)
GREEN = (0,255,0)
BLACK = (0,0,0)
## the dictionary that keeps track of the grid
blocks = {}
class Block():
def __init__(self, rect, index):
self.index = index ## Where the block is in the dictionary
self.rect = rect ## pygame rect object
self.type = "empty"
self.color = BLACK
self.searched = False ## all start of not searched, this is so you don't search a block twice
self.parents = None ## parent is passed when a block is searched so that block knows where it came from
def becomeSearched(self, parent):
self.searched = True
self.color = (100,100,100)
self.parent = parent ## the block it came from
def setPath(self):
self.color = GREEN
self.type = "path"
def show(self):
pygame.draw.rect(screen, self.color, self.rect)
for event in pygame.event.get():
pass
pygame.display.update(self.rect) ## to save time I only update the display for each new block
def becomeGoal(self):
self.type = "goal"
self.color = GREEN
def becomeStart(self):
self.type = "start"
self.color = RED
def becomeObstacle(self):
self.type = "obs"
self.color = (0,0, 255)
def getAdjacent(index):
x , y = index
left= (x-1 , y)
right = (x+1 , y)
down = (x, y+1)
up = (x, y-1)
return([up,down,left,right]) ## returns all four blocks around a given rect
def initBlocks():
for x in range(math.floor(WIDTH / scale)): ##
for y in range(math.floor(HEIGHT / scale)):
local_rect = pygame.Rect(x*scale, y*scale , scale, scale)
blocks[(x,y)] = (Block(local_rect, (x,y))) ## creates a block class object with index = to the position in the blocks dictionary
active = [] ## this is a queue that is used to keep track of which block is being checked
def findPath():
for block in blocks.values():
if block.type == "start": ## finds starting block and adds to queue
active.append(block)
break
elif block.type == "goal": ## figures out which block you're getting to
goal = block
while len(active) > 0: ## while queue basically
current_block = active.pop(0) ## removes top from queue and makes current bloc
## ok because active is appened with current block later
blocksAround = getAdjacent(current_block.index)
random.shuffle(blocksAround) ## this isn't necessary, just adds some noise
for i in blocksAround: ## i is a key, like (3,4)
if i in blocks.keys(): ## prevents from checking blocks off screen
localBlock = blocks[i]
if localBlock.type == "goal": ## if the adjacent block is the goal, start path
while current_block != None:
current_block.setPath() ## becomes part of path, aka turns green
current_block.show()
current_block = current_block.parent ## repeats with the block it came from
if current_block.type == "start":
return ## to break out of the whole function "findPath"
if localBlock.type == "empty" and not localBlock.searched:
localBlock.becomeSearched(current_block) ## change to gray (grey) (?)
active.append(localBlock) ## if it's a new block add it to queue
localBlock.show()
#renderScreen()
initBlocks()
over = False
setUp1 = False
setUp2 = False
## These up coming while Loops are the work of a half asleep Eli, please don't judge##
while not setUp1:
mouse = pygame.mouse.get_pos()
left_click = pygame.mouse.get_pressed()[0] ## if I were smart I would've put this in the event loop
for block in blocks.values(): ## this is the actual block class object as opposed to the key
if left_click and block.rect.collidepoint(mouse): ## selects the block you clicked
block.becomeObstacle()
block.show()
for event in pygame.event.get():
if event.type == pygame.QUIT: ## this doesn't work until the last loop thanks to my dumb design
setUp1 = over
over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE: ## breaks out of the first loop
setUp1 = True
pygame.display.update()
while not setUp2:
mouse = pygame.mouse.get_pos()
left_click = pygame.mouse.get_pressed()[0]
for block in blocks.values():
if left_click and block.rect.collidepoint(mouse):
block.becomeStart()
block.show()
setUp2 = True ## breaks out of loop, next click is goal
pygame.display.update()
time.sleep(.5) ## this is here so you don't turn the start block into the goal immediately
for event in pygame.event.get():
if event.type == pygame.QUIT:
setUp2 = True
over = True
pygame.display.update()
setUp3 = False
while not setUp3:
mouse = pygame.mouse.get_pos()
left_click = pygame.mouse.get_pressed()[0]
for block in blocks.values():
if left_click and block.rect.collidepoint(mouse):
block.becomeGoal() ## greenify ! :)
block.show()
setUp3 = True
for event in pygame.event.get():
if event.type == pygame.QUIT:
setUp2 = over
over = True
pygame.display.update()
findPath() ## spooky magic