Question

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.

Était-ce utile?

La solution

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

Autres conseils

Thought I would offer this up as a possibility. I recently wanted to have all my turtles dancing together too and the closest I got was to create a list of all my turtle objects and then iterate over that list.

Admittedly this gets them to move one at a time but they won't make a second move until all have made the first.

I've written a full program here for you merely to demonstrate how it looks. If it's not what you are looking for I do apologise, but at least its a possible solution to another problem for someone else.

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

Try this code:

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

To make all the turtles move at the same time(if I'm understanding the question correctly) you need to use the tracer() method.

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

Because turtle updates the screen every time any turtle moves, tracer is how you get them to all move at the same time. Usually turtle.tracer(0, 20) should be fine or turtle.tracer(1, 20).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top