如何进行碰撞?所以乌龟/蛇不会开箱即用。试图让它们停留在 (-200, -200) 和 (200, 200) 内。
from turtle import *
from random import *
def bounding_box():
up()
right(90)
forward(200)
down()
left(90)
forward(200)
left(90)
forward(400)
left(90)
forward(400)
left(90)
forward(400)
left(90)
forward(200)
up()
goto(0,0)
down()
def drawSnakeRec(segments, length):
if segments <= 0 or length <= 0:
return 0
else:
color(random(), random(), random())
forward(length)
pensize(randint(1,10))
left(randint(-30, 30))
return length + drawSnakeRec(segments - 1, randint(1,20))
def drawSnakeIter(segments, length):
TL = 0
while segments > 0:
color(random(), random(), random())
pensize(randint(1,10))
forward(length)
left(randint(-30, 30))
TL += length
segments -=1
return TL
def main():
segments = int(input("Enter the segments between 0 and 500: "))
bounding_box()
hideturtle()
speed('fast')
if segments < 0 or segments > 500:
print("Segments is out of range. Segment must be between 0 and 500 inclusive")
input("Press enter to close")
else:
x = drawSnakeRec(segments, randint(1, 20))
print("Recursive Snake's Length is:",x,"Units")
input("Press Enter to go on Iterative Snake")
up()
goto(0,0)
reset()
bounding_box()
y = drawSnakeIter(segments, randint(1,20))
print("Iterative Snake's Length is:",y," Units")
input("Press Enter to exit...")
bye()
main()
有什么帮助吗?谢谢你。所以,我正在努力完成这件事。是的,这是我要完成的实验室作业。如果你们能帮助我或提供我能做的事情,那就太好了。PS:我不擅长乌龟。我以前从未学过乌龟,好吧,我学过一点python。