创建两个以上的海龟并移动它们 [英] Create more than two turtles and moving them

查看:82
本文介绍了创建两个以上的海龟并移动它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

你可以使用turtle.Turtle() 来创建许多海龟,然后你可以用它一个一个地做小动作.海龟几乎同时移动.

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

如果你想一直移动(同时做其他事情)
那么你可以使用ontimer()来做小动作.

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

这篇关于创建两个以上的海龟并移动它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆