move - How to get Python turtle to stop trying to catch up the turtle it's following? -
i making game in turtle python, 1 turtle (called lewi) follows (called ella). now, goal of game ella, slower lewi, should follow lewi. lewi moves according presses of arrow keys.
ella follow lewi, problem if, say, press 'right' key twice or more before ella has arrived lewi's position, , later let catch position, start moving of previous positions failed catch lewi, , goes on go of lewi's positions missed.
ella shouldn't retracing steps, how fix problem? here 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 problem ella dependent on lewi motion. if make independent, running on timer event, can react lewi's motion , chase him more naturally. also, since you'll hitting motion keys rapidly , repeatedly, i've changed handlers block out hits while current hit being processed. i've changed handlers set heading , 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 image information example can run -- can put images code.
if want ella follow exact same path lewi, @ slower pace, can done. indicate such , i'll update example.
Comments
Post a Comment