I am making a game in Turtle with Python, where one Turtle (called Lewi) follows another (called Ella). For now, the goal of the game is that Ella, who is slightly slower than Lewi, should follow Lewi. Lewi moves according to presses of the arrow keys.
Ella does follow Lewi, but the problem is that if, say, you press the 'right' key twice or more before Ella has arrived to Lewi's position, and then later let her catch up to his position, then she will start moving back to all of her previous positions where she failed to catch up to Lewi, and from then goes on to go to all of Lewi's positions that she missed.
Ella shouldn't be retracing her steps, so how do I fix this problem? Here is the code:
import turtle
image = "C:/Python27/Pythonprogramming/image.gif"
screenr = turtle.Screen()
Lewi = turtle.Turtle()
Ella = turtle.Turtle()
screenr.addshape(image)
Lewi.shape(image)
screenr.bgpic("winXP.gif")
screenr.setup(1279, 815)
Lewi.penup()
speed = 50
def up():
Lewi.sety(Lewi.ycor()+speed)
Ella.speed(1)
Ella.goto(Lewi.pos())
if Ella.pos() == Lewi.pos():
print("loo")
def down():
Lewi.sety(Lewi.ycor()-speed)
Ella.speed(1)
Ella.goto(Lewi.pos())
if Ella.pos() == Lewi.pos():
print("loo")
def left():
Lewi.forward(-speed)
Ella.speed(1)
Ella.goto(Lewi.pos())
if Ella.pos() == Lewi.pos():
print("loo")
def right():
Lewi.forward(speed)
Ella.speed(1)
Ella.goto(Lewi.pos())
if Ella.pos() == Lewi.pos():
print("loo")
screenr.onkey(up, "Up")
screenr.onkey(down, "Down")
screenr.onkey(right, "Right")
screenr.onkey(left, "Left")
screenr.listen()
turtle.mainloop()
I believe the problem is that Ella is dependent on Lewi for her motion. If we make her independent, running on a timer event, then she can react to Lewi's motion and chase him more naturally. Also, since you'll be hitting the motion keys rapidly and repeatedly, I've changed their handlers to block out hits while the current hit is being processed. I've also changed the handlers to set a heading and then move forward:
from turtle import Turtle, Screen
screen = Screen()
Lewi = Turtle(shape="turtle")
Lewi.color("red")
Lewi.penup()
Ella = Turtle(shape="turtle")
Ella.color("green")
Ella.speed("slowest")
screen.setup(1279, 815)
speed = 20
def up():
screen.onkey(None, "Up")
Lewi.setheading(90)
Lewi.forward(speed)
screen.onkey(up, "Up")
def down():
screen.onkey(None, "Down")
Lewi.setheading(270)
Lewi.forward(speed)
screen.onkey(down, "Down")
def left():
screen.onkey(None, "Left")
Lewi.setheading(180)
Lewi.forward(speed)
screen.onkey(left, "Left")
def right():
screen.onkey(None, "Right")
Lewi.setheading(0)
Lewi.forward(speed)
screen.onkey(right, "Right")
def move_Ella():
if Ella.pos() != Lewi.pos():
Ella.setheading(Ella.towards(Lewi))
Ella.forward(speed)
screen.ontimer(move_Ella, 200)
screen.onkey(up, "Up")
screen.onkey(down, "Down")
screen.onkey(right, "Right")
screen.onkey(left, "Left")
screen.listen()
screen.ontimer(move_Ella, 200)
screen.mainloop()
I removed your image information so this example can be run by anyone -- you can easily put your images back into the code.
If you want Ella to follow the exact same path as Lewi, just at a slower pace, that too can be done. Indicate such and I'll update the example.