Create more than two turtles and moving them

故事扮演 提交于 2019-12-24 19:46:37

问题


How to make few turtles in a screen and make them move one at once?


回答1:


You can use turtle.Turtle() to create many turtles and then you can use it one by one to make small move. Turtles will move almost at the same time.

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()

If you want to move all the time (and do other things at the same time)
then you can use ontimer() to make small moves.

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()


来源:https://stackoverflow.com/questions/47382389/create-more-than-two-turtles-and-moving-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!