Question

"""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. Please be as detailed as you can.

Était-ce utile?

La solution

I think the turtle has no detection and can not see.. So you need to test for coordinates with pos() or xcor() and ycor().

I think your fence is 200 left, up, right and down from x = 0, y = 0. (You 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. 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. 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'). It will show you a lot of functions that you can use.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top