How do I make one turtle follow another turtle in python?

0 votes
How do I make one turtle follow another turtle in python? I'm trying to create something where one turtle moves around to stay safe from the other turtle. How do I achieve this?
Jul 8 in Python by Munaza
26 views

1 answer to this question.

0 votes

Try something like this:

from turtle import Turtle, Screen

pg = Screen()
pg.screensize(500, 500)
pg.title("Turtle Keys")

run = Turtle("turtle")
run.speed("fastest")
run.color("blue")
run.penup()
run.setposition(250, 250)

follow = Turtle("turtle")
follow.speed("fastest")
follow.color("red")
follow.penup()
follow.setposition(-250, -250)

def k1():
    run.forward(10)

def k2():
    run.left(45)

def k3():
    run.right(45)

def k4():
    run.backward(10)

def quitThis():
    pg.bye()

def follow_runner():
    follow.setheading(follow.towards(run))
    follow.forward(1)
    pg.ontimer(follow_runner, 10)

pg.onkey(k1, "Up")  # the up arrow key
pg.onkey(k2, "Left")  # the left arrow key
pg.onkey(k3, "Right")  # you get it!
pg.onkey(k4, "Down")
pg.onkey(quitThis, 'q')

pg.listen()

follow_runner()

pg.mainloop()
answered Jul 8 by Ayushi

Related Questions In Python

0 votes
1 answer

How do I make a delay in Python?

Hahah! Yes, you need to create a ...READ MORE

answered Jul 4 in Python by Pooja
22 views
0 votes
2 answers

How do I copy a file in python?

copy a file in python  from shutil ...READ MORE

answered Mar 27 in Python by rajesh
• 1,210 points
58 views
0 votes
1 answer

How do I copy a file in python?

Use the shutil module. copyfile(src, dst) Copy the contents ...READ MORE

answered Jul 31, 2018 in Python by Priyaj
• 56,520 points
34 views
+2 votes
1 answer

How do you make a block comment in python?

''' This is a multiline comment. I ...READ MORE

answered Aug 23, 2018 in Python by Priyaj
• 56,520 points
55 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 216 views
+4 votes
6 answers
0 votes
1 answer

How do I append one string to another in Python?

If you only have one reference to ...READ MORE

answered Oct 22, 2018 in Python by SDeb
• 13,160 points
41 views
0 votes
1 answer

In Python, how do I read a file line-by-line into a list?

with open(fname) as f:     content = f.readlines() # you ...READ MORE

answered Oct 9, 2018 in Python by SDeb
• 13,160 points
113 views