[英]Python Turtle Graphics
"""Import turtle to start drawing"""
from turtle import *
"""imports the random function"""
from random import *
"""draws the bounding box"""
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()
"""I want the snake to stay within that box above"""
"""draws the recursive snake"""
def drawSnakeRec(segments, length):
"""Draws the iterative snake and returns the total 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))
"""draws the iterative snake"""
def drawSnakeIter(segments, length):
TL = 0
"""Draws the iterative snake and returns the total length"""
while segments > 0:
color(random(), random(), random())
pensize(randint(1,10))
forward(length)
left(randint(-30, 30))
TL += length
segments -=1
return TL
"""defines the main function"""
def main():
segments = int(input("Enter the segments between 0 and 500: "))
bounding_box()
hideturtle()
speed('fast')
"""ask for the number of segments"""
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:
"""draw the first snake"""
x = drawSnakeRec(segments, randint(1, 20))
print("Recursive Snake's Length is:",x,"Units")
input("Press Enter to draw Iterative Snake")
up()
goto(0,0)
reset()
"""start the second drawing"""
bounding_box()
y = drawSnakeIter(segments, randint(1,20))
print("Iterative Snake's Length is:",y," Units")
input("Press Enter to exit...")
bye()
"""runs the main function"""
main()
Question: How can I keep the snake inside the bounding box? 问题:如何将蛇保留在边界框中? I want the snake to turn 180 degrees when it hits the bounding box. 我希望蛇撞到边界框时将其旋转180度。 Please be as detailed as you can. 请尽可能详细。
I think the turtle has no detection and can not see.. So you need to test for coordinates with pos()
or xcor()
and ycor()
. 我认为乌龟没有检测到并且看不到..因此,您需要使用pos()
或xcor()
和ycor()
测试坐标。
I think your fence is 200 left, up, right and down from x = 0, y = 0. (You goto(0,0)
.) 我认为您的栅栏从x = 0,y = 0到左,上,右和下200。(您goto(0,0)
。)
So your test would be 所以你的测试是
if xcor() > 200: # move to left, make x smaller, turn around!
if xcor() < -200: # move to right, make x bigger, turn around!
if ycor() > 200:
if ycor() < -200:
You need to fill in the x and y. 您需要填写x和y。 Maybe like this: 可能是这样的:
if xcor() > 200: setx(200)
if xcor() < -200: setx(-200)
if ycor() > 200: sety(200)
if ycor() < -200: sety(-200)
But that may not look too pretty. 但这看起来可能不太漂亮。 You can let it walk in a circle until it is inside again.. Maybe. 您可以让它走一圈直到它再次进入内部。 It is up to you. 它是由你决定。 You wanted 180 degree turn around.. you can put this instead of setx and sety. 您想要180度转弯..您可以放置它而不是setx和sety。 Hint: if you make small steps nobody notices that you are out of the cage. 提示:如果您迈出小步,没人会注意到您已经离开笼子了。
If you need more information about turtle you can use help('turtle')
. 如果您需要有关turtle的更多信息,可以使用help('turtle')
。 It will show you a lot of functions that you can use. 它将向您展示许多可以使用的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.