This project is a maze generator written in Python using the Pygame library for graphics and PIL (Pillow) for image manipulation. It can generate mazes of various sizes and save them as PNG images, as well as JSON files to store the maze structure.
- Maze Generation: Uses a recursive backtracking algorithm to generate mazes.
- Save as PNG: Mazes can be saved as PNG images.
- Tile Generation: Supports tiled maze generation to generate very large mazes.
- Maze Solver: Can optionally solve the maze and display the solution path.
- Save as JSON: The maze structure can be saved to a JSON file.
- Configuration via command line arguments: Easily configurable parameters such as maze size, cell size, etc.
- Python 3.6+
- Pygame
- Pillow (PIL)
You can install dependencies with the command:
pip install pygame pillowTo run the script, use the following command:
python maze.py [options]-W,--width: The width of the maze (number of columns). Default: 20.-H,--height: The height of the maze (number of rows). Default: 20.-c,--cell-size: The size of each cell in pixels. Default: 10.-t,--tile-size: Tile size in cells (used if tiling is enabled). Default: 200.-n,--filename-base: Base filename for output files (image and JSON). Default: "large_maze".-s,--solve: Solve the maze and display the solution path.--no-tiling: Disable tiling and generate a single image.
- Create a 50x50 maze with a cell size of 15, save to file "my_maze.png", without solving:
python maze.py -W 50 -H 50 -c 15 -n my_maze- Create a 100x100 maze with a cell size of 10, split into 100x100 tiles, save to files "large_maze_combined.png" and "large_maze_solved_combined.png", with solving:
python maze.py -W 100 -H 100 -c 10 -t 100 -n large_maze -s- Create a 30x30 maze with a cell size of 20, without tiles, without solutions:
python maze.py -W 30 -H 30 -c 20 -n small_maze --no-tilingmaze.py: The main script that contains theMazeandCellclasses, as well as functions for generating and saving mazes.
Cellclass: Represents a single cell in the maze, stores information about walls and visited status.Mazeclass:
- Initializes the maze by creating a grid of cells.
- Generates the maze using a recursive backtracking algorithm.
- Solves the maze using a depth-first search (DFS) algorithm.
- Draws the maze on a Pygame Surface, with the ability to tile and display the solution.
- Saves the maze as JSON.
- Function
generate_maze_image_and_json_tiled_combined:
- Creates a Pygame Surface for drawing the maze.
- Draws the maze, cell by cell.
- Saves the image as PNG.
- Saves the maze structure as JSON.
- Function
main:
- Processes command line arguments.
- Creates a
Mazeobject. - Calls functions to generate, solve, and save the maze.