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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
import random from draw_maze import * from maze_def import * from maze import *
def kruskal_maze(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) x=1 y=1 z=0 gridlist=[] gridlist.append((x,y,z)) collection =[] wallList=[] for zi in range(0, levels, 1): for xi in range(0, cols, 1): for yi in range(0, rows, 1): if xi%2 == 1 and yi%2==1 and zi%2==0: maze_map[xi][yi][zi]=CELL_NO_VISIT collection.append([(xi,yi,zi)]) continue if xi == 0: continue if yi == 0: continue if xi == cols-1: continue if yi == rows-1: continue if zi%2==0 and xi%2==0 and yi%2==0: continue if zi%2==1 and not (xi%2==1 and yi%2==1): continue if zi%2==1: wallList.append((xi,yi,zi, UD)) continue if xi%2==0: wallList.append((xi,yi,zi, LR)) continue if yi%2==0: wallList.append((xi,yi,zi, FB)) continue print("error")
while wallList: x,y,z,d = random.choice(wallList) wallList.remove((x,y,z,d)) if d == UD: grida = (x,y,z-1) gridb = (x,y,z+1) elif d==LR : grida = (x-1,y,z) gridb = (x+1,y,z) elif d==FB: grida = (x,y-1,z) gridb = (x,y+1,z)
colla = [] collb = [] for coll in collection: if grida in coll: colla = coll if gridb in coll: collb = coll maze_map[grida[0]][grida[1]][grida[2]] = CELL_VISIT maze_map[gridb[0]][gridb[1]][gridb[2]] = CELL_VISIT if colla != collb: maze_map[x][y][z] = NOWALL coll = colla+collb collection.remove(colla) collection.remove(collb) collection.append(coll) return maze_map
def kruskal_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) collection =[] wallList=[] for zi in range(0, levels): for xi in range(0, cols): for yi in range(0, rows): if maze_map[xi][yi][zi] == CELL_NO_VISIT: collection.append([(xi,yi,zi)]) continue if maze_map[xi][yi][zi] == STEEL: continue if zi%2==1: wallList.append((xi,yi,zi, UD)) continue if xi%2==0 and yi%2==1: wallList.append((xi,yi,zi, LR)) continue if xi%2==1 and yi%2==0: wallList.append((xi,yi,zi, FB)) continue print("error")
while True: for event in pygame.event.get(): if event.type == pygame.QUIT: return if wallList: nowx,nowy,nowz,d = random.choice(wallList) wallList.remove((nowx,nowy,nowz,d)) if d == UD: grida = (nowx,nowy,nowz-1) gridb = (nowx,nowy,nowz+1) elif d==LR : grida = (nowx-1,nowy,nowz) gridb = (nowx+1,nowy,nowz) elif d==FB: grida = (nowx,nowy-1,nowz) gridb = (nowx,nowy+1,nowz) colla = [] collb = [] for coll in collection: if grida in coll: colla = coll if gridb in coll: collb = coll maze_map[nowx][nowy][nowz] = WALL_VISIT if colla != collb: maze_map[nowx][nowy][nowz] = NOWALL if d == UD: grida = (nowx,nowy,nowz-1) gridb = (nowx,nowy,nowz+1) if maze_map[grida[0]][grida[1]][grida[2]] == STAIRS_D: maze_map[grida[0]][grida[1]][grida[2]] = STAIRS_UD else: maze_map[grida[0]][grida[1]][grida[2]] = STAIRS_U if maze_map[gridb[0]][gridb[1]][gridb[2]] == STAIRS_U: maze_map[gridb[0]][gridb[1]][gridb[2]] = STAIRS_UD else: maze_map[gridb[0]][gridb[1]][gridb[2]] = STAIRS_D elif d==LR : grida = (nowx-1,nowy,nowz) gridb = (nowx+1,nowy,nowz) if maze_map[grida[0]][grida[1]][grida[2]] == CELL_NO_VISIT: maze_map[grida[0]][grida[1]][grida[2]] = CELL_VISIT if maze_map[gridb[0]][gridb[1]][gridb[2]] == CELL_NO_VISIT: maze_map[gridb[0]][gridb[1]][gridb[2]] = CELL_VISIT elif d==FB: grida = (nowx,nowy-1,nowz) gridb = (nowx,nowy+1,nowz) if maze_map[grida[0]][grida[1]][grida[2]] == CELL_NO_VISIT: maze_map[grida[0]][grida[1]][grida[2]] = CELL_VISIT if maze_map[gridb[0]][gridb[1]][gridb[2]] == CELL_NO_VISIT: maze_map[gridb[0]][gridb[1]][gridb[2]] = CELL_VISIT coll = colla+collb collection.remove(colla) collection.remove(collb) collection.append(coll) draw_maze(maze_map, levels, rows, cols, (nowx,nowy,nowz), not wallList) time_passed = clock.tick(5)
pygame.display.update() return
if __name__ == "__main__": '''main''' kruskal_maze_demo(5, 11, 15)
|