Package kuimaze :: Module map_generator
[hide private]
[frames] | no frames]

Source Code for Module kuimaze.map_generator

 1  ''' 
 2  Simple maze generator 
 3  ''' 
 4   
 5  import numpy 
 6  from numpy.random import random_integers as rand 
 7   
 8   
9 -def maze(width=10, height=10, complexity=.75, density=.75):
10 # Only odd shapes 11 width = width + 1 12 height = height + 1 13 shape = ((height // 2) * 2 + 1, (width // 2) * 2 + 1) 14 # Adjust complexity and density relative to maze size 15 complexity = int(complexity * (5 * (shape[0] + shape[1]))) 16 density = int(density * ((shape[0] // 2) * (shape[1] // 2))) 17 # Build actual maze 18 Z = numpy.zeros(shape, dtype=bool) 19 # Fill borders 20 Z[0, :] = Z[-1, :] = 1 21 Z[:, 0] = Z[:, -1] = 1 22 # Make aisles 23 for i in range(density): 24 x, y = rand(0, shape[1] // 2) * 2, rand(0, shape[0] // 2) * 2 25 Z[y, x] = 1 26 for j in range(complexity): 27 neighbours = [] 28 if x > 1: 29 neighbours.append((y, x - 2)) 30 if x < shape[1] - 2: 31 neighbours.append((y, x + 2)) 32 if y > 1: 33 neighbours.append((y - 2, x)) 34 if y < shape[0] - 2: 35 neighbours.append((y + 2, x)) 36 if len(neighbours): 37 y_,x_ = neighbours[rand(0, len(neighbours) - 1)] 38 if Z[y_, x_] == 0: 39 Z[y_, x_] = 1 40 Z[y_ + (y - y_) // 2, x_ + (x - x_) // 2] = 1 41 x, y = x_, y_ 42 # convert to maze.py format 43 ret = [[0 for x in range(len(Z[0]) - 2)] for y in range(len(Z) - 2)] 44 for i in range(len(Z)): 45 for j in range(len(Z[0])): 46 if i > 0 and j > 0 and i < len(Z) - 1 and j < len(Z[0]) - 1: 47 if Z[i][j] == 1: 48 ret[i - 1][j - 1] = [0, 0, 0] 49 else: 50 ret[i - 1][j - 1] = [255, 255, 255] 51 if i == 1 and j == 1: 52 ret[i - 1][j - 1] = [0, 0, 255] 53 if i == len(Z) - 2 and j == len(Z[0]) - 2: 54 ret[i - 1][j - 1] = [255, 0, 0] 55 return ret
56