0
# Imported Modules for Randomization and Turtle
import turtle as trtl
import random as rand
from random import *
# Painter Configuration and Screen Configuration
painter = trtl.Turtle()
distanceForward = 20
painter.pensize(5)
painter.speed(10000000)
painter.screen.setup(1000, 1000)


walls = 32


#This for loop is essential--> it creates 32 lines, and based on the conditions below, adds walls, barriers, and exits
for i in range(walls):
    #Register the beginning position of the Turtle.
    xBeginCor = painter.xcor()
    yBeginCor = painter.ycor()
    #Variables for randomization of Exits and Doors
    initialWallDistance = randint(1, distanceForward-5)
    doorDistance = randint(1,distanceForward-18)
    #Program for the Walls and their Randomization
    #Prevents the last 4 lines having barriers protruding
    #We feel this method of randomizing the wall distance was really innovative and that it works pretty well
    if i < walls - 4:
        painter.penup()
        painter.forward(initialWallDistance)
        painter.left(90)
        painter.pendown()
        painter.forward(30)
        painter.backward(30)
        painter.right(90)
        painter.penup()
        #Preventing overlapping for the Walls and Doors. This does not work perfectly, and sometimes the doors are too small as a result of this
        if doorDistance == range(0, 21):
          doorDistance + 20
          painter.forward(distanceForward - initialWallDistance)
        else:
          painter.forward(distanceForward - initialWallDistance)
    #Creates the randomization of the doors. This works really well, as it makes the turtle go a random distance forward from the beginning of the line, and create a door
    painter.goto(xBeginCor, yBeginCor)
    painter.pendown()
    painter.forward(doorDistance)
    painter.penup()
    painter.forward(20)
    painter.pendown()
    painter.forward(distanceForward-doorDistance)
    #Turn the turtle to create the next line 
    painter.right(90)
    #Change the length of the line so the next one is a longer distance
    distanceForward += 15
#Keeps window open
wn = trtl.Screen()
wn.mainloop()

I need help stopping these doors and barriers happening on top of each other. I have also attached an image describing what I mean.

This code here is what I am working on to prevent the two from going on top of each other:

        if doorDistance == range(0, 21):
          doorDistance + 20
          painter.forward(distanceForward - initialWallDistance)
        else:
          painter.forward(distanceForward - initialWallDistance)

Nothing is working. At this point I am completely confused, and I have no idea what I am doing. Any explanations/help would be appreciated

Note: I am a beginner, so I will not be able to use any complex techniques

Image showing barrier on top of door.

Image of the actual Maze itself

DocZerø
  • 8,037
  • 11
  • 38
  • 66
Vurhd
  • 1
  • What is the meaning behind this code: `doorDistance == range(0, 21)`? – DocZerø Mar 20 '22 at 21:27
  • I don't even know, I was tinkering around with the code to see if I could prevent it from overlapping. But what I tried was that if doorDistance is between 0 and 20, the doorDistance would increase by 5, resulting in no overlaps. – Vurhd Mar 20 '22 at 21:30
  • That should be `if 0 <= doorDistance <= 20:`. You cannot compare to a `range`, as it is an object which generates values lazily. – DocZerø Mar 20 '22 at 21:33
  • Ok I've done that. What should I do now to prevent the barriers and doors overlapping? – Vurhd Mar 20 '22 at 21:40

1 Answers1

0

This code doesn't work Python-wise:

if doorDistance == range(0, 21):
    doorDistance + 20

It should be something like:

if 0 <= doorDistance <= 20:
    doorDistance += 20

But that's not going to fix your problem. Your logic to avoid door and barrier overlap doesn't work. As you spiral from the center, you put up barriers pointing to the next layer, and leave door gaps. But the overlaps happen on the next spiral when all memory of where those barriers are located is lost.

My rework of your code into Python (still broken):

# Imported Modules for Randomization and Turtle
from turtle import Screen, Turtle
from random import randint

WALLS = 32

# Painter Configuration and Screen Configuration
screen = Screen()
screen.setup(1000, 1000)

painter = Turtle()
painter.hideturtle()
painter.pensize(5)
painter.speed('fastest')

distanceForward = 20

# This for loop is essential--> it creates 32 lines, and based on the conditions below, adds walls, barriers, and exits
for wall in range(WALLS):
    # Register the beginning position of the Turtle.
    beginCor = painter.position()

    # Variables for randomization of Exits and Doors
    initialWallDistance = randint(1, distanceForward-5)
    doorDistance = randint(1, distanceForward-18)

    # Program for the Walls and their Randomization
    # Prevents the last 4 lines having barriers protruding
    # We feel this method of randomizing the wall distance was really innovative and that it works pretty well

    if wall < WALLS - 4:
        painter.penup()
        painter.forward(initialWallDistance)
        painter.left(90)
        painter.pendown()
        painter.forward(30)
        painter.penup()
        painter.backward(30)
        painter.right(90)

        # Preventing overlapping for the Walls and Doors. This does not work perfectly, and sometimes the doors are too small as a result of this
        if 0 <= doorDistance <= 20:
            doorDistance += 20

        painter.forward(distanceForward - initialWallDistance)

    # Creates the randomization of the doors. This works really well, as it makes the turtle go a random distance forward from the beginning of the line, and create a door
    painter.goto(beginCor)
    painter.pendown()
    painter.forward(doorDistance)
    painter.penup()
    painter.forward(20)
    painter.pendown()
    painter.forward(distanceForward - doorDistance)

    # Turn the turtle to create the next line
    painter.right(90)

    # Change the length of the line so the next one is a longer distance
    distanceForward += 15

# Keeps window open
screen.mainloop()

Your logic seems like it could produce unsolvable mazes, like your example one.

cdlane
  • 40,441
  • 5
  • 32
  • 81