创建两个以上的海龟并移动它们

时间:2017-11-19 21:51:43

标签: python-3.x oop python-3.6 turtle-graphics

如何在屏幕上制作几只海龟并让它们一次移动?

1 个答案:

答案 0 :(得分:0)

您可以使用turtle.Turtle()创建许多海龟,然后您可以逐个使用它来进行小动作。海龟几乎会在同一时间移动。

import turtle

t1 = turtle.Turtle()
t2 = turtle.Turtle()

for x in range(36):
    # first turtle makes small move
    t1.left(10)  
    t1.forward(10)
    # second turtle makes small move
    t2.right(10)
    t2.forward(10)

turtle.done()

如果你想一直移动(并同时做其他事情)
然后你可以使用ontimer()进行小动作。

import turtle

def move_t1():
    # first turtle makes small move
    t1.left(10)  
    t1.forward(10)

    # repeat after 100ms
    turtle.ontimer(move_t1, 100)

def move_t2():
    # second turtle makes small move
    t2.right(10)  
    t2.forward(10)

    # repeat after 100ms
    turtle.ontimer(move_t2, 100)

t1 = turtle.Turtle()
t2 = turtle.Turtle()

move_t1() # first turtle makes first move
move_t2() # second turtle makes first move

turtle.done()