-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyMaze.py
364 lines (283 loc) · 12.3 KB
/
myMaze.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# Author: William
# Description: This is a maze solving algorithm. This is an implementation of the Wall Follower Algorithm.
# It uses the "left hand" of the robot as a means to solve the maze.
# Date: April 23, 2018
import sys, pygame
from pygame.locals import *
size_x = 32
size_y = 32
change_x = 32
change_y = 32
Size = 32
SPEED = 32
timer = 375
DAY9YELLOW = (255, 167, 26)
GOAL = (245, 163, 183)
DANGER = (255, 0, 0)
CHECKPOINT = (0, 255, 0)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREY = (200, 200, 200)
BLUE = (50, 50, 255)
m = 21
n = 21
goal_x = 640
goal_y = 576
# List to hold all the sprites
all_sprite_list = pygame.sprite.Group()
clock = pygame.time.Clock()
# x--->
# 0 ------------------> 21
maze = [ [1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], # 0 y
[1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,1], # 1 |
[1,0,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,1], # 2 \ | /
[1,0,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,1,1,1], # 3 \ /
[1,0,1,0,0,0,5,0,0,0,1,0,0,0,0,0,0,0,0,1,1,1], # 4 `
[1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,1], # 5
[1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,3,0,0,1,1,1], # 6
[1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,1], # 7
[1,1,1,1,1,1,1,0,0,1,1,0,0,0,0,0,0,5,0,1,1,1], # 8
[1,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,1,1], # 9
[1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,1,1], # 10
[1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,1,1], # 11
[1,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1], # 12
[1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1], # 13
[1,0,0,3,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1], # 14
[1,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1], # 15
[1,1,1,0,5,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1,1,1], # 16
[1,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,3,0,0,1,1,1], # 17
[1,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1,1,1], # 18
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1], # 19
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1], # 20
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1] ] # 21
class Robot(pygame.sprite.Sprite):
def __init__(self, x, y):
# Call the parent's constructor
super().__init__()
# Set height, width
self.image = pygame.Surface([Size, Size])
self.image.fill(DAY9YELLOW)
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.y = y
self.rect.x = x
self.face = 5
self.lhs = 4
# Set speed vector
self.change_x = 0
self.change_y = 0
self.walls = None
self.goals = None
self.space = None
def update(self):
""" Update the robot position. """
# Move left/right=====
self.rect.x += self.change_x
if((self.change_x<0) & (maze[int(self.rect.x/32)][int(self.rect.y/32)+1] != 1)):
print ("Going West, Turning Left")
self.face = (self.face - 1) % 4
self.lhs = (self.lhs - 1) % 4
if((self.change_x>0) & (maze[int(self.rect.x/32)][int(self.rect.y/32)-1] != 1)):
print ("Going East, Turning Left")
self.face = (self.face - 1) % 4
self.lhs = (self.lhs - 1) % 4
block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)
for block in block_hit_list:
# Wall to the east
if ((self.change_x > 0) & (self.face %4 == 1)):
if((maze[int(self.rect.x/32)][int(self.rect.y/32)] == 0)):
print("what")
self.rect.x -= self.change_x
print ("You've hit a wall at the East")
self.rect.right = block.rect.left
print ("Turning Right")
self.face = (1 + self.face ) % 4
self.lhs = (1 + self.lhs) % 4
# Wall to the west
elif ((self.change_x < 0) & (self.face %4 ==3)):
self.rect.x -= self.change_x
print ("You've hit a wall at the West!")
self.rect.left = block.rect.right
print ("Turning Right")
self.face = (self.face + 1) % 4
self.lhs = (1 + self.lhs) % 4
self.rect.y += self.change_y
if((self.change_y>0) & (maze[int(self.rect.x/32)+1][int(self.rect.y/32)] != 1)):
print ("Going South, Turning Left")
self.face = (self.face - 1) % 4
self.lhs = (self.lhs - 1) % 4
if((self.change_y<0) & (maze[int(self.rect.x/32)-1][int(self.rect.y/32)] != 1)):
print ("Going North, Turning Left")
self.face = (self.face - 1) % 4
self.lhs = (self.lhs - 1) % 4
block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)
for block in block_hit_list:
# Wall to the South
if ((self.change_y > 0) & (self.face %4 == 2)):
self.rect.y -= self.change_y
print ("You've hit a wall at the South!")
self.rect.bottom = block.rect.top
print ("Turning Right")
self.face = (1 + self.face ) % 4
self.lhs = (1 + self.lhs) % 4
# Wall to the North
elif ((self.change_y < 0) & (self.face %4 == 0)):
self.rect.y -= self.change_y
print ("You've hit a wall at the North!")
self.rect.top = block.rect.bottom
print ("Turning Right")
self.face = (self.face + 1) % 4
self.lhs = (1 + self.lhs) % 4
if(self.rect.x == goal_x) & (self.rect.y == goal_y):
print("You've solved the maze! Hooray!!!")
pygame.quit()
sys.exit(0)
self.change_x = 0
self.change_y = 0
class Wall(pygame.sprite.Sprite):
""" Wall the robot can run into. """
def __init__(self, x, y, width, height):
""" Constructor for the wall that the robot can run into. """
super().__init__()
# Make a black wall, of the size specified in the parameters
self.image = pygame.Surface([width, height])
self.image.fill(BLACK)
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.y = y
self.rect.x = x
class Space(pygame.sprite.Sprite):
""" Wall the robot can run into. """
def __init__(self, x, y, width, height):
""" Constructor for the wall that the robot can run into. """
super().__init__()
# Make a transparent wall, of the size specified in the parameters
self.image = pygame.Surface([width, height], pygame.SRCALPHA, 32)
self.image = self.image.convert_alpha()
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.y = y
self.rect.x = x
class Goal(pygame.sprite.Sprite):
""" Wall the robot can run into. """
def __init__(self, x, y, width, height):
""" Constructor for the wall that the robot can run into. """
# Call the parent's constructor
super().__init__()
# Make the goal, of the size specified in the parameters
self.image = pygame.Surface([width, height])
self.image.fill(GOAL)
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.y = y
self.rect.x = x
class Danger(pygame.sprite.Sprite):
""" Wall the robot can run into. """
def __init__(self, x, y, width, height):
""" Constructor for the wall that the robot can run into. """
# Call the parent's constructor
super().__init__()
# Make the goal, of the size specified in the parameters
self.image = pygame.Surface([width, height])
self.image.fill(DANGER)
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.y = y
self.rect.x = x
class CheckPoint(pygame.sprite.Sprite):
""" Wall the robot can run into. """
def __init__(self, x, y, width, height):
""" Constructor for the wall that the robot can run into. """
# Call the parent's constructor
super().__init__()
# Make the goal, of the size specified in the parameters
self.image = pygame.Surface([width, height])
self.image.fill(CHECKPOINT)
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.y = y
self.rect.x = x
# Left-Hand Rule wall following algorithm
def LHRwallFollowing(robot, screen):
face = robot.face # 0 is north, 1 is east, 2 is south, 3 is west
LHS = robot.lhs
position = robot
#lhs is north
if(LHS%4 == 0):
print("Current Position: (", int((robot.rect.x/32)+2),", ", int((robot.rect.y/32)-1), ") --Going East")
robot.change_x += SPEED
#lhs is east
if(LHS%4 == 1):
print("Current Position: (", int((robot.rect.x/32)+2),", ", int((robot.rect.y/32)-1), ") --Going South")
robot.change_y += SPEED
#lhs is south
if(LHS%4 == 2):
print("Current Position: (", int((robot.rect.x/32)+2),", ", int((robot.rect.y/32)-1), ") --Going West")
robot.change_x += -SPEED
#lhs is west
if(LHS%4 == 3):
print("Current Position: (", int((robot.rect.x/32)+2), ", ", int((robot.rect.y/32)-1), ") --Going North")
robot.change_y += -SPEED
def main():
pygame.init()
size = width, height =640, 640
screen = pygame.display.set_mode(size)
pygame.display.set_caption('Maze Solving Project by William Z.')
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill(WHITE)
# Make the walls. (x_pos, y_pos, width, height)
wall_list = pygame.sprite.Group()
# Make the goal. (x_pos, y_pos, width, height)
goal_list = pygame.sprite.Group()
# Make the space. (x_pos, y_pos, width, height)
space_list = pygame.sprite.Group()
# Make the danger. (x_pos, y_pos, width, height)
danger_list = pygame.sprite.Group()
# Make the danger. (x_pos, y_pos, width, height)
checkpoint_list = pygame.sprite.Group()
for j in range(0, n):
for i in range(0, m):
if(maze[i][j] == 1):
wall = Wall(i*32, j*32, Size, Size)
wall_list.add(wall)
all_sprite_list.add(wall)
if(maze[i][j] == 2):
goal = Goal(i*32, j*32, Size, Size)
goal_list.add(wall)
all_sprite_list.add(goal)
if(maze[i][j] == 0):
space = Space(i*32, j*32, Size, Size)
space_list.add(space)
all_sprite_list.add(space)
if(maze[i][j] == 3):
danger = Danger(i*32, j*32, Size, Size)
danger_list.add(danger)
all_sprite_list.add(danger)
if(maze[i][j] == 5):
checkpoint = CheckPoint(i*32, j*32, Size, Size)
checkpoint_list.add(checkpoint)
all_sprite_list.add(checkpoint)
robot = Robot(-64, 32)
robot.face = 1
robot.lhs = 0
robot.walls = wall_list
robot.goals = goal_list
robot.space = space_list
all_sprite_list.add(robot)
clock = pygame.time.Clock()
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
LHRwallFollowing(robot, screen)
all_sprite_list.update()
screen.fill(GREY)
all_sprite_list.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.time.wait(timer)
pygame.quit()
if __name__ == '__main__':
main()