Rendering a Maze as an Image

ASCII art is undeniably functional, but not necessarily attractive. Often, we want our products to have a bit more polish. Fortunately, APIs are available for most languages for either drawing directly to a view, or (at the very least) rendering to an offscreen canvas and saving it as an image file. We’ll explore the latter of those techniques with a Ruby library called ChunkyPNG,[7] which will let us write our mazes as PNG images. (The concepts should translate readily to other UI and graphics APIs.)

If you’re following along in Ruby, you’ll need to install ChunkyPNG to get the code that follows to work. Fortunately, it’s not hard:

 
$ ​gem install chunky_png

Now, then.

Just as we used the to_s method to render ...

Get Mazes for Programmers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.