Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 蟒蛇:同时移动海龟_Python_Turtle Graphics - Fatal编程技术网

Python 蟒蛇:同时移动海龟

Python 蟒蛇:同时移动海龟,python,turtle-graphics,Python,Turtle Graphics,我正在创建一个python程序,其中一个方法必须允许两个不同的海龟接近或“尝试”在一个位置会聚 海龟是否会聚的依赖性取决于海龟的随机速度 但我最关心的是让两只不同的海龟以不同的速度向同一地点移动 或者另一个想法,我曾经尝试同时运行两行代码(两个海龟的动作),但我是一个中级程序员,我不太确定这是否可能 感谢您抽出时间回答我的问题因此,如果位置是预先确定的,并且海龟的速度是提前计算的,您可以有一个循环,只需将两只海龟(同时,在同一帧中)移向该点,它们移动的距离取决于它们的速度。不能同时移动两个对象,

我正在创建一个python程序,其中一个方法必须允许两个不同的海龟接近或“尝试”在一个位置会聚

海龟是否会聚的依赖性取决于海龟的随机速度

但我最关心的是让两只不同的海龟以不同的速度向同一地点移动

或者另一个想法,我曾经尝试同时运行两行代码(两个海龟的动作),但我是一个中级程序员,我不太确定这是否可能


感谢您抽出时间回答我的问题

因此,如果位置是预先确定的,并且海龟的速度是提前计算的,您可以有一个循环,只需将两只海龟(同时,在同一帧中)移向该点,它们移动的距离取决于它们的速度。

不能同时移动两个对象,只能模拟它。 这是我给10年级学生的分数,作为对同一问题的提示。 不完美,但它显示了概念

##turtleChase.py
##Randomly place the turtles on the screen
##one turtle will then chase the other as it moves across the screen
##
##input: mouseclick events
##output: graphics on screen, text in Shell

##pseudocode:
##setup your screen
##setup the turtles
##randomly place both turtles
##randomly find a location to move the first turtle to
##turn the first turtle towards the desired location
##in a loop:
##move the first turtle a small distance
##determine the heading to move the second turtle to the first turtle
##move the second turtle a small distance
##is the second turtle at the same position of the first turtle?
##if it is
##quit the loop
##if not
##continue the loop


import turtle, random
random.seed()

# setup the output window
picSize = (400,600)
playGround = turtle.Screen()
playGround.screensize(picSize[0], picSize[1])

#setup the turtles
bob = turtle.Turtle()
bob.ht()
bob.color('red')
jeff = turtle.Turtle()
jeff.ht()
jeff.color('blue')

# find random positions for the turtles
# use the picSize variable so that we can change the screensize easily
# without having to change a lot of code.
# if the screen is 600 pixels tall, then the y-coordinates go from
# -300 to +300, just like in math.
jeffx = random.randint(-picSize[0]/2,picSize[0]/2)
jeffy = random.randint(-picSize[1]/2,picSize[1]/2)
bobx = random.randint(-picSize[0]/2,picSize[0]/2)
boby = random.randint(-picSize[1]/2,picSize[1]/2)

# find a point to move bob to
bobNewx = random.randint(-picSize[0]/2,picSize[0]/2)
bobNewy = random.randint(-picSize[1]/2,picSize[1]/2)
newBobPos = (bobNewx,bobNewy)

print(jeffx,jeffy)
print(bobx,boby)

# place the turtles and show them
bob.setpos(bobx,boby)
jeff.setpos(jeffx,jeffy)
jeff.st()
bob.st()

#rotate bob towards its target location
bobTurn = bob.towards(newBobPos)
bob.setheading(bobTurn)

while bob.position() != jeff.position():
   bob.fd(1)
   jeffTurn = jeff.towards(bob)
   jeff.setheading(jeffTurn)
   jeff.fd(1.5)

另一种让海龟以不同速度同时移动的方法是使用计时器事件。在这里,我将@dougc905的有趣示例改为使用计时器:

from turtle import Turtle, Screen
from random import seed, randint

seed()

DELAY = 100  # milliseconds

# setup the output window
picSize = (400, 600)
playGround = Screen()
playGround.screensize(*picSize)

# setup the turtles
bob = Turtle(shape='turtle', visible=False)
bob.penup()
bob.color('red')
bob.speed('slow')

jeff = Turtle(shape='turtle', visible=False)
jeff.penup()
jeff.color('blue')
jeff.speed('normal')

x_quadrant = -picSize[0] // 2, picSize[0] // 2
y_quadrant = -picSize[1] // 2, picSize[1] // 2

# find random positions for the turtles
jeff_xy = randint(*x_quadrant), randint(*y_quadrant)
bob_xy = randint(*x_quadrant), randint(*y_quadrant)

# find a point to move bob to and rotate towards its target location
bobNew_xy = randint(*x_quadrant), randint(*y_quadrant)
bob.setheading(bob.towards(bobNew_xy))

# place the turtles and show them
jeff.setpos(jeff_xy)
jeff.showturtle()
jeff.pendown()

bob.setpos(bob_xy)
bob.showturtle()
bob.pendown()

# bob's motion is in a straight line
def moveStraight():
    bob.fd(bob.speed())
    playGround.ontimer(moveStraight, DELAY)

# jeff's motion is towards bob
def moveToward():
    if bob.position() != jeff.position():
        jeff.setheading(jeff.towards(bob))
        jeff.fd(jeff.speed())
    playGround.ontimer(moveToward, DELAY)

moveStraight()
moveToward()

playGround.exitonclick()

您已经试过了吗?目前,这个问题有点模糊和宽泛,你是在寻找线程,还是一些算法?是的。我现在有一只乌龟,它先移动到这个位置,然后第二只乌龟移动到这个位置,我有一个方法,计算乌龟的速度和每只乌龟从初始位置到最终位置的距离,并利用这些信息确定哪只乌龟会先到达那个位置。但是我想要两个海龟移动的图形化表示。您是否使用标准Python模块?我通常使用生成器来实现相同的方法(每个海龟移动一点并产生收益)。不过,我添加了一个替代答案,显示了您使用计时器事件重做的乐趣(+1)示例。