Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Basic Maze generator
- # @eigenbom
- from random import randint, sample
- from operator import xor
- TITLE = 'MAZE'
- CELL_SIZE = 16
- ROOMS_WIDE = 18
- ROOMS_HIGH = 18
- CELLS_WIDE = 2 * ROOMS_WIDE + 1
- CELLS_HIGH = 2 * ROOMS_HIGH + 1
- WIDTH = CELL_SIZE * CELLS_WIDE
- HEIGHT = CELL_SIZE * CELLS_HIGH
- ROOM, WALL, CORRIDOR = 0, 1, 2
- valid_rooms = set()
- cells = None
- generator = None
- timer = 0
- def setup_rooms():
- """Create a donut-shaped world for rooms to live"""
- global valid_rooms
- for i in range(ROOMS_WIDE):
- for j in range(ROOMS_HIGH):
- if (i < ROOMS_WIDE/3 or i >= 2*ROOMS_WIDE/3) or (j < ROOMS_HIGH/3 or j >= 2*ROOMS_HIGH/3):
- valid_rooms.add((i,j))
- def adjacent_walls(room):
- walls = set()
- for dir in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
- if (room[0]+dir[0], room[1]+dir[1]) in valid_rooms:
- # if 0 <= room[0]+dir[0] < ROOMS_WIDE and 0 <= room[1]+dir[1] < ROOMS_HIGH:
- walls.add((dir[0], dir[1], room))
- return walls
- def create_maze():
- global cells
- open_walls = set()
- start = sample(valid_rooms, 1)[0]
- path = set({start})
- walls = adjacent_walls(start)
- while len(walls)>0:
- wall = sample(walls, 1)[0]
- dx, dy, room = wall
- r1 = room[0], room[1]
- r2 = room[0] + dx, room[1] + dy
- suitable = xor(r1 in path, r2 in path)
- if suitable:
- r = r2 if r1 in path else r1
- if r is r1:
- open_walls.add((dx, dy, r))
- else:
- open_walls.add((-dx, -dy, r))
- path.add(r)
- walls.update(adjacent_walls(r))
- walls.remove(wall)
- # update cells
- for rx, ry in path:
- c, v = cells[1 + 2*ry][1 + 2*rx]
- if c != ROOM:
- v = 0
- else:
- v = v + 1
- cells[1 + 2*ry][1 + 2*rx] = (ROOM, v)
- for ow in open_walls:
- dx, dy, r = ow
- cells[1 + 2*r[1] + dy][1 + 2*r[0] + dx] = (CORRIDOR, 0)
- yield
- def generate():
- global cells, generator
- cells = [[(WALL, 0) for i in range(CELLS_WIDE)] for j in range(CELLS_HIGH)]
- setup_rooms()
- generator = create_maze()
- def update(dt):
- global timer, generator
- timer += dt
- if timer > 0.001 and generator:
- timer = 0
- try:
- next(generator)
- except StopIteration:
- generator = None
- for i in range(CELLS_WIDE):
- for j in range(CELLS_HIGH):
- cells[j][i] = cells[j][i][0], 0
- def draw():
- screen.fill((42, 52, 67))
- if cells is None:
- return
- # corridors
- for i in range(CELLS_WIDE):
- for j in range(CELLS_HIGH):
- cell, value = cells[j][i]
- if cell == CORRIDOR:
- sz = int(0.75 * CELL_SIZE)
- rect = Rect((i*CELL_SIZE + (CELL_SIZE - sz)/2, j*CELL_SIZE + (CELL_SIZE - sz)/2), (sz, sz))
- colour = 131, 117, 135
- screen.draw.filled_rect(rect, colour)
- # rooms
- for i in range(CELLS_WIDE):
- for j in range(CELLS_HIGH):
- cell, value = cells[j][i]
- if cell == ROOM:
- colour = 131, 117, 135
- screen.draw.filled_circle(((i+0.5)*CELL_SIZE, (j+0.5)*CELL_SIZE), int(0.8*CELL_SIZE), colour)
- if 0 < value < 40:
- colour = 132, 219, 252
- screen.draw.filled_circle(((i+0.5)*CELL_SIZE, (j+0.5)*CELL_SIZE), int(0.5*CELL_SIZE), colour)
- def on_key_down():
- generate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement