2014-02-18 40 views
0
"""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() 

问题: 如何将蛇保持在边界框内?我想让蛇在碰到边界框时转动180度。请尽可能详细。Python Turtle Graphics

+0

我还没有尝试过任何东西。有人告诉我应该使用setpos()函数,但我不知道如何使用它。 – Learner

+0

http://docs.python.org/2/library/turtle.html#turtle.setpos – jonrsharpe

回答

0

我觉得龟没有检测,不能看.. 所以你需要测试的坐标与pos()xcor()ycor()

我觉得你的围栏是200左,上,右,从x = 0下来,Y = 0(你goto(0,0)

所以,你的测试将是

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: 

您需要填写x和y。 也许是这样的:

if xcor() > 200: setx(200) 
if xcor() < -200: setx(-200) 
if ycor() > 200: sety(200) 
if ycor() < -200: sety(-200) 

但是,这可能看起来不太漂亮。你可以让它走一圈,直到它再次进入内部。也许。它是由你决定。你想180度转身..你可以把这个,而不是setx和sety。提示:如果你做小步骤,没有人注意到你离开了笼子。

如果您需要更多关于龟的信息,您可以使用help('turtle')。它会告诉你很多你可以使用的功能。

+0

经过一番调整后,终于奏效了。 Thnx很多! – Learner

+0

欢迎!你可以发布你的修改并接受答案吗?顺便说一句:谷歌的“Python for循环”。你可以使用它。 [这里有一些Python的教程。](http://python.opentechschool.org/)也是一个龟。你可能已经知道了很多。 – User