Skip to content

Instantly share code, notes, and snippets.

@horstjens
Created October 10, 2024 12:35
Show Gist options
  • Save horstjens/94153279ab20278a8bd1ee3dca1ce78c to your computer and use it in GitHub Desktop.
Save horstjens/94153279ab20278a8bd1ee3dca1ce78c to your computer and use it in GitHub Desktop.
turtle chaser 01
import turtle
# turtle bounces from screen edge
#w, h = 600,400
#turtle.setup(w*2, h*2)
screen = turtle.Screen()
screen.setup(1.0, 0.9)
w, h = screen.window_width()/2, screen.window_height()/2
a = turtle.Turtle()
#a.speed(0)
a.shape("turtle")
a.color("blue")
a.left(10)
b = turtle.Turtle()
b.shape("turtle")
b.color("red")
while True:
a.forward(10)
winkel = a.heading()
if a.xcor() > w:
a.setx(w)
a.setheading(180-winkel)
elif a.xcor() < -w:
a.setx(-w)
a.setheading(180-winkel)
if a.ycor() > h:
a.sety(h)
a.setheading(360-winkel)
elif a.ycor() < -h:
a.sety(-h)
a.setheading(360-winkel)
# ----------
winkel2 = b.towards(a)
#diff = b.heading() - winkel2
diff = (winkel2 - b.heading() - 0) %360
#print(diff)
if abs(diff) < 4:
pass
elif diff > 180:
b.right(3)
elif diff < 180:
b.left(3)
turtle.title(f"b towards a:{winkel2:.2f}, b heading: {b.heading():.2f}, diff: {diff:.2f}")
#diff = b.heading() - winkel2
#b.left(-diff / 2)
b.forward(5)
turtle.exitonclick()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment