-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze_setup.py
110 lines (97 loc) · 2.51 KB
/
maze_setup.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
import pygame
import random
import time
import numpy
from maze_generator import *
pygame.init()
##maze = [[3, 1, 0, 0, 0],
## [0, 0, 0, 1, 0],
## [0, 1, 1, 0, 0],
## [1, 0, 0, 0, 1],
## [0, 0, 1, 0, 4]]
print(maze)
size = numpy.size(maze,1)
walls = []
path = []
screen = pygame.display.set_mode([size*60,size*60])
screen.fill((255,255,255))
time.sleep(10)
for i in range(0,size):
try:
y = maze[i].index(2)
x = i
break
except:
continue
print((x,y))
path.append((x,y))
while True:
for r in range(0,size):
for c in range(0,size):
if maze[r][c] == 0:
color = (255,255,255)
elif maze[r][c] == 1:
color = (0,0,0)
elif maze[r][c] == 2:
color = (0,255,0)
elif maze[r][c] == 3:
color = (255,0,0)
pygame.draw.rect(screen, color, pygame.Rect(c*60,r*60,60,60))
pygame.display.flip()
pygame.draw.rect(screen,(0,0,255),pygame.Rect(y*60,x*60,60,60))
pygame.display.flip()
if maze[x][y] == 3:
break
a = x
b = y
while True:
i = random.choice(['x','y'])
if i == 'x':
z = random.choice((-1,1))
x = x + z
if i == 'y':
z = random.choice((-1,1))
y = y + z
if (x,y) in walls:
x = a
y = b
continue
elif x < 0:
walls.append((x,y))
x = a
y = b
continue
elif x == size:
walls.append((x,y))
x = a
y = b
continue
elif y < 0:
walls.append((x,y))
x = a
y = b
continue
elif y == size:
walls.append((x,y))
x = a
y = b
continue
elif maze[x][y] == 1:
walls.append((x,y))
x = a
y = b
continue
elif ((x,y) in path) and (((a+1,b) in walls or (a+1,b) in path) and ((a-1,b) in walls or (a-1,b) in path) and ((a,b+1) in walls or (a,b+1) in path) and ((a,b-1) in walls or (a,b-1) in path)):
walls.append((a,b))
pygame.draw.rect(screen, [0,255,255], pygame.Rect(b*60,a*60,60,60))
pygame.display.flip()
break
elif (x,y) in path:
x = a
y = b
continue
else:
path.append((x,y))
break
print((x,y))
time.sleep(.5)