Python:同时移动海龟

时间:2015-09-05 14:53:29

标签: python turtle-graphics

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

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

但我的直接关注是试图让两只不同的海龟以不同的速度向相同的位置移动。

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

感谢您感谢时间回复我的问题

3 个答案:

答案 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)

答案 1 :(得分:0)

因此,如果位置是预先确定的,并且龟的速度是提前计算的,那么你可以有一个循环只是将两只海龟(同时,在同一帧中)移动到那个点,他们的行动将取决于他们的速度。

答案 2 :(得分:0)

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

Error: ENOENT: no such file or directory, scandir '...\node_modules\ionic-angular\templates\page'
    at Error (native)
    at Object.fs.readdirSync (fs.js:856:18)
    at getTemplatesInDir (..\AppData\Roaming\npm\node_modules\ionic\node_modules\@ionic\app-generators\index.js:23931:65)
    at Generator.renderAndWriteTemplates (..\AppData\Roaming\npm\node_modules\ionic\node_modules\@ionic\app-generators\index.js:25309:
116)
    at ..\AppData\Roaming\npm\node_modules\ionic\node_modules\@ionic\app-generators\index.js:25304:26


ENOENT: no such file or directory, scandir '..\node_modules\ionic-angular\templates\page' (CLI v2.1.4)

Your system information:

Cordova CLI: 6.3.1
Gulp version:  CLI version 3.9.0
Gulp local:   Local version 3.9.1
Ionic Framework Version: 2.0.0-beta.11
Ionic CLI Version: 2.1.4
Ionic App Lib Version: 2.1.2
OS: Windows 7 SP1
Node Version: v5.1.0

enter image description here

相关问题