Want to keep learning?

This content is taken from the Raspberry Pi Foundation & National Centre for Computing Education's online course, Object-oriented Programming in Python: Create Your Own Adventure Game. Join the course to learn more.

Skip to 0 minutes and 2 seconds Now it’s time for you to have a go at writing a program using objects. You’re going to create a turtle race using the turtle object from the Python turtle module. So first thing, I’m just going to create a new program, and I’m going to save my new program as turtlerace.

Skip to 0 minutes and 25 seconds Now, I need to import the Turtle class from the turtle module. So I’m going to say from turtle import Turtle.

Skip to 0 minutes and 37 seconds Note how the name of the module is in lowercase, but the name of the class is capitalised. It’s typical to capitalise class names. Now I’ve imported the Turtle class I can create a turtle object. I’m going to create a race of people that I work with. So the first turtle I’m going to create is going to be called laura. Now, I’ve created my laura turtle, I can set some attributes. So attributes define what an object looks like or perhaps how it behaves. So the first thing I’m going to say is I want the colour of laura to be red.

Skip to 1 minute and 22 seconds And as this is a turtle race, what I’m also going to do is actually set the shape to be an actual turtle.

Skip to 1 minute and 33 seconds Once I’ve set some attributes of my objects, I can then use some methods. So methods tell an object how to do something or how to behave. So I’m going to say laura, and I’m going to say penup. So I don’t want to draw just yet, so I’m going to put the pen up. And then what I’m going to do is I’m going to tell the laura object to go to a position on the screen. In which case, this is going to be minus 160, 100. And then, finally, I’m going to tell the laura object to put the pen down. So she’s ready to start drawing. Now, I can actually run my program at this time and see what happens.

Skip to 2 minutes and 11 seconds So let’s run it. So if I click Run, well, our turtle window appears, and, very quickly, a red turtle appeared– this is laura– and moved to this position. Let’s just run that again so we can see what happened. And again, our turtle appeared and moved from the middle of the screen to this position here, which is at minus 160, 100. Now, I can create as many turtle objects as I like. I’m going to have a race between people I work with, so I’m going to create three more turtle objects.

Skip to 2 minutes and 42 seconds The next one I’m going to create is rik and I’m going to create another turtle object of rik and I’m going to create two more which are going to be lauren and carrieanne . Now I’ve created my new turtle objects why I need to do is set up the attributes of those new turtles. So the first one I’m going to do is I’m going to set up rik. So I’m going to make the colour of rik green, so he’s different to the colour of laura. I still want him to be a turtle shape. I’m going to put the pen up again. So rik’s pen is going to go up.

Skip to 3 minutes and 15 seconds I’m going to move him to a different position– so to be underneath the laura object, and then to, again, put the pen down. And I’m going to do the same thing with lauren and the carrieanne turtles, but give them different colours so I can distinguish between them. And again, let’s run our program and see what happens. So we click run, and 1, 2, 3, 4 turtles appear. They’ve all got different colours. They’re all different instances of that turtle class. So we’ve got laura, rik, lauren and carrieanne. So now, to make our turtles race, what I’m going to do is I’m going to tell each one of these turtle objects to move forward a random distance.

Skip to 3 minutes and 56 seconds So in order to make them move forward a random distance, I’m going to say from the random module import randint.

Skip to 4 minutes and 7 seconds And I’m going to create a loop. so I’m going to say for movement in range 100– so I want to run this code 100 times. I’m going to say laura, I want you to move forward, so I’m going to use the forward method, And I want want to move forward a random distance– a random integer between 1 and 5. And then I’m going to do the same thing for the rik, lauren, and the carrieanne turtles. So let’s see that happen. So I, I click Run, my four turtles appear, and they all start moving forward at random distances between 1 and 5 until they reach the end. And it looks like red won, which was laura.

Skip to 4 minutes and 52 seconds Looks like the laura object won the race. Now, it’s time for you to have a go. Experiment with this program, change some of the attributes, maybe create some new turtle objects. And then upload your code to maybe Pastebin or Trinket and share and link to your program in the comments below. If you get stuck, be sure to ask questions, and help each other out. Good luck.

Turtle race

In this step you will be writing a program using objects, using Python’s turtle module to create a turtle race.

animation of the Python turtle race program, 4 "turtles" are shown drawing lines across the screen at different speeds

  1. Create a new Python program and save it as turtlerace.py.

  2. You need to ask Python to import the Turtle class, which is like a blueprint for making a turtle. You will look at what a class is in more detail later on in the course; for now, use this code:

    from turtle import Turtle
    

    Tip: It is important you don’t name your file the same as an import, e.g. turtle.py, otherwise the code will try to import itself and will not work.

  3. Create an instance of a Turtle object. I’m going to name my Turtle object ‘laura’. You can give your turtle whatever name you like.

    laura = Turtle()
    

    As the name of the Turtle object is a variable name, it must start with a letter and it cannot contain any spaces. You are creating a variable in exactly the same way as you usually do, except that the data type of the variable is not an integer or a string, but a Turtle!

    Did you notice that a capital ‘T’ is used when referring to the Turtle class? This is because class names usually start with a capital so that they are easily distinguishable from variable names.

    Each Turtle object is a different instance and will need a different name, so that when you give instructions, you can be specific about which object you are giving the instructions to.

  4. Tell your Turtle object what it should look like. Inside the object are attributes, which are pieces of data we can define. The Turtle object has attributes for colour and shape; you can use the color and shape methods to customise those attributes:

    laura.color('red')
    laura.shape('turtle')
    

    You can also tell the Turtle object what to do by calling other methods.

  5. Use the code below to instruct the turtle to stop drawing with penup(), then to move to a location with goto(), and finally to get ready to draw a line with pendown().

    laura.penup()
    laura.goto(-160, 100)
    laura.pendown()
    
  6. Run your program. What happens?

    The Python turtle window, 1 red turtle is displayed on the screen

    Create three more instances of a Turtle object, each with a different name.

    rik = Turtle()
    lauren = Turtle()
    carrieanne = Turtle()
    

    If you send all the turtles to the same starting point, they will all be on top of each other.

  7. Tell one new turtle to goto(-160, 70), one to goto(-160, 40), and one to goto(-160, 10), for example:

    rik.penup()
    rik.goto(-160, 70)
    rik.pendown()
    

    You can also set a different colour for each turtle if you like, using the .color method as before.

  8. Save and run your code to check that each of your turtles positions itself correctly, ready to start the race!

    You now need to make the Turtle objects race. Each turtle will move forward by a random number of pixels.

  9. After the code to create your four Turtle objects, add this code, replacing the names (laura, rik, etc.) with the names of your own turtle objects:

    from random import randint
    
    for movement in range(100):
        laura.forward(randint(1,5))
        rik.forward(randint(1,5))
        lauren.forward(randint(1,5))
        carrieanne.forward(randint(1,5))
    

    Just as when you used methods to tell the turtle to penup(), pendown(), and goto(), with this code you are using the forward() method to ask it to move forward a random distance between one and five units.

    Note: The randint function from the random module generates random integers.

  10. Save and run your code and see which turtle wins! The result should be different each time you run the code.

Depending on your IDE, you might find that your code executes and then closes the window before you have had a chance to see the output. You can add this line to the end of your files to prevent this from happening:

~~~python
input("Press Enter to close")
~~~

Experiment with the turtle race program, change some attributes, and perhaps create some new turtle objects.

Copy your program onto Pastebin.com, create a paste, and share the link in the comments.

There is also a Code Club version of the turtle race project that you can try yourself, or if you are an educator, try with your learners.

Share this video:

This video is from the free online course:

Object-oriented Programming in Python: Create Your Own Adventure Game

Raspberry Pi Foundation