How to move multiple turtles at the same time in python?
<p>Hi I have an assignment which is asked to set two turtles in a race track(same size but separate track).  I can able to make them move but the second one move only when the first one moved a half of the track.  I'm not sure how to make the turtles move at the same time.  Here is my code, please help me if you have any idea about it.  Thank you!</p>

<pre><code>import turtle
import random
import time


wn = turtle.Screen()
wn.bgcolor("lightgreen")

t = turtle.Turtle()
t.shape('turtle')
t.color('red')

t2 = turtle.Turtle()
t2.shape('turtle')
t2.color('blue')

#user input function

p = float(input('please insert the perimeter:'))

#set the track
def drawTrack(p,r):
    shortside = (p/2.0)/(r+1)
    longside = r*shortside
    turtle.setup((shortside*2)+60, longside +40)
    t.penup()
    t2.penup()
    t.setposition(-shortside-10, -longside/2)
    t2.setposition(10, -longside/2)   
    for i in range (2):
        #first track
        t.speed(1)
        t.pendown()
        t.forward(shortside)
        t.left(90)
        t.forward(longside)
        t.left(90)

        #second track
        t2.speed(1)
        t2.pendown()
        t2.forward(shortside)
        t2.left(90)
        t2.forward(longside)
        t2.left(90) 

drawTrack(p,2)

wn.exitonclick()
</code></pre>

<p>There are a couple of ways you can go about this.</p>

<p>One approach to use the <code>screen.ontimer()</code> event (see the turtle documentation).  This approach is nice as you can adjust the turtles to actual clock time and this can run within the turtle event loop so that other events can also take place (like <code>exitonclick()</code>).</p>

<p>The approach I used below is to break up turtle motion into tiny steps inside a Python <em>generator</em> which yields after every bit of motion.  This allows us to alternate between the turtles.  The race takes place before the turtle event loop so <code>exitonclick()</code> is invalid until the race is over.</p>

<p>To provide a differential in speed, I've used the turtle drawing speed as part of the motion calculation so if you say <code>turtle1.speed("fast")</code> it will move fast compared to a <code>turtle2.speed("slow")</code>.  There are other ways to go about this using random and/or varying speeds.</p>

<p>I've changed the prompt for the perimeter to be a dialog box and made various style tweaks:</p>

<pre><code>from turtle import Turtle, Screen

screen = Screen()
screen.bgcolor("lightgreen")

turtle1 = Turtle(shape='turtle')
turtle1.color('red')
turtle1.speed("slow")  # = 3
turtle1.penup()

turtle2 = Turtle(shape='turtle')
turtle2.color('blue')
turtle2.speed(4)  # "slow" (3) &lt; 4 &lt; "normal" (6)
turtle2.penup()

# user input function

perimeter = screen.numinput("Track Perimeter", "Please enter the perimeter:", default=2000, minval=500, maxval=3000)

def full_track_crawl(turtle, shortside, longside):
    speed = turtle.speed()
    turtle.pendown()

    for j in range (2):
        for i in range(0, int(shortside), speed):
            turtle.forward(speed)
            yield(0)
        turtle.left(90)
        for i in range(0, int(longside), speed):
            turtle.forward(speed)
            yield(0)
        turtle.left(90)

    turtle.penup()

# set the track
def drawTrack(perimeter, ratio):
    shortside = (perimeter / 2.0) / (ratio + 1)
    longside = ratio * shortside

    screen.setup(shortside * 2 + 60, longside + 40)

    turtle1.setposition(-shortside - 10, -longside / 2)
    turtle2.setposition(10, -longside / 2)   

    generator1 = full_track_crawl(turtle1, shortside, longside)
    generator2 = full_track_crawl(turtle2, shortside, longside)

    while (next(generator1, 1) + next(generator2, 1) &lt; 2):
        pass

drawTrack(perimeter, 2)

screen.exitonclick()
</code></pre>

<p>Happy racing!</p>

python turtle-graphics