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, 2019 in Python by Munaza
4,736 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, 2019 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, 2019 in Python by Pooja
1,026 views
0 votes
1 answer

How do I check if a given Python string is a substring of another one?

Try using in like this: >>> x = 'hello' >>> y ...READ MORE

answered Nov 26, 2020 in Python by Gitika
• 65,910 points
365 views
0 votes
0 answers

i want to ask that how can i run one file of python in another file in jupyter notebook

motion_detection.ipynb # Python program to implement # Webcam ...READ MORE

Dec 16, 2020 in Python by Ramsha
• 120 points
704 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, 2019 in Python by rajesh
• 1,270 points
971 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,070 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
3,491 views
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,300 points
512 views
0 votes
2 answers

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

readline function help to  read line in ...READ MORE

answered Jun 21, 2020 in Python by sahil
• 580 points
1,476 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP