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
|
import random import pygame from draw_maze import * from maze import * from maze_def import *
def find_wall(walls, x, y, z,levels, rows, cols): ret = [] if x > 1 and walls[x-1][y][z] == WALL: ret.append((x-1,y,z, LR)) if y > 1 and walls[x][y-1][z] == WALL: ret.append((x,y-1,z, FB)) if x < cols - 2 and walls[x+1][y][z] == WALL: ret.append((x+1,y,z, LR)) if y < rows - 2 and walls[x][y+1][z] == WALL: ret.append((x,y+1,z, FB)) if z > 0 and walls[x][y][z-1] == WALL: ret.append((x,y,z-1, UD)) if z < levels-1 and walls[x][y][z+1] == WALL: ret.append((x,y,z+1, UD)) return ret
def prim_maze_demo(levels, rows, cols): levels=2*levels-1 rows=2*rows+1 cols=2*cols+1 maze_map=maze_init_draw(levels,rows,cols) x=1 y=1 z=0 maze_map[x][y][z] = CELL_VISIT walllist=[] ws = find_wall(maze_map, x, y, z, levels, rows, cols) walllist.extend(ws) posx,posy=None,None while True: for event in pygame.event.get(): if event.type == pygame.QUIT: return if walllist: x, y, z, d = random.choice(walllist) walllist.remove((x,y,z,d)) maze_map[x][y][z]=WALL_VISIT if d == FB: if (maze_map[x][y-1][z] == CELL_NO_VISIT or maze_map[x][y+1][z] == CELL_NO_VISIT) and (maze_map[x][y-1][z] != maze_map[x][y+1][z]): maze_map[x][y][z]=NOWALL if maze_map[x][y-1][z] == CELL_NO_VISIT: gy=y-1 else: gy=y+1 maze_map[x][gy][z]=CELL_VISIT ws = find_wall(maze_map, x, gy , z, levels, rows,cols) walllist.extend(ws) elif d == LR: if (maze_map[x-1][y][z] == CELL_NO_VISIT or maze_map[x+1][y][z] == CELL_NO_VISIT) and (maze_map[x-1][y][z] != maze_map[x+1][y][z]): maze_map[x][y][z]=NOWALL if maze_map[x-1][y][z] == CELL_NO_VISIT: gx=x-1 else: gx=x+1 maze_map[gx][y][z]=CELL_VISIT ws = find_wall(maze_map, gx, y , z, levels, rows,cols) walllist.extend(ws) elif d == UD: if (maze_map[x][y][z-1] == CELL_NO_VISIT or maze_map[x][y][z+1] == CELL_NO_VISIT) and (maze_map[x][y][z-1] != maze_map[x][y][z+1]): maze_map[x][y][z]=NOWALL if maze_map[x][y][z-1] == CELL_NO_VISIT: gz=z-1 maze_map[x][y][gz]=STAIRS_U if maze_map[x][y][z+1] == CELL_VISIT: maze_map[x][y][z+1]=STAIRS_D else: maze_map[x][y][z+1]=STAIRS_UD else: gz=z+1 maze_map[x][y][gz]=STAIRS_D if maze_map[x][y][z-1] == CELL_VISIT: maze_map[x][y][z-1]=STAIRS_U else: maze_map[x][y][z-1]=STAIRS_UD ws = find_wall(maze_map, x, y , gz, levels, rows,cols) walllist.extend(ws)
for x1,y1,z1,d1 in walllist: if d1 == FB: if y1 > 1 and y1 < rows-2 and maze_map[x1][y1-1][z1] != CELL_NO_VISIT and maze_map[x1][y1+1][z1] != CELL_NO_VISIT: walllist.remove((x1,y1,z1,d1)) maze_map[x1][y1][z1]=WALL_VISIT elif d1 == LR: if x1 > 1 and x1 < cols-2 and maze_map[x1-1][y1][z1] != CELL_NO_VISIT and maze_map[x1+1][y1][z1] != CELL_NO_VISIT: walllist.remove((x1,y1,z1,d1)) maze_map[x1][y1][z1]=WALL_VISIT elif d1 == UD: if z1 > 0 and z1 < levels-1 and maze_map[x1][y1][z1-1] != CELL_NO_VISIT and maze_map[x1][y1][z1+1] != CELL_NO_VISIT: walllist.remove((x1,y1,z1,d1)) maze_map[x1][y1][z1]=WALL_VISIT draw_maze(maze_map, levels, rows, cols, (x,y,z), not walllist) time_passed = clock.tick(30) pygame.display.update() return
if __name__ == "__main__": '''main''' prim_maze_demo(3, 5, 8)
|