When my turtles go outside of the light blue square, I want them to head towards the middle of the square so that they come inside the square again. Yet they don't seem to respond to my last for loop. The objective is to not let the turtles go outside the light blue square.
import turtle
import random
bobby = turtle.Turtle()
robby = turtle.Turtle()
bobby.color('blue')
robby.color('red')
bobby.shape('turtle')
robby.shape('turtle')
robby.speed(0)
bobby.speed(0)
def rectangle(x, y, width, height, color):
bobby.penup()
bobby.goto(x, y)
bobby.pendown()
bobby.setheading(0)
bobby.fillcolor(color)
for i in range(2):
bobby.begin_fill()
bobby.forward(width)
bobby.left(90)
bobby.forward(height)
bobby.left(90)
bobby.end_fill()
rectangle(-250, -250, 500, 500, 'lightblue')
directions = list(range(-45, 45))
forward = list(range(0, 25))
x = list(range(-250, 250))
y = list(range(-250, 250))
def jump(x, y, Aturtle):
Aturtle.penup()
Aturtle.goto(x, y)
Aturtle.pendown()
jump(random.choice(x), random.choice(y), bobby)
jump(random.choice(x), random.choice(y), robby)
def move_random(Aturtle, dir, forw):
Aturtle.setheading(Aturtle.heading() + random.choice(dir))
Aturtle.forward(random.choice(forw))
for i in range(250):
if bobby.xcor() and bobby.ycor() and robby.xcor() and robby.ycor() < 250:
move_random(bobby, directions, forward)
move_random(robby, directions, forward)
else:
bobby.towards(0, 0)
robby.towards(0, 0)
turtle.done()