Running turtles simultaneously with turtle graphics

15,031

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()
Share:
15,031
Mission Impossible
Author by

Mission Impossible

Updated on June 04, 2022

Comments

  • Mission Impossible
    Mission Impossible almost 2 years

    How can I make 4 different turtles move at the same time? Also, how to make a person shape for the Turtle.shapemethod? 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.

  • Mission Impossible
    Mission Impossible about 12 years
    I thinking of making a sports field graphic with players, so I'd need to make each of them move in different directions at the same time.
  • Matt
    Matt about 12 years
    You could also try delaying the animation update so they appear to have moved at the same time turtle.update
  • Mission Impossible
    Mission Impossible about 12 years
    I don't think update will help. Also, I'm running python 3.2, and delay is a screen method, it might have been different in previous versions. I called delay, and it changed the time of the animation, not the time between animations. So a screen.delay(200) took 200 milliseconds but screen.delay(5) took 5, and the turtles still have separately. thx for you help
  • Matt
    Matt about 12 years
    You should probably accept answers to all the previous questions people have helped you with. Or at least provide feedback as to why you didn't accept them. People will stop answering your questions if you never accept an answer
  • Mission Impossible
    Mission Impossible about 12 years
    Sorry, I tried what you had suggested, but the update and delay methods didn't appear useful for making the animation seem continuous, but I appreciate the help.
  • Mission Impossible
    Mission Impossible about 12 years
    Where do I go to get numPy for Mac-Lion?
  • Matt
    Matt about 12 years
    here, but I just used it to get random numbers to move and turn by...you can change them to constants if you like. Just replace the value inside t.right() and t.forward()
  • Mission Impossible
    Mission Impossible about 12 years
    That's brilliant. Cool drawing.
  • Mission Impossible
    Mission Impossible about 12 years
    As I was implementing this idea, I noticed one issue. Everything is simultaneous, but it becomes instantaneous. It appears that in order for the turtles to move simultaneously, they have to move almost instantaneously. Would there be a way to keep them moving together, but not moving at relativistic speeds?