Python Turtle Maze Generator: Creating Randomly Generated Mazes with Turtle Graphics
import turtle import random
turtle.ht() turtle.speed(0) row = 0 column = 0 cell_list = [0, 0, 0, 0, 0] cell_opening = 0
def down_opening1(): global cell_opening turtle.fd(10) turtle.bk(1) turtle.penup() turtle.rt(90) turtle.fd(10) turtle.pendown() turtle.rt(90) turtle.fd(10) cell_opening = 0
def down_opening2(): global cell_opening turtle.fd(10) turtle.rt(90) turtle.fd(11) cell_opening = 1
def left_opening1(): global cell_opening turtle.rt(90) turtle.fd(10) turtle.lt(90) turtle.fd(11) cell_opening = 0
def left_opening2(): global cell_opening turtle.rt(90) turtle.fd(10) turtle.bk(1) turtle.penup() turtle.lt(90) turtle.fd(10) turtle.pendown() turtle.lt(90) turtle.fd(10) cell_opening = 1
def ceiling(): global cell_opening turtle.penup() turtle.fd(10) turtle.rt(90) turtle.pendown() turtle.fd(10) cell_opening = 1
def cell(): global cell_list, row, cell_opening cell_random = random.randint(0, 1) turtle.setheading(0) if column == 0: if cell_opening == 0: if cell_random == 0: down_opening1() cell_list[row] = 1 elif cell_random == 1: down_opening2() elif cell_opening == 1: if cell_random == 0: left_opening1() cell_list[row] = 1 elif cell_random == 1: left_opening2() else: if cell_list[row] == 0: if cell_opening == 0: if cell_random == 0: ceiling() elif cell_random == 1: cell_list[row] = 1 elif cell_opening == 1: if cell_random == 0: left_opening1() cell_list[row] = 1 elif cell_random == 1: left_opening2() elif cell_list[row] == 1: if cell_opening == 0: if cell_random == 0: down_opening1() cell_list[row] = 1 elif cell_random == 1: ceiling() if cell_opening == 1: if cell_random == 0: down_opening2() elif cell_random == 1: cell_list[row] = 1
for i in range(5): for j in range(5): turtle.penup() turtle.goto(10 * column, 10 * row) turtle.pendown() column += 1 cell() column = 0 row += 1 cell_list = [0, 0, 0, 0, 0] cell_opening = 0
turtle.mainloop()
原文地址: https://www.cveoy.top/t/topic/qFiQ 著作权归作者所有。请勿转载和采集!