Question

I would like to create a program where one Turtle object always stays above all of the other Turtle objects. I don't know if this is possible, but any help would be apprecated.

This is my code:

from turtle import *
while True:
    tri = Turtle()
    turtle = Turtle()
    tri.pu()
    tri.pencolor("white")
    tri.color("black")
    tri.shape("turtle")
    tri.bk(400)
    turtle = Turtle()
    turtle.pu()
    turtle.pencolor("white")
    turtle.shape("square")
    turtle.color("white")
    turtle.pu()
    turtle.speed(0)
    tri.speed(0)
    turtle.shapesize(100,100,00)
    setheading(towards(turtle))
    while tri.distance(turtle) > 10:
        turtle.ondrag(turtle.goto)
        tri.setheading(tri.towards(turtle))
        tri.fd(5)
    clearscreen()

No correct solution

OTHER TIPS

Why not just do all the drawing for the "bottom" turtle first? Then do the drawing for the "top" turtle? This should make the top turtle always visible.

My Observed Rules of Turtle Layering:

  • Multiple Turtles moving to same location: last to arrive is on top.

  • Same thing drawn by multiple turtles: there are no rules!

To illustrate my second point, consider this code:

from turtle import Turtle, Screen

a = Turtle(shape="square")
a.color("red")
a.width(6)
b = Turtle(shape="circle")
b.color("green")
b.width(3)

b.goto(-300, 0)
b.dot()
a.goto(-300, 0)
a.dot()

a.goto(300, 0)
b.goto(300, 0)

screen = Screen()
screen.exitonclick()

Run it and observe the result. On my system, the final goto() draws a long green line over the red one but the green line disappears as soon as it has finished drawing. Comment out the two calls to dot() and observe again. Now the green line remains over the red one. Now change the calls from dot() to stamp() or circle(5) instead. Observe and formulate your own rule...

Now back to your example, which is badly flawed (you're actually manipulating three turtles, not two!) Here's my simplification:

from turtle import Turtle, Screen

tri = Turtle(shape="turtle")
tri.color("black")
tri.pu()

turtle = Turtle(shape="square")
turtle.shapesize(4)
turtle.color("pink")
turtle.pu()

def drag_handler(x, y):
    turtle.ondrag(None)
    turtle.goto(x, y)
    turtle.ondrag(drag_handler)

turtle.ondrag(drag_handler)

tri.bk(400)
while tri.distance(turtle) > 10:
    tri.setheading(tri.towards(turtle))
    tri.fd(5)

screen = Screen()
screen.mainloop()

You can tease tri by dragging the pink square until tri catches up with it. Ultimately, tri will land on top as long as the square isn't moving when tri catches it. If you drag the square over tri, then it will temporarily cover him as it is the "last to arrive".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top