kopia lustrzana https://github.com/villares/sketch-a-day
52 wiersze
1.9 KiB
Python
52 wiersze
1.9 KiB
Python
# based on Randomized Prim's algorithm https://en.wikipedia.org/wiki/Maze_generation_algorithm
|
|
|
|
from random import randint
|
|
|
|
def setup():
|
|
global m, shape, Z, C
|
|
size(800, 800)
|
|
rectMode(CENTER)
|
|
colorMode(HSB)
|
|
noLoop()
|
|
noStroke()
|
|
# Only odd shapes
|
|
shape = ((101 // 2) * 2 + 1, (101 // 2) * 2 + 1)
|
|
Z = [[0] * shape[1] for _ in range(shape[0])]
|
|
C = [[0] * shape[1] for _ in range(shape[0])]
|
|
m = maze(101,101)
|
|
w, h = 8, 8
|
|
for x in range(len(m)):
|
|
for y in range(len(m[0])):
|
|
if m[x][y]==1: fill(C[x][y])
|
|
else: fill(255)
|
|
rect(w/2 + w * x, h/2 + h * y, w, h)
|
|
saveFrame("s174.png")
|
|
|
|
def maze(mh, mw, complexity=.51, density=2):
|
|
# Adjust complexity and density relative to maze size
|
|
complexity = int(complexity * (5 * (shape[0] + shape[1]))) # number of components
|
|
density = int(density * ((shape[0] // 2) * (shape[1] // 2))) # size of components
|
|
# Build actual maze
|
|
|
|
# Make aisles
|
|
for i in range(density):
|
|
x = randint(0, shape[1] // 2) * 2
|
|
y = randint(0, shape[0] // 2) * 2 # pick a random position
|
|
Z[y][x] = 1
|
|
for j in range(complexity):
|
|
neighbours = []
|
|
if x > 1: neighbours.append((y, x - 2))
|
|
if x < shape[1] - 2: neighbours.append((y, x + 2))
|
|
if y > 1: neighbours.append((y - 2, x))
|
|
if y < shape[0] - 2: neighbours.append((y + 2, x))
|
|
if len(neighbours):
|
|
y_,x_ = neighbours[randint(0, len(neighbours) - 1)]
|
|
if Z[y_][x_] == 0:
|
|
Z[y_][x_] = 1
|
|
C[y_][x_] = color(i+j % 256, 255, 200)
|
|
Z[y_ + (y - y_) // 2][ x_ + (x - x_) // 2] = 1
|
|
C[y_ + (y - y_) // 2][ x_ + (x - x_) // 2] = color(i+j % 256, 255, 200)
|
|
x, y = x_, y_
|
|
redraw()
|
|
return Z
|