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.