Send
Close Add comments:
(status displays here)
Got it! This site "robinsnyder.com" uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website. Note: This appears on each machine/browser from which this site is accessed.
Python: Maze generation using PIL
1. Python: Maze generation using PIL
2. Recursive maze generation and visualization
3. Colors
The ImageDraw class uses colors are represented as a tuple of the integer values for red, green, and blue. I find it easier to work with the hex forms of red, green and blue.
The color method converts from hex to a tuple representation.
4. Color examples
color of "000000" returns (0,0,0), black.
color of "FF0000" returns (255,0,0), red.
color of "00FF00" returns (0,255,0), green.
color of "0000FF" returns (0,0,255), blue.
color of "FFFFFF" returns (255,255,255), white.
A Lua command line console program is now presented to create a maze as an image.
This may not be the best Lua program for this task, but is sufficient for the present purposes.
The global variable linesCount1 is used, in part to show how global variables can be used, when appropriate, as some languages require special handling or access mechanisms for global variables.
5. Algorithm pesudo-code
Here is a pseudo-code for the maze generation algorithm as described to make a maze of rows and columns.
Main:
Start with an empty canvas.
Outline the maze limits with an entry and exit point on opposite sides.
Call MazeGenerate with the rows and columns to be filled in.
MazeGenerate:
If the rows and columns requested are both greater than 1 Then
If the height is greater than the width Then
Divide the grid into top and bottom parts.
Draw a (horizontal) line between the parts, leaving a gap to pass through.
Call MazeGenerate on the top part
Call MazeGenerate on the bottom part
Else
Divide the grid into left and right parts.
Draw a (vertical) line between the parts, leaving a gap to pass through.
Call MazeGenerate on the left part
Call MazeGenerate on the right part
End If
End If
The actual code fills in details that are not needed at the higher level of the algorithm description. For example, the side and gap lengths, and exact x and y coordinates are omitted. As another example, the division into parts can be done using pseudo-random numbers, but that is omitted from the algorithm.
For more information, see the following:
Maze algorithm explanation using GIF images
Here is the Python code [#2]
Here is the output of the Python code.
Here is the image created by the Python code.