0

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()
cdlane
  • 40,441
  • 5
  • 32
  • 81
Tompan
  • 27
  • 4
  • Does this answer your question? [Why does "a == x or y or z" always evaluate to True?](https://stackoverflow.com/questions/20002503/why-does-a-x-or-y-or-z-always-evaluate-to-true) – Tomerikoo Nov 10 '21 at 13:53
  • 1
    This is regarding the line `if bobby.xcor() and bobby.ycor() and robby.xcor() and robby.ycor() < 250:` in teh last for loop – Tomerikoo Nov 10 '21 at 13:53
  • 1
    `and` doesn't work as you expect - you have to compare every value with `< 250` separatelly - `bobby.xcor() < 250 and bobby.ycor() < 250 and ...` – furas Nov 10 '21 at 14:03

1 Answers1

0

This logic:

bobby.towards(0, 0)

is incomplete -- it simply does a calculation, it doesn't affect the turtle's direction. To achieve that, you need to do:

bobby.setheading(bobby.towards(0, 0))

This logic is wishful thinking and wrongheaded:

if bobby.xcor() and bobby.ycor() and robby.xcor() and robby.ycor() < 250:

Bobby's collisions are independent of Robby's so we can't handle them both with a single if statement. And we need to make explicit comparisons of the positions although Python does give us a reasonably compact way to do that:

if -250 < bobby.xcor() < 250 and -250 < bobby.ycor() < 250:
    move_random(bobby, directions, distances)
else:
    bobby.setheading(bobby.towards(0, 0))

Here's a rework of your code to address the above and any other issues I found:

from turtle import Screen, Turtle
from random import choice

def rectangle(x, y, width, height, color):
    bobby.penup()
    bobby.goto(x, y)
    bobby.pendown()
    bobby.setheading(0)
    bobby.fillcolor(color)

    for _ in range(2):
        bobby.begin_fill()

        bobby.forward(width)
        bobby.left(90)
        bobby.forward(height)

        bobby.end_fill()
        bobby.left(90)

    bobby.fillcolor(bobby.pencolor())


def jump(x, y, turtle):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()

def move_random(turtle, directions, distances):
    turtle.setheading(turtle.heading() + choice(directions))
    turtle.forward(choice(distances))

def move():
    if -250 < bobby.xcor() < 250 and -250 < bobby.ycor() < 250:
        move_random(bobby, directions, distances)
    else:
        bobby.setheading(bobby.towards(0, 0))
        move_random(bobby, [0], distances)

    if -250 < robby.xcor() < 250 and -250 < robby.ycor() < 250:
        move_random(robby, directions, distances)
    else:
        robby.setheading(robby.towards(0, 0))
        move_random(robby, [0], distances)

    screen.ontimer(move)

screen = Screen()

bobby = Turtle()
bobby.shape('turtle')
bobby.color('blue')
bobby.speed('fastest')

robby = Turtle()
robby.shape('turtle')
robby.color('red')
robby.speed('fastest')

rectangle(-250, -250, 500, 500, 'lightblue')

directions = list(range(-45, 45))
distances = list(range(0, 25))

x = list(range(-250, 250))
y = list(range(-250, 250))

jump(choice(x), choice(y), bobby)
jump(choice(x), choice(y), robby)

move()

screen.mainloop()

enter image description here

cdlane
  • 40,441
  • 5
  • 32
  • 81