CoderDojo turtle racing – 4 – countdown timer

1 Comment

To make this race more realistic, lets give our turtle a countdown timer so he knows when to start!

Find the following lines in the bumpy top example.

# start
start = time.time()

And replace them with these lines so that the turtle knows when to start.

# count down to start
for countdown in ["5","4","3","2","1"]:
  scorer.write("Time to start " + countdown,font=f)
  time.sleep(1)
  scorer.clear()
start = time.time()

The “for” command creates a loop in the code which can save a lot of time. All the indented lines after the “for” command are run for each different value of “countdown”. The list [“5″,”4″,”3″,”2″,”1”] is all the different values of countdown.

Once you have that working, you can try replacing [“5″,”4″,”3″,”2″,”1”] with [“ready”,”set”,”go”].

NEXT: Try creating your own track!

Categories: CoderDojo

CoderDojo turtle racing – 3 – bumpy top

1 Comment

Here is a more difficult track. See if you can navigate your turtle all the way around the track without touching the sides of the track.

# Turtle racing track = bumpy top
# Don't edit the lines for turtles "track" or "scorer"
# Edit the lines for turtle "racer"
# between "# ninja start" and "# ninja end"
# See if you can find the fastest way around the track
# Make sure the turtle doesn't touch the sides

import turtle
import time

#turtle which draws track
track = turtle.Turtle()
track.speed(0)
track.color("black")
track.penup()
track.hideturtle()

#inside of track
track.goto(-50,-100)
track.pendown()
track.forward(100)
track.circle(100,90)
track.forward(50)
track.circle(50,180)
track.circle(-50,180)
track.circle(50,180)
track.forward(50)
track.circle(100,90)
track.penup()

#outside of track
track.goto(-50,-150)
track.pendown()
track.forward(100)
track.circle(150,90)
track.forward(50)
track.circle(100,180)
track.right(180)
track.circle(100,180)
track.forward(50)
track.circle(150,90)
track.penup()

# start / finish line
track.color("gray")
track.goto(0,-100)
track.pendown()
track.goto(0,-150)
track.penup()

# turtle which keeps score
scorer = turtle.Turtle()
scorer.color("blue")
scorer.speed(0)
scorer.penup()
scorer.goto(-170,170)
f = ("Arial",15,"normal")

# turtle which races around track
racer = turtle.Turtle()
racer.shape("turtle")
racer.color("blue")
racer.speed(10)

# set up racer at start line
racer.penup()
racer.goto(-20,-125)
racer.pendown()

# start
start = time.time()

# ninja start


# YOUR CODE HERE


# ninja end

#publish score
end = time.time()
scorer.write("Time (seconds) = "+str(end-start),font=f)

NEXT: CoderDojo turtle racing – 4 – countdown timer

Categories: CoderDojo

CoderDojo turtle racing – 2 – oval track with scoring

1 Comment

This follows on from CoderDojo turtle racing – 1 – oval track. Copy your Python code from the first post and run it in this one. This time you will get a score showing how long it took your turtle to get around the track.

# Turtle racing track = oval track with score
# Don't edit the lines for turtle "track"
# Edit the lines for turtle "racer"
# between "# ninja start" and "# ninja end"
# See if you can find the fastest way around the track
# Make sure the turtle doesn't touch the sides

import turtle
import time

# turtle which draws track
track = turtle.Turtle()
track.speed(0)
track.color("black")
track.penup()
track.hideturtle()

# inside of track
track.goto(-50,-100)
track.pendown()
track.forward(100)
track.circle(100,180)
track.forward(100)
track.circle(100,180)
track.penup()

# outside of track
track.goto(-50,-150)
track.pendown()
track.forward(100)
track.circle(150,180)
track.forward(100)
track.circle(150,180)
track.penup()

# start / finish line
track.color("gray")
track.goto(20,-100)
track.pendown()
track.goto(20,-150)
track.penup()

# turtle which keeps score
scorer = turtle.Turtle()
scorer.color("blue")
scorer.speed(0)
scorer.penup()
scorer.goto(-170,170)
f=("Arial",15,"normal")

# turtle which races around track
racer = turtle.Turtle()
racer.shape("turtle")
racer.color("blue")
racer.speed(10)

# set up racer at start line
racer.penup()
racer.goto(0,-125)
racer.pendown()

# start
time.sleep(2)
start = time.time()

# ninja start


# PUT YOUR TURTLE RACER CODE HERE


# ninja end

#publish score
end = time.time()
scorer.write("Time (seconds) = "+str(end-start),font=f)

NEXT: CoderDojo turtle racing – 3 – bumpy top

Categories: CoderDojo

CoderDojo turtle racing – 1 – oval track

1 Comment

This is a fun task to play with Python’s turtle graphics. Below is Python code to draw a race track. See if you can write some Python code between “# ninja start” and “# ninja end” which will make the turtle race all the way around the track without bumping into the side of the track. I have provided the first three lines which gets the turtle half way around the first turn.

The python commands you will need are

racer.forward(100) # move in a straight line 100 pixels
racer.left(45)     # turn 45 degrees to the left
racer.right(45)    # turn 45 degrees to the right

You can change the numbers to whatever values you need to get the turtle all the way around the track.

You can run this python code on your computer or on website such as https://hourofpython.trinket.io .

Here is the python code to run and edit

# Turtle racing track = oval track
# Don't edit the lines for turtle "track"
# Edit the lines for turtle "racer"
# between "# ninja start" and "# ninja end"
# See if you can find the fastest way around the track
# Make sure the turtle doesn't touch the sides

import turtle

# turtle which draws track
track = turtle.Turtle()
track.speed(0)
track.color("black")
track.penup()
track.hideturtle()

# inside of track
track.goto(-50,-100)
track.pendown()
track.forward(100)
track.circle(100,180)
track.forward(100)
track.circle(100,180)
track.penup()

# outside of track
track.goto(-50,-150)
track.pendown()
track.forward(100)
track.circle(150,180)
track.forward(100)
track.circle(150,180)
track.penup()

# turtle which races around track
racer = turtle.Turtle()
racer.shape("turtle")
racer.color("blue")
racer.speed(10)

# set up racer at start line
racer.penup()
racer.goto(0,-125)
racer.pendown()

# ninja start
# python code to race around track
# example code shows first three straight lines
racer.forward(100)
racer.left(45)
racer.forward(100)
racer.left(45)
racer.forward(100)
# ninja end

NEXT: CoderDojo turtle racing – 2 – oval track with scoring

Categories: CoderDojo