4

How do I make the collision? so the turtle/snake doesn't go out of the box. I'm trying to make them stay inside the (-200, -200) and (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()
ggorlen
  • 44,755
  • 7
  • 76
  • 106
Singh2013
  • 169
  • 1
  • 4
  • 12
  • What do you want to happen when it hits the edge? Coolest Turtle program I've seen, btw. – Veedrac Sep 24 '13 at 21:30
  • Is this a homework assignment? If so, you really should clarify that in your question. Stackoverflow can give you pointers, but asking for any more than that is unethical and cheating. There's no magic command in turtle to help you do what you want... you've got to check where each recursion step / iteration is relative to the box yourself. – accidental_PhD Sep 24 '13 at 21:34
  • @yasashiku yes this is a hw. Also, how would this be cheating since I need help how to do this. I have no idea where to start off with. – Singh2013 Sep 24 '13 at 21:39
  • @Veedrac thanks. I wanted to bounce off the wall and continuing drawing. – Singh2013 Sep 24 '13 at 21:41
  • You need to clarify that it's homework and you only want high level pointers - the way it's worded now, it implies you want someone to solve it for you. Which would be cheating. I suggest editing your question to emphasize this. – accidental_PhD Sep 24 '13 at 21:42
  • @yasashiku Okay, I added that this is my Lab Homework. I never used pointers before or something like that. I have learned turtle function that is all. – Singh2013 Sep 24 '13 at 21:46
  • @Singh Please specify what you want to happen when the snake hits the edge of the box. There are several things you can do upon a collision event. – Shashank Sep 24 '13 at 21:47
  • @ShashankGupta I got it now. THanks tho. I used position and if statement to prevent the drawing to go out of the box. – Singh2013 Sep 24 '13 at 23:16

2 Answers2

1

I suggest a large if like:

if turtle.ycor() >= 200 or turtle.ycor() <=-200 or  turtle.xcor() >= 200 or turtle.xcor <= -200

This worked for me!

Tunaki
  • 132,869
  • 46
  • 340
  • 423
E1247L
  • 13
  • 4
0

In both the recursive version and the iterative version, you're turning a random direction and moving forward a random length. If you want to avoid the bounding box, these numbers need to know that the bounding box is there.

There are few things you could do, depending on the kind of behavior you want. Probably the easiest would be to put in a check if the new coordinate is outside of the box, and if so, change course appropriately.

If you want it to gradually avoid the box, you could factor in the distance to the nearest edge in either the turning or moving decisions.

In either case, turtle.pos() will be helpful.

accidental_PhD
  • 744
  • 8
  • 17