源码
原创文章禁止转载
maze_def,maze,draw_maze 地址
import random
from draw_maze import *
from maze_def import *
from maze import *
def visit_cell(maze_map, x, y, z, cols, rows):
if maze_map[x][y][z] == CELL_NO_VISIT:
maze_map[x][y][z] = CELL_VISIT
grids = []
minx = x
while True:
if minx > 1 and maze_map[minx-1][y][z] == CELL_NO_VISIT:
minx -=1
else:
break
maxx = x
while True:
if maxx < cols - 2 and maze_map[maxx+1][y][z] == CELL_NO_VISIT:
maxx+=1
else:
break
miny=y
while True:
if miny > 1 and maze_map[x][miny-1][z] == CELL_NO_VISIT:
miny -=1
else:
break
maxy=y
while True:
if maxy < rows - 2 and maze_map[x][maxy+1][z] == CELL_NO_VISIT:
maxy +=1
else:
break
for xi in range(minx, maxx+1):
for yi in range(miny, maxy+1):
if maze_map[xi][yi][z] == CELL_NO_VISIT:
maze_map[xi][yi][z] = CELL_VISIT
grids.append((xi,yi,z))
return grids
def depth_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_draw2(levels,rows,cols, 5, 3, 5)
nowx=1
nowy=1
nowz=0
history = []
grids = visit_cell(maze_map, nowx, nowy, nowz, cols, rows)
history.extend(grids)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
if history:
if maze_map[nowx][nowy][nowz] == CELL_NO_VISIT:
grids = visit_cell(maze_map, nowx, nowy, nowz, cols, rows)
history.extend(grids)
check = []
if nowx > 1:
if maze_map[nowx-2][nowy][nowz] == CELL_NO_VISIT:
check.append('L')
elif maze_map[nowx-1][nowy][nowz] == WALL_NO_VISIT:
maze_map[nowx-1][nowy][nowz] = WALL_VISIT
if nowy > 1 :
if maze_map[nowx][nowy-2][nowz] == CELL_NO_VISIT:
check.append('F')
elif maze_map[nowx][nowy-1][nowz] == WALL_NO_VISIT:
maze_map[nowx][nowy-1][nowz] = WALL_VISIT
if nowx < cols-2:
if maze_map[nowx+2][nowy][nowz] == CELL_NO_VISIT:
check.append('R')
elif maze_map[nowx+1][nowy][nowz] == WALL_NO_VISIT:
maze_map[nowx+1][nowy][nowz] = WALL_VISIT
if nowy < rows-2:
if maze_map[nowx][nowy+2][nowz] == CELL_NO_VISIT:
check.append('B')
elif maze_map[nowx][nowy+1][nowz] == WALL_NO_VISIT:
maze_map[nowx][nowy+1][nowz] = WALL_VISIT
if nowz < levels-2:
if maze_map[nowx][nowy][nowz+2] == CELL_NO_VISIT:
check.append('U')
elif maze_map[nowx][nowy][nowz+1] == WALL_NO_VISIT:
maze_map[nowx][nowy][nowz+1] = WALL_VISIT
if nowz > 1:
if maze_map[nowx][nowy][nowz-2] == CELL_NO_VISIT:
check.append('D')
elif maze_map[nowx][nowy][nowz-1] == WALL_NO_VISIT:
maze_map[nowx][nowy][nowz-1] = WALL_VISIT
if len(check):
move_direction = random.choice(check)
if move_direction == 'L':
maze_map[nowx-1][nowy][nowz] = NOWALL
nowx=nowx-2
if move_direction == 'F':
maze_map[nowx][nowy-1][nowz] = NOWALL
nowy=nowy-2
if move_direction == 'R':
maze_map[nowx+1][nowy][nowz] = NOWALL
nowx=nowx+2
if move_direction == 'B':
maze_map[nowx][nowy+1][nowz] = NOWALL
nowy=nowy+2
if move_direction == 'U':
maze_map[nowx][nowy][nowz+1] = NOWALL
if maze_map[nowx][nowy][nowz] == STAIRS_D:
maze_map[nowx][nowy][nowz] = STAIRS_UD
else:
maze_map[nowx][nowy][nowz] = STAIRS_U
if maze_map[nowx][nowy][nowz+2] == STAIRS_U:
maze_map[nowx][nowy][nowz+2] = STAIRS_UD
else:
maze_map[nowx][nowy][nowz+2] = STAIRS_D
nowz=nowz+2
if move_direction == 'D':
maze_map[nowx][nowy][nowz-1] = NOWALL
if maze_map[nowx][nowy][nowz] == STAIRS_U:
maze_map[nowx][nowy][nowz] = STAIRS_UD
else:
maze_map[nowx][nowy][nowz] = STAIRS_D
if maze_map[nowx][nowy][nowz-2] == STAIRS_D:
maze_map[nowx][nowy][nowz-2] = STAIRS_UD
else:
maze_map[nowx][nowy][nowz-2] = STAIRS_U
nowz=nowz-2
grids = visit_cell(maze_map, nowx, nowy, nowz, cols, rows)
history.extend(grids)
else:
nowx, nowy, nowz = history.pop()
draw_maze(maze_map, levels, rows, cols, (nowx,nowy,nowz), not history)
time_passed = clock.tick(20)
pygame.display.update()
return
if __name__ == "__main__":
'''main'''
depth_maze_demo(3, 15, 21)
生成的迷宫
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ba0752cc2490274431d5d9b9242ac3bf.png#pic_center)
GIF演示
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1d77d0e15fe0d093f37d99fc5e1310d3.gif#pic_center)