Python-同时移动两个海龟对象

Python-同时移动两个海龟对象,python,turtle-graphics,Python,Turtle Graphics,我想创建一个程序,其中一个海龟对象移动到用户单击鼠标的位置,而另一个海龟对象同时移动。我有第一部分,但我似乎无法让其他部分发挥作用 任何帮助都将不胜感激 这是我的密码。(这一部分的第一部分归功于@Cygwinnian) 我绝对不是Python的turtle模块的专家,但这里有一些代码,我认为它们能满足您的需求。当第一只海龟不在时,第二只海龟将来回移动: from turtle import * screen = Screen() # create the screen turtle = Tu

我想创建一个程序,其中一个海龟对象移动到用户单击鼠标的位置,而另一个海龟对象同时移动。我有第一部分,但我似乎无法让其他部分发挥作用

任何帮助都将不胜感激

这是我的密码。(这一部分的第一部分归功于@Cygwinnian)


我绝对不是Python的
turtle
模块的专家,但这里有一些代码,我认为它们能满足您的需求。当第一只海龟不在时,第二只海龟将来回移动:

from turtle import *

screen = Screen() # create the screen

turtle = Turtle() # create the first turtle
screen.onscreenclick(turtle.goto) # set up the callback for moving the first turtle

turtle2 = Turtle() # create the second turtle

def move_second(): # the function to move the second turtle
    turtle2.back(100)
    turtle2.forward(200)
    turtle2.back(100)
    screen.ontimer(move_second) # which sets itself up to be called again

screen.ontimer(move_second) # set up the initial call to the callback

screen.mainloop() # start everything running
这段代码创建了一个函数,可以将第二只海龟从其起始位置反复来回移动。它使用
屏幕的
ontimer
方法反复调度自己。一个稍微聪明一点的版本可能会检查一个变量,看看它是否应该退出,但我没有费心


这确实会使两只海龟移动,但它们实际上不会同时移动。在任何给定的时刻,只有一个人可以移动。我不确定是否有办法解决这个问题,除了可能将移动分割成更小的部分(例如,让海龟一次交替移动一个像素)。如果您想要更精美的图形,您可能需要从
turtle
模块开始

你能澄清一下你想要两只海龟同时移动的意思吗?我认为Python turtle模块不允许您同时移动多个对象,但是您可以在单击后让它们依次移动,或者让第二个turtle在移动第一个turtle的两次单击之间一直移动。@Blckknght是的,如果可能的话,第二个。我想“让第二只乌龟在移动第一只乌龟的两次点击之间一直移动。”谢谢你的持续帮助。绝对完美!非常感谢你!不确定这是否相关,但我使用的是Python2.7,我必须删除screen.mainloop(),并在它工作之前简单地使用mainloop()。
from turtle import *

screen = Screen() # create the screen

turtle = Turtle() # create the first turtle
screen.onscreenclick(turtle.goto) # set up the callback for moving the first turtle

turtle2 = Turtle() # create the second turtle

def move_second(): # the function to move the second turtle
    turtle2.back(100)
    turtle2.forward(200)
    turtle2.back(100)
    screen.ontimer(move_second) # which sets itself up to be called again

screen.ontimer(move_second) # set up the initial call to the callback

screen.mainloop() # start everything running