Running turtles simultaneously with turtles graphics

How can I make 4 different turtles at the same time? In addition, how to make a human figure for a method Turtle.shape? I know that there is a Screen method called register_shape, but I could not 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, tSecondA tThirdis the position, and t1, t2, t3, t4are the turtles. I want all turtles to move in unison.

+3
source share
4 answers

Here is the documentation for register_shape

, , . , . , .

- ,

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

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

, . , , , , .

, , , .

, , . , , , , , - .

import turtle
import random
wn = turtle.Screen()

bob = turtle.Turtle()
mary = turtle.Turtle()
fred = turtle.Turtle()
jane = turtle.Turtle()

turtles = [bob, mary, fred, jane]

for move in range(10):
    for item in turtles:
        item.left(random.randrange(-180, 180))
        item.forward(50)

wn.exitonclick()
0

:

import turtle
import threading

window = turtle.Screen()
window.bgcolor("red")


class myTurtle (threading.Thread):
    def __init__(self,init_angle):
        threading.Thread.__init__(self)
        self.noel = turtle.Turtle()
        self.noel.rt(init_angle)
        self.init_angle = init_angle
    def run(self):
        for self.i in range(self.init_angle,self.init_angle + 9):
            for self.j in range(4):
                self.noel.fd(100)
                self.noel.rt(90)
                self.noel.rt(1)


t1 = myTurtle(0)
t2 = myTurtle(90)

t1.start()
t2.start()
window.exitonclick()
0
source

For all turtles to move simultaneously (if I understand the question correctly), you need to use the method tracer().

turtle.tracer(
    (First number is how many frames go by before turtle updates the screen),
    (second number is a delay in milliseconds)
)

Because the turtle refreshes the screen every time the turtle moves, the tracer is how you can make them move at the same time. Usually turtle.tracer(0, 20)should be exact or turtle.tracer(1, 20).

0
source

All Articles