Mission Impossible Mission Impossible - 6 months ago 39
Python Question

Running turtles simultaneously with turtle graphics

How can I make 4 different turtles move at the same time? Also, how to make a person shape for the

Turtle.shape
method? I know there's a Screen method called
register_shape
, but I couldn't find any documentation on it.

def turtles(self, base):
self.t4.goto(self.tFirst)
self.t1.goto(self.tSecond)
self.t2.goto(self.tThird)
self.t3.goto(self.tHome)
if base >= 2:
self.t4.goto(self.tSecond)
self.t1.goto(self.tThird)
self.t2.goto(self.tHome)
if base >= 3:
self.t4.goto(self.tThird)
self.t1.goto(self.tHome)
if base == 4:
self.t4.goto(self.tHome)


tFirst
,
tSecond
an
tThird
are positions, and
t1
,
t2
,
t3
,
t4
are turtles. I want all of the turtles to move in unison.

Answer

Here is the documentation for register_shape

As for your first question, I'm not sure exactly what you mean. Making them all move in separate threads would be my first idea, but even then they technically don't move at the same time. I have never actually used turtle graphics, but just know of the concept.

This does something close to what you are talking about

import turtle
import numpy as np

tlist = list()
colorlist = ["red", "green", "black", "blue", "brown"]
for i in xrange(5):
    tlist.append(turtle.Turtle(shape="turtle"))
    tlist[i].color(colorlist[i])
    tlist[i].speed(1)
screen = turtle.getscreen()
for i in xrange(100):
    screen.tracer(1000)
    for t in tlist:
        t.right((np.random.rand(1) - .5) * 180)
        t.forward(int((np.random.rand(1) - .5) * 100))
    screen.update()

or

import turtle
#import numpy as np
from time import sleep

tlist = list()
colorlist = ["red", "green", "black", "blue", "brown"]
for i in xrange(5):
    tlist.append(turtle.Turtle(shape="turtle"))
    tlist[i].color(colorlist[i])
    tlist[i].speed(1)
screen = turtle.getscreen()
for i in xrange(100):
    screen.tracer(1000)
    for i, t in enumerate(tlist):
        #t.right((np.random.rand(1) - .5) * 180))
        t.right(33 * i)
        #t.forward(int((np.random.rand(1) - .5) * 100))
        t.forward(50 * i)
    sleep(1)
    screen.update()