-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStraight Mazes.py
191 lines (150 loc) · 4.86 KB
/
Straight Mazes.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
import pygame
from random import randint, choice
import os
from math import sqrt
wait = False
# . is walls, # is nothing, @ is finish, & is start
maze = []
maze_size = 0
while not (maze_size >= 5):
maze_size = input("How big do you want it to be? ")
try:
maze_size = int(maze_size)
if not maze_size >= 5:
print("Please make it at least 5!")
pass
except ValueError:
print("Please make it an integer.")
maze_size = 0
for y in range(maze_size):
row = []
for x in range(maze_size):
row.append(".")
maze.append(row)
cells_list = []
white = (255,255,255)
black = (0,0,0)
blue = (0,0,255)
green = (0,255,0)
purple = (255,0,255)
players = []
creating = True
events = []
wait_time = 0
path = os.path.dirname(__file__)
if not os.path.exists(os.path.join(path, "mazes")):
os.makedirs(os.path.join(path, "mazes"))
def save_maze(board):
save_name = ""
while len(save_name) == 0:
save_name = input("What do you want to save it as? (Don't include the extension) ")
save_name = os.path.join(path, "mazes", save_name + ".txt")
file = open(save_name, "w+")
file.write("width: " + str(maze_size) + "\n")
temp_board = board
for row in temp_board:
row = str(row)
row = row.replace("[","")
row = row.replace("]","")
row = row.replace("'","")
row = row.replace(" ","")
file.write(row + "\n")
def neighbors(cell):
neighbors = []
x = cell[0]
y = cell[1]
if x-2 >= 0 and maze[y][x-2] == ".":
neighbors.append([x-2,y])
if x+2 <= maze_size - 1 and maze[y][x+2] == ".":
neighbors.append([x+2,y])
if y-2 >= 0 and maze[y-2][x] == ".":
neighbors.append([x,y-2])
if y+2 <= maze_size - 1 and maze[y+2][x] == ".":
neighbors.append([x,y+2])
return neighbors
def create_passage(cell, cell2):
if cell[0] > cell2[0]:
maze[cell[1]][cell[0]-1] = "#"
if cell[0] < cell2[0]:
maze[cell[1]][cell[0]+1] = "#"
if cell[1] > cell2[1]:
maze[cell[1]-1][cell[0]] = "#"
if cell[1] < cell2[1]:
maze[cell[1]+1][cell[0]] = "#"
def set_start_and_end():
x = randint(0,maze_size-1)
y = randint(0,maze_size-1)
x2 = randint(0,maze_size-1)
y2 = randint(0,maze_size-1)
while not (sqrt((x2-x)**2 + (y2-y)**2) > maze_size/2 * 1.5 and maze[y][x] == "#" and maze[y2][x2] == "#"):
x = randint(0,maze_size-1)
y = randint(0,maze_size-1)
x2 = randint(0,maze_size-1)
y2 = randint(0,maze_size-1)
maze[y][x] = "@"
maze[y2][x2] = "&"
def create():
global cells_list
global creating
start_cell = [-1,-1]
while start_cell[0] % 2 == 1 and start_cell[0] < 0 and start_cell[1] % 2 == 1 and start_cell[1] < 0:
start_cell = [randint(1,maze_size-2),randint(1,maze_size-2)]
cells_list.append([start_cell[0],start_cell[1]])
maze[cells_list[0][1]][cells_list[0][0]] = "#"
while len(cells_list) > 0:
current_cell = cells_list[0]
if len(neighbors(current_cell)) > 0:
random_neighbor = choice(neighbors(current_cell))
maze[random_neighbor[1]][random_neighbor[0]] = "#"
create_passage(current_cell, random_neighbor)
cells_list.append(random_neighbor)
else:
cells_list.remove(current_cell)
get_events()
handle_events(events)
if wait:
draw(maze)
pygame.time.wait(wait_time)
set_start_and_end()
def get_events():
global events
events = []
for event in pygame.event.get():
events.append(event)
def handle_events(events_list):
global playing
for event in events_list:
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q or event.key == pygame.K_ESCAPE:
playing = False
pygame.quit()
quit()
elif event.key == pygame.K_s:
save_maze(maze)
def draw(board):
screen.fill(white)
for x in range(len(board)):
for y in range(len(board[x])):
if board[x][y] == ".":
pygame.draw.rect(screen, black, [y*tile_size,x*tile_size,tile_size,tile_size])
elif board[x][y] == "@":
pygame.draw.rect(screen, blue, [y*tile_size,x*tile_size,tile_size,tile_size])
elif board[x][y] == "&":
pygame.draw.rect(screen, purple, [y*tile_size,x*tile_size,tile_size,tile_size])
pygame.display.update()
tile_size = int(600/len(maze))
pygame.init()
size = [len(maze)*tile_size,len(maze[0])*tile_size]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Maze Fun!")
create()
while True:
get_events()
handle_events(events)
draw(maze)
pygame.time.wait(wait_time)
pygame.quit()
quit()