# 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()我需要帮助阻止这些门和障碍发生在彼此之上。我还附上了一幅描述我的意思的图片。
这里的代码是我所要做的,以防止这两个代码之间的相互影响:
if doorDistance == range(0, 21):
doorDistance + 20
painter.forward(distanceForward - initialWallDistance)
else:
painter.forward(distanceForward - initialWallDistance)什么都没起作用。现在我完全糊涂了,我也不知道自己在做什么。如有任何解释/帮助,将不胜感激
注:我是一个初学者,所以我不会使用任何复杂的技术


发布于 2022-03-21 16:49:12
这段代码不适用于Python:
if doorDistance == range(0, 21):
doorDistance + 20应该是这样的:
if 0 <= doorDistance <= 20:
doorDistance += 20但这并不能解决你的问题。你的逻辑,以避免门和障碍重叠,是行不通的。当你从中心旋转时,你会设置障碍指向下一层,并留下门的空隙。但是,当这些障碍所在的所有记忆都丢失时,重叠就会发生在下一个螺旋线上。
我将您的代码重新编写成Python (仍未完成):
# 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()你的逻辑似乎会产生无法解决的迷宫,就像你的例子一样。
https://stackoverflow.com/questions/71550739
复制相似问题