def maze(*arms): m = mazer.Maze(3,3) for direction, arm in zip([N,E,S,W], arms): if arm: m.move(1, 1, direction) m.carve(AHEAD) arm_d = dict(zip([LEFT, RIGHT], arm)) for d in m.branch([LEFT, RIGHT]): for i in range(arm_d[d]): m.carve(AHEAD) or m.carve(d) return m
if not options.output: parser.error("No output file specified") if options.rows <= 0: parser.error("The maze must have at least one row") if options.columns <= 0: parser.error("The maze must have at least one column") width = options.width if not width: width = options.column_width * options.columns + 2 * options.margin if not width: parser.error("Neither --width nor --column-width is specified") height = options.height if not height: height = options.row_height * options.rows + 2 * options.margin if not height: parser.error("Neither --height nor --row-height is specified") m = mazer.Maze(options.columns, options.rows) mazer.generate.wilson(m) m.render_to_png(options.output, width, height, margin=options.margin, background_colour=(0.7, 0.7, 0.7), fill_colour=(1, 1, 1), path_border_percent=100 / 3, draw_entrance_and_exit=False)
import cairo, mazer surface = cairo.PDFSurface("/tmp/weave-4.pdf", 3 * 72, 3 * 72) c = cairo.Context(surface) renderer = mazer.render.CairoRenderer(c, width=3 * 72, height=3 * 72) m = mazer.Maze(4, 4) for x in (1, 2): for y in (1, 2): m.weave(x, y, [mazer.V_OVER_H, mazer.H_OVER_V][(x - 1) * (y - 1)]) m.carve(mazer.AHEAD) m.move(0, 2, mazer.S) m.carve(mazer.AHEAD) m.carve(mazer.LEFT) m.move(2, 0, mazer.E) m.carve(mazer.AHEAD) m.carve(mazer.RIGHT) m.move(3, 2, mazer.S) m.carve(mazer.AHEAD) m.carve(mazer.RIGHT) print m.render_as_unicode() renderer.render(m) c.show_page() surface.finish()
import cairo, mazer surface = cairo.PDFSurface("/tmp/weave.pdf", 3 * 72, 3 * 72) c = cairo.Context(surface) renderer = mazer.render.CairoRenderer(c, width=3 * 72, height=3 * 72) m = mazer.Maze(3, 3) m.weave(1, 1, mazer.V_OVER_H) #m.weave(1,1,mazer.H_OVER_V) m.move(0, 1) m.carve(mazer.N) m.carve(mazer.E) m.move(2, 0) m.carve(mazer.S) m.move(2, 2) m.carve(mazer.W) m.move(0, 2) m.carve(mazer.N) print m.render_as_unicode() renderer.render(m) c.show_page() surface.finish()
# Some little bits of test code for copy-pasting into ipython import mazer m = mazer.Maze(10, 10) m.weave(3, 3, mazer.H_OVER_V) m.weave(4, 4, mazer.V_OVER_H) m.weave(5, 5, mazer.H_OVER_V) print m.render_as_unicode() import mazer, sys sys.path.append("scripts") import make_maze m = mazer.Maze(10,10) m.weave(4, 4, mazer.V_OVER_H) make_maze.kruskal(m) print m.render_as_unicode() import mazer, sys sys.path.append("scripts") import make_maze m = mazer.Maze(10,10) make_maze.aldous_broder(m) print m.render_as_unicode() import mazer, sys
type="str", help="name of output file") (options, args) = parser.parse_args() if args: parser.error("Unexpected non-option argument: " + args[0]) if not options.output: parser.error("No output file specified") if options.rows <= 0: parser.error("The maze must have at least one row") if options.columns <= 0: parser.error("The maze must have at least one column") w, h = options.columns, options.rows maze = mazer.Maze(w, h) mazer.generate.wilson(maze) im = cairo.ImageSurface(cairo.FORMAT_A1, 2 * w + 1, 2 * h + 1) stride = im.get_stride() data = im.get_data() def whiten(x, y): byte_index = y * stride + x // 8 data[byte_index] = chr(ord(data[byte_index]) | 0x1 << (x % 8)) for y in range(h): for x in range(w): whiten(2 * x + 1, 2 * y + 1)