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
|
import random from maze import * from draw_maze import *
def wilson_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) x=1 y=1 z=0
tmpgrids = [] tmpwalls = [] 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) notusegrids.remove((nowx,nowy,nowz)) maze_map[nowx][nowy][nowz]=CELL_VISIT nowx,nowy,nowz = notusegrids[0] tmpgrids.append((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) nextgrid=(newx,newy,newz) if move == 'l': newy = nowy newx = nowx-2 newz = nowz opwall=(nowx-1,nowy,nowz) nextgrid=(newx,newy,newz) if move == 'b': newy = nowy+2 newx = nowx newz = nowz opwall=(nowx,nowy+1,nowz) nextgrid=(newx,newy,newz) if move == 'r': newy = nowy newx = nowx+2 newz = nowz opwall=(nowx+1,nowy,nowz) nextgrid=(newx,newy,newz) if move == 'u': newy = nowy newx = nowx newz = nowz+2 opwall=(nowx,nowy,nowz+1) nextgrid=(newx,newy,newz) if move == 'd': newy = nowy newx = nowx newz = nowz-2 opwall=(nowx,nowy,nowz-1) nextgrid=(newx,newy,newz) if (newx, newy, newz) in tmpgrids: i = tmpgrids.index((newx, newy, newz)) tmpgrids=tmpgrids[:i+1] tmpwalls=tmpwalls[:i] nowx=newx nowy=newy nowz=newz elif maze_map[newx][newy][newz] != CELL_NO_VISIT: tmpwalls.append(opwall) for (x,y,z) in tmpgrids: notusegrids.remove((x,y,z)) if maze_map[x][y][z]==CELL_NO_VISIT: maze_map[x][y][z]=CELL_VISIT else: print("x") for (x,y,z) in tmpwalls: maze_map[x][y][z]=NOWALL if z%2 == 1: if maze_map[x][y][z-1] == CELL_VISIT: maze_map[x][y][z-1]=STAIRS_U elif maze_map[x][y][z-1] == STAIRS_D: maze_map[x][y][z-1]=STAIRS_UD if maze_map[x][y][z+1] == CELL_VISIT: maze_map[x][y][z+1]=STAIRS_D elif maze_map[x][y][z+1] == STAIRS_U: maze_map[x][y][z+1]=STAIRS_UD else: maze_map[x][y][z]=NOWALL tmpgrids=[] tmpwalls=[] if notusegrids: nowx,nowy,nowz = notusegrids[0] tmpgrids.append((nowx,nowy,nowz)) else: tmpgrids.append(nextgrid) tmpwalls.append(opwall) nowx=newx nowy=newy nowz=newz draw_maze(maze_map, levels, rows, cols, (nowx,nowy,nowz), not notusegrids) if tmpgrids: for (xi,yi,zi) in tmpgrids: a = zi % 2 b = zi // 2 px, py = 1 + xi * DIAMOND_SIZE[0] + a * (cols*DIAMOND_LENGTH+10), 1 + yi * DIAMOND_SIZE[1] + b*(rows*DIAMOND_LENGTH+10) screen.blit(DIAMOND_W, (px, py))
if tmpwalls: for (xi,yi,zi) in tmpwalls: a = zi % 2 b = zi // 2 px, py = 1 + xi * DIAMOND_SIZE[0] + a * (cols*DIAMOND_LENGTH+10), 1 + yi * DIAMOND_SIZE[1] + b*(rows*DIAMOND_LENGTH+10) screen.blit(DIAMOND_W, (px, py)) time_passed = clock.tick(30) pygame.display.update() return
if __name__ == "__main__": '''main''' wilson_maze_demo(5, 15, 17)
|