2014-02-18 59 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