Time to organize a race

  1. create many turtles
  2. assign colours
  3. let them race

Set up all variables

What's mypos ?

  1. Type it in the python shell >>>
  2. what do you get?
  3. What will we use these numbers for?

Assign appropriate starting values

Two turtles:

same pattern, different colours

Let's race!

START YOUR ENGINES

But that wasn't a real race!

they all end ex aequo! Can we change that?

Of course we can! Cause we got the power!

First, let's get another library to help us

import random

Then let's change how far they go on each turn

Instead of
t.forward(10)
Use this:
step = random.randint(0,20)
t.forward(step)

that would be enough to see them race, but we might want to change our loop

winner = None
while winner == None:

This is a while loop, not quite the same as a for loop

What are the differences between these two loops?

  1. We don't know right away how many loops we need to do
  2. Make sure you CHANGE the condition inside the loop
  3. A while loop may never stop

Now let's see if we have a winner

after the forward call, check if the turtle has reached the finish line

x,y=t.pos()
if x > 200:
    winner=t
    break

break???

break will get us out of the inner loop.

You might want to kill off the losers too

for t in turtles():
if t != winner:
    t.hideturtle()

And a special celebration effect

for i in range(10):
    for c in mycolors:
        beach.bgcolor(c)

see the race