...

Turtle Race

In this class, students will learn how to create a Turtle race game. Through this project, students will have the opportunity understand how to create a reusable function to create a racer and use it in multiple occasions.
The lesson will cover the following topics:


  •     Introduction to the Turtle graphics library
  •     Understanding the For loop
  •     User of Python functions
Step 1: Let's start coding
  • In your logged-in trinket account click on New Trinket -> Python

  • Or Open the blank Python template trinket: Open New.
  • Next
    Step 2: Turtle Race Game

    Create a window

    The first step is to import the Turtle module. Place import statements at the top of your code. Next, define the screen and specify its dimension and set the background colour. Type the code as follows:

    Draw the Race Stripes

    We will use a for loop to draw lines. We'll start at the initial position x and y

    Create a first Racer

    We need to select a colour and position for the first racer and set it to the race line.

    Add Second Racer

    The same way we add another racer, we only change the colour, position and the name

    Make the race happen

    To start the race we will create a for loop where we line up all the racers. All the racers will need to move forward, so we will pick the random number of pixels each racser can move forward using randint command.

    Let's improve our game adding a function to create a racer

    We will create a function which will simplify adding a new racer, this function will have following parameters:

  • col - colour of the racer
  • name - name of the racer
  • position - initial position we need to place the racer
  • Add new racers to the game

    After we've added a function to create a new racer it is so easy to add new racers to the game. We just need add 2 lines of code to add new racer
    We will add 3 more racers, r4, r5, r6

    Prev Next
    Step 3: The complete code
    Step 4: Save your work

  • If you don't have a Trinket account, click the down arrow and then click Link. This will give you a link that you can save and come back to later.
  • If you have a Trinket account, you can click Remix to save your own copy of the trinket.
  • Prev Next
    Complete and Share

    Submit your final project



    Copy your Trinket Url

  • Click Share->Link
  • Copy the link to share your code
  • Paste link into the field
  • Prev