Category: 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