| 12
 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
 
 | 
 
 import random
 from maze import *
 from draw_maze import *
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 def aldous_broder_maze_demo(levels, rows, cols):
 if 0 == levels % 2:
 levels+=1
 if 0 == rows % 2:
 rows+=1
 if 0 == cols % 2:
 cols+=1
 
 maze_map = maze_init_draw(levels, rows, cols)
 
 notusegrids = []
 for tz in range(0, levels):
 for tx in range(0, cols):
 for ty in range(0, rows):
 if maze_map[tx][ty][tz]==CELL_NO_VISIT:
 notusegrids.append((tx, ty, tz))
 
 elif maze_map[tx][ty][tz] == WALL_NO_VISIT:
 maze_map[tx][ty][tz] = WALL_VISIT
 
 nowx,nowy,nowz = random.choice(notusegrids)
 
 maze_map[nowx][nowy][nowz]=CELL_VISIT
 notusegrids.remove((nowx,nowy,nowz))
 posx,posy=None,None
 
 while True:
 for event in pygame.event.get():
 if event.type == pygame.QUIT:
 return
 
 if  notusegrids:
 directions = []
 
 if nowy > 1:
 directions.append('f')
 if nowx > 1:
 directions.append('l')
 if nowy < rows-2:
 directions.append('b')
 if nowx < cols-2:
 directions.append('r')
 if nowz < levels-1:
 directions.append('u')
 if nowz > 0:
 directions.append('d')
 if len(directions):
 
 move = random.choice(directions)
 
 
 
 
 if move == 'f':
 newy = nowy-2
 newx = nowx
 newz = nowz
 opwall=(nowx,nowy-1,nowz)
 if move == 'l':
 newy = nowy
 newx = nowx-2
 newz = nowz
 opwall=(nowx-1,nowy,nowz)
 if move == 'b':
 newy = nowy+2
 newx = nowx
 newz = nowz
 opwall=(nowx,nowy+1,nowz)
 if move == 'r':
 newy = nowy
 newx = nowx+2
 newz = nowz
 opwall=(nowx+1,nowy,nowz)
 if move == 'u':
 newy = nowy
 newx = nowx
 newz = nowz+2
 opwall=(nowx,nowy,nowz+1)
 if move == 'd':
 newy = nowy
 newx = nowx
 newz = nowz-2
 opwall=(nowx,nowy,nowz-1)
 
 if maze_map[newx][newy][newz] == CELL_NO_VISIT:
 
 if opwall:
 maze_map[opwall[0]][opwall[1]][opwall[2]] = NOWALL
 
 if move == 'd':
 if maze_map[nowx][nowy][nowz] == STAIRS_U:
 maze_map[nowx][nowy][nowz]=STAIRS_UD
 else:
 maze_map[nowx][nowy][nowz]=STAIRS_D
 maze_map[newx][newy][newz]=STAIRS_U
 elif move == 'u':
 if maze_map[nowx][nowy][nowz] == STAIRS_D:
 maze_map[nowx][nowy][nowz]=STAIRS_UD
 else:
 maze_map[nowx][nowy][nowz]=STAIRS_U
 maze_map[newx][newy][newz]=STAIRS_D
 else:
 maze_map[newx][newy][newz]=CELL_VISIT
 
 nowx=newx
 nowy=newy
 nowz=newz
 notusegrids.remove((newx,newy,newz))
 else:
 
 nowx=newx
 nowy=newy
 nowz=newz
 
 draw_maze(maze_map, levels, rows, cols, (nowx, nowy, nowz), not notusegrids)
 time_passed = clock.tick(30)
 pygame.display.update()
 return
 
 
 
 if __name__ == "__main__":
 '''main'''
 aldous_broder_maze_demo(4, 11, 15)
 
 
 
 |