2015-09-05 89 views
-2

我创建一个Python程序和方法必须允许两个不同的海龟接近或“尝试”在一个位置的Python:移动海龟同时

的海龟是否收敛的依赖或不收敛一个取决于乌龟的随机速度。

但是我的直接担忧是试图让两只不同的乌龟以不同的速度朝向相同的位置移动。

或者我有一个替代的想法,试图同时运行两行代码(两个龟的运动),但我是一个中级程序员,我不太确定这是甚至可能的。

谢谢你,感谢时间,如果位置是预先确定的答复我的问题

+0

任何你已经尝试过?就目前而言,这个问题有点含糊而广泛,你在寻找线索还是某些算法? – haraldkl

+0

是的。我现在有一只乌龟首先移动到位置,然后他第二只乌龟移动到位置,然后我有一种方法,它将乌龟的速度和每只乌龟从初始位置到最终位置的距离,并使用此信息来确定哪只乌龟会先到达那个位置。但是我想要有一个图形化表示两个海龟移动 –

+1

您是否使用标准Python [turtle](https://docs.python.org/3/library/turtle.html)模块? –

回答

0

左右,龟的速度是什么计算的时间提前,你可以有一个循环,它仅仅是将两种海龟(同一时间,在相同的框架内)朝着那个点移动,移动的距离取决于它们的速度。

2

您不能同时移动两个对象,只能模拟它。 这就是我在同一个问题上给我的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) 
+0

我通常使用生成器来实现这个相同的方法(每个乌龟移动一点点和产量)。不过,我已经添加了一个替代答案,用于显示您的乐趣(+1)使用计时器事件重做的示例。 – cdlane

0

使乌龟同时以不同速度移动的另一种方法是使用计时器事件。在这里,我修改@ 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() 

enter image description here