To create a random maze using the turtle module in Python, you can follow these steps:

  1. Import the turtle module:
import turtle
  1. Set up the turtle screen and its properties:
screen = turtle.Screen()
screen.setup(width=600, height=600)
screen.title("Random Maze")
screen.bgcolor("white")
  1. Create a turtle object to draw the maze:
maze_turtle = turtle.Turtle()
maze_turtle.speed(0)
maze_turtle.color("black")
maze_turtle.penup()
maze_turtle.hideturtle()
  1. Create a grid to represent the maze using a 2D list. Each cell can be either open or closed. Initialize all cells as closed (0):
grid = [[0] * 15 for _ in range(15)]
  1. Define a function to draw the maze:
def draw_maze():
    for i in range(-270, 270, 40):
        for j in range(-270, 270, 40):
            maze_turtle.goto(j, i)
            maze_turtle.pendown()
            if grid[(i + 270) // 40][(j + 270) // 40] == 1:
                maze_turtle.color("white")
            else:
                maze_turtle.color("black")
            maze_turtle.setheading(0)
            for _ in range(4):
                maze_turtle.forward(20)
                maze_turtle.left(90)
            maze_turtle.penup()
  1. Define a function to generate a random maze using a depth-first search algorithm:
import random

def generate_maze(row, col):
    grid[row][col] = 1
    directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
    random.shuffle(directions)
    for dx, dy in directions:
        new_row = row + dx * 2
        new_col = col + dy * 2
        if 0 <= new_row < 15 and 0 <= new_col < 15 and grid[new_row][new_col] == 0:
            grid[new_row][new_col] = 1
            grid[row + dx][col + dy] = 1
            generate_maze(new_row, new_col)
  1. Call the generate_maze function to create the maze:
generate_maze(0, 0)
  1. Call the draw_maze function to draw the generated maze:
draw_maze()
  1. Add a turtle.done() statement to keep the turtle window open:
turtle.done()

The complete code would look like this:

import turtle
import random

screen = turtle.Screen()
screen.setup(width=600, height=600)
screen.title("Random Maze")
screen.bgcolor("white")

maze_turtle = turtle.Turtle()
maze_turtle.speed(0)
maze_turtle.color("black")
maze_turtle.penup()
maze_turtle.hideturtle()

grid = [[0] * 15 for _ in range(15)]

def draw_maze():
    for i in range(-270, 270, 40):
        for j in range(-270, 270, 40):
            maze_turtle.goto(j, i)
            maze_turtle.pendown()
            if grid[(i + 270) // 40][(j + 270) // 40] == 1:
                maze_turtle.color("white")
            else:
                maze_turtle.color("black")
            maze_turtle.setheading(0)
            for _ in range(4):
                maze_turtle.forward(20)
                maze_turtle.left(90)
            maze_turtle.penup()

def generate_maze(row, col):
    grid[row][col] = 1
    directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
    random.shuffle(directions)
    for dx, dy in directions:
        new_row = row + dx * 2
        new_col = col + dy * 2
        if 0 <= new_row < 15 and 0 <= new_col < 15 and grid[new_row][new_col] == 0:
            grid[new_row][new_col] = 1
            grid[row + dx][col + dy] = 1
            generate_maze(new_row, new_col)

generate_maze(0, 0)
draw_maze()

turtle.done()

When you run this code, it will create a window with a random maze drawn by the turtle graphics.

How to make a random maze by turtle in python

原文地址: https://www.cveoy.top/t/topic/i4Ud 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录