-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaze.py
More file actions
114 lines (82 loc) · 2.56 KB
/
maze.py
File metadata and controls
114 lines (82 loc) · 2.56 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
from PIL import Image
import pygame
import sys
from stack import Stack
from cell import Cell
import random
import time
import argparse
cols = 20; rows = 20; size = 15
direct = False
(width, height) = (cols*size, rows*size)
screen = pygame.display.set_mode((width+1, height+1))
cellMap = []
stack = Stack()
def createMap():
tempMap = []
for y in range(rows):
row = []
for x in range(cols):
row.append(Cell(x, y, size, screen))
tempMap.append(row)
return tempMap
def draw():
pygame.draw.rect(screen, (0, 0, 0), (0, 0, rows*size, cols*size))
for row in cellMap:
for element in row:
element.draw()
pygame.display.flip()
def generateMaze():
vistiedCells = 0
startCell = cellMap[0][0]
startCell.visited = True
stack.push(startCell)
while vistiedCells != cols*rows-1:
currentCell = stack.top()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
if currentCell == None:
stack.pop()
continue
neighbors = currentCell.getNeighbors(cellMap)
filteredNeighbors = list(filter(lambda c: c.visited == False, neighbors))
if len(filteredNeighbors) < 1:
stack.pop()
continue
else:
randomNr = random.randint(0, len(filteredNeighbors)-1)
randomNeighbor = filteredNeighbors[randomNr]
randomNeighbor.visited = True
randomNeighbor.connectedNeighbors.append(currentCell)
currentCell.connectedNeighbors.append(randomNeighbor)
Cell.currentSelected = currentCell
stack.push(randomNeighbor)
vistiedCells += 1
if direct == False:
draw()
def main():
global rows, cols, direct, screen, cellMap
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--size", type=int, nargs="+")
parser.add_argument("-d", "--direct", action="store_true")
args = parser.parse_args()
if args.size != None:
rows = args.size[0]
cols = args.size[1] if len(args.size) >= 2 else cols
else:
rows = 40
cols = 40
direct = args.direct
(width, height) = (cols*size, rows*size)
screen = pygame.display.set_mode((width+1, height+1))
pygame.display.set_caption('Maze generator')
cellMap = createMap()
generateMaze()
draw()
if __name__ == "__main__":
main()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)