| 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
 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
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 
 | 
 import random
 import pygame
 import maze
 
 pygame.init()
 size = width, height = 800, 600
 screen = pygame.display.set_mode(size)
 
 diamond_color_size = 12
 COLOR_RED, COLOR_BLUE, COLOR_GREEN, COLOR_YELLOW, COLOR_BLACK, COLOR_FLAXEN, COLOR_GOLD, COLOR_GRAY, COLOR_PINK, COLOR_ORANGE, COLOR_WHEAT, COLOR_CYAN = list(range(
 diamond_color_size))
 COLOR = {
 COLOR_RED: (255, 0, 0),
 COLOR_BLUE: (0, 0, 255),
 COLOR_GREEN: (0, 255, 0),
 COLOR_YELLOW: (255, 255, 0),
 COLOR_BLACK: (0, 0, 0),
 COLOR_FLAXEN: (250, 240, 230),
 COLOR_GOLD : (255,215,0),
 COLOR_GRAY: (128,128,128),
 COLOR_PINK:(255,192,203),
 COLOR_ORANGE: (255,165,0),
 COLOR_WHEAT: (245,222,179),
 COLOR_CYAN : (0,255,255),
 }
 
 DIAMOND_LEN = 20
 DIAMOND_SIZE = (DIAMOND_LEN, DIAMOND_LEN)
 
 DIAMONDS=[]
 for x in range(diamond_color_size):
 diamoand=pygame.surface.Surface(DIAMOND_SIZE).convert()
 diamoand.fill(COLOR[x])
 DIAMONDS.append(diamoand)
 
 DIAMONDS=[]
 for x in range(diamond_color_size):
 diamoand=pygame.surface.Surface(DIAMOND_SIZE).convert()
 diamoand.fill(COLOR[x])
 DIAMONDS.append(diamoand)
 
 
 
 use_font = pygame.font.Font("FONT.TTF", 16)
 use_font12 = pygame.font.Font("FONT.TTF", 12)
 
 background=pygame.surface.Surface(size).convert()
 background.fill(COLOR[COLOR_BLACK])
 
 score_surface = use_font.render("找到终点", True, COLOR[COLOR_BLACK], COLOR[COLOR_FLAXEN])
 
 clock = pygame.time.Clock()
 
 NOWALL=maze.NOWALL
 WALL=maze.WALL
 WALL2=maze.WALL2
 VISIT=maze.VISIT
 NOVISIT=maze.NOVISIT
 VERTICAL = maze.VERTICAL
 HORIZONTAL = maze.HORIZONTAL
 INFINITE = maze.INFINITE
 
 def DrawCircle(screen,  position, color, pure=False, radius=6, width=6):
 if pure:
 pygame.draw.circle(screen, color, position, radius, width)
 else:
 pygame.draw.circle(screen, color, position, radius-3, 3)
 pygame.draw.circle(screen, COLOR[COLOR_RED], position, radius-2, 1)
 pygame.draw.circle(screen, COLOR[COLOR_GREEN], position, radius-1, 1)
 pygame.draw.circle(screen, COLOR[COLOR_BLUE], position, radius, 1)
 
 
 
 
 
 def FindNextCircle(startList, walls, grids, rows, cols):
 startNextList = []
 for node in startList:
 r, c = node
 l = grids[r][c]
 
 if r>0 and NOWALL == walls[r][c][1] and INFINITE == grids[r-1][c]:
 
 nr=r-1
 nc=c
 if (nr,nc) not in startNextList:
 startNextList.append((nr,nc))
 grids[nr][nc] = l+1
 if c>0 and NOWALL == walls[r][c][0] and INFINITE == grids[r][c-1]:
 
 nr=r
 nc=c-1
 if (nr,nc) not in startNextList:
 startNextList.append((nr,nc))
 grids[nr][nc] = l+1
 if c<cols-1 and NOWALL == walls[r][c+1][0] and INFINITE == grids[r][c+1] :
 
 nr=r
 nc=c+1
 if (nr,nc) not in startNextList:
 startNextList.append((nr,nc))
 grids[nr][nc] = l+1
 if r<rows-1 and NOWALL == walls[r+1][c][1] and INFINITE == grids[r+1][c] :
 
 nr=r+1
 nc=c
 if (nr,nc) not in startNextList:
 startNextList.append((nr,nc))
 grids[nr][nc] = l+1
 
 startList.clear()
 startList.extend(startNextList)
 return startList
 
 def sample_findmainpath_step(r, c, mainList, walls, grids, rows, cols):
 
 findMainPath = False
 mainList.append((r,c))
 l = grids[r][c]
 nl=l-1
 
 if r>0 and NOWALL == walls[r][c][1] and nl == grids[r-1][c]:
 
 nr=r-1
 nc=c
 elif c>0 and NOWALL == walls[r][c][0] and nl == grids[r][c-1]:
 
 nr=r
 nc=c-1
 elif c<cols-1 and NOWALL == walls[r][c+1][0] and nl == grids[r][c+1] :
 
 nr=r
 nc=c+1
 elif r<rows-1 and NOWALL == walls[r+1][c][1] and nl == grids[r+1][c] :
 
 nr=r+1
 nc=c
 
 if 0 == nl:
 mainList.append((nr,nc))
 findMainPath = True
 r,c=nr,nc
 return r,c, findMainPath
 
 def sample_findpathing_step(startList, walls, grids, rows, cols, treasures):
 
 
 findPath = False
 startNextList = []
 for node in startList:
 r, c = node
 l = grids[r][c]
 ln=l+1
 if node in treasures:
 findPath = True
 startList.clear()
 break
 
 if r>0 and NOWALL == walls[r][c][1] and INFINITE == grids[r-1][c]:
 
 nr=r-1
 nc=c
 if (nr,nc) not in startNextList:
 startNextList.append((nr,nc))
 grids[nr][nc] = ln
 if c>0 and NOWALL == walls[r][c][0] and INFINITE == grids[r][c-1]:
 
 nr=r
 nc=c-1
 if (nr,nc) not in startNextList:
 startNextList.append((nr,nc))
 grids[nr][nc] = ln
 if c<cols-1 and NOWALL == walls[r][c+1][0] and INFINITE == grids[r][c+1] :
 
 nr=r
 nc=c+1
 if (nr,nc) not in startNextList:
 startNextList.append((nr,nc))
 grids[nr][nc] = ln
 if r<rows-1 and NOWALL == walls[r+1][c][1] and INFINITE == grids[r+1][c] :
 
 nr=r+1
 nc=c
 if (nr,nc) not in startNextList:
 startNextList.append((nr,nc))
 grids[nr][nc] = ln
 
 startList.clear()
 startList.extend(startNextList)
 return findPath, r, c
 
 
 def draw_diamond(r, c, screen, diamod):
 px,py= 1 + (c) * DIAMOND_SIZE[0], 1 + (r) * DIAMOND_SIZE[1]
 screen.blit(diamod, (px, py))
 return
 
 
 
 def draw_diamond_and_str(r, c, screen, diamod, use_font, string, color, color_back):
 px,py= 1 + (c) * DIAMOND_SIZE[0], 1 + (r) * DIAMOND_SIZE[1]
 screen.blit(diamod, (px, py))
 distance_surface = use_font.render(string, True, color, color_back)
 screen.blit(distance_surface, (px, py))
 return
 
 
 
 def multipath_maze_demo(rows, cols):
 maze_h = rows * DIAMOND_SIZE[0] + 1
 maze_w = cols * DIAMOND_SIZE[0] + 1
 size = (maze_w, maze_h)
 
 maze_surface=pygame.surface.Surface(size).convert()
 
 
 
 
 
 walls = maze.wilson_maze(rows, cols)
 
 posion_xy=(40, 40)
 
 grids=[[ INFINITE for i in range(cols)]for j in range(rows)]
 
 
 r=0
 c=0
 startPoint=(r,c)
 endPoint=(rows-1,cols-1)
 
 maze.down_wall_maze(walls, rows, cols, startPoint, endPoint)
 maze.down_wall_maze(walls, rows, cols, startPoint, endPoint)
 
 n=3
 treasures=[]
 tmpTreasures=[]
 while n > 0:
 r = random.randint(0, rows-1)
 c = random.randint(0, cols-1)
 if (r,c) not in treasures and (r,c) != startPoint and (r,c) != endPoint:
 treasures.append((r,c))
 tmpTreasures.append((r,c))
 n -=1
 
 mainList=[]
 
 findEndPoint=False
 findPath=False
 findTreasures=None
 findMainPath=None
 
 startList=[startPoint]
 grids[startPoint[0]][startPoint[1]]=0
 startMap=[]
 startMap += startList
 
 parts = []
 while True:
 for event in pygame.event.get():
 if event.type == pygame.QUIT:
 return
 if not findPath:
 
 if tmpTreasures:
 findPath, r, c = sample_findpathing_step(startList, walls, grids, rows, cols, tmpTreasures)
 
 treasurePoint=(r,c)
 else:
 findPath, r, c = sample_findpathing_step(startList, walls, grids, rows, cols, [endPoint])
 elif not findMainPath:
 
 r, c, findMainPath = sample_findmainpath_step(r, c, mainList, walls, grids, rows, cols)
 
 if findMainPath:
 
 if not tmpTreasures:
 findEndPoint = True
 else:
 if not findEndPoint:
 grids=[[ INFINITE for i in range(cols)]for j in range(rows)]
 startMap=[]
 if tmpTreasures:
 tmpTreasures.remove(treasurePoint)
 findPath = False
 findMainPath = False
 startList=[treasurePoint]
 grids[treasurePoint[0]][treasurePoint[1]]=0
 startMap += startList
 
 screen.blit(background, (0, 0))
 
 
 for cx in range(cols):
 for ry in range(rows):
 
 if maze.INFINITE == grids[ry][cx]:
 draw_diamond(ry, cx, maze_surface, DIAMONDS[COLOR_GRAY])
 else:
 s = "{}".format(grids[ry][cx])
 draw_diamond_and_str(ry, cx, maze_surface, DIAMONDS[COLOR_WHEAT], use_font12, s, COLOR[COLOR_BLACK], COLOR[COLOR_WHEAT])
 
 for pos in startMap:
 s = "{}".format(grids[pos[0]][pos[1]])
 draw_diamond_and_str(pos[0], pos[1], maze_surface, DIAMONDS[COLOR_WHEAT], use_font12, s, COLOR[COLOR_BLACK], COLOR[COLOR_WHEAT])
 
 if startList and not mainList:
 for pos in startList:
 s = "{}".format(grids[pos[0]][pos[1]])
 draw_diamond_and_str(pos[0], pos[1], maze_surface, DIAMONDS[COLOR_RED], use_font12, s, COLOR[COLOR_BLACK], COLOR[COLOR_RED])
 
 if mainList:
 for pos in mainList:
 s = "{}".format(grids[pos[0]][pos[1]])
 draw_diamond_and_str(pos[0], pos[1], maze_surface, DIAMONDS[COLOR_YELLOW], use_font12, s, COLOR[COLOR_BLACK], COLOR[COLOR_YELLOW])
 
 s = "{}".format(grids[pos[0]][pos[1]])
 draw_diamond_and_str(r, c, maze_surface, DIAMONDS[COLOR_GREEN], use_font12, s, COLOR[COLOR_BLACK], COLOR[COLOR_GREEN])
 
 pygame.draw.rect(maze_surface, COLOR[COLOR_RED], (0, 0, DIAMOND_LEN*cols+1, DIAMOND_LEN*rows+1), 2)
 
 DrawWalls(maze_surface, DIAMOND_SIZE, walls, rows, cols)
 
 if parts:
 DrawWallList(maze_surface, COLOR[COLOR_RED], DIAMOND_SIZE, parts, rows, cols)
 
 if startPoint:
 pos = (startPoint[1]*DIAMOND_LEN + 10, startPoint[0]*DIAMOND_LEN + 10)
 DrawCircle(maze_surface, pos, COLOR[COLOR_RED])
 if endPoint:
 pos = (endPoint[1]*DIAMOND_LEN + 10, endPoint[0]*DIAMOND_LEN + 10)
 DrawCircle(maze_surface, pos, COLOR[COLOR_RED])
 
 if treasures:
 for p in treasures:
 pos = (p[1]*DIAMOND_LEN + 10, p[0]*DIAMOND_LEN + 10)
 DrawCircle(maze_surface, pos, COLOR[COLOR_GOLD])
 
 
 screen.blit(maze_surface, posion_xy)
 
 
 if findEndPoint:
 screen.blit(score_surface, (100, 20+rows*22))
 
 clock.tick(25)
 
 pygame.display.update()
 return
 
 
 def DrawWalls(screen, DIAMOND_SIZE, walls, rows, cols):
 for cx in range( cols):
 for ry in range(rows):
 px,py = 1 + (cx) * DIAMOND_SIZE[0], 1 + (ry) * DIAMOND_SIZE[1]
 color = COLOR[COLOR_BLACK]
 if maze.WALL == walls[ry][cx][0]:
 pygame.draw.line(screen, color, (px, py), (px, py+DIAMOND_LEN), 2)
 if maze.WALL == walls[ry][cx][1]:
 pygame.draw.line(screen, color, (px, py), (px+DIAMOND_LEN, py), 2)
 return
 
 
 def DrawWallList(screen, color, DIAMOND_SIZE, wlist, rows, cols):
 for r,c,d in wlist:
 px,py = 1 + (c) * DIAMOND_SIZE[0], 1 + (r) * DIAMOND_SIZE[1]
 if d == 0:
 pygame.draw.line(screen, color, (px, py), (px, py+DIAMOND_LEN), 2)
 if d == 1:
 pygame.draw.line(screen, color, (px, py), (px+DIAMOND_LEN, py), 2)
 return
 
 
 
 if __name__ == "__main__":
 '''main'''
 multipath_maze_demo(20, 30)
 
 
 |