Skip to 0 minutes and 1 secondLet's have a go at writing a program using objects. For this activity, we will use Python's Turtle module, which should already have been created when you instal Python. Almost all of the coding tasks in this course can be completed using Trinket if you don't have Python installed. First, we need to ask Python to import the turtle class, which is like a blueprint for making a turtle. Next, we will create a turtle object. I'm going to name my turtle object Laura, but you can call yours whatever you like. Now, let's tell our turtle object what it should look like. We can also tell the object what it should do by calling some other methods.

Skip to 0 minutes and 44 secondsSince the name of the turtle object is just a variable name, it must start with a letter, and it cannot contain any spaces. In fact, we are just creating a variable in exactly the same way as usual, except the data type of the variable is not an integer or a string, but a turtle.

Skip to 1 minute and 8 secondsNow it's your turn. Create three more turtle objects, making sure to give different names.

Skip to 1 minute and 25 secondsWe don't want to send all of the turtles to the same location, so tell one new turtle to go to minus 160, 70; one to go to minus 160, 40; and one to go to minus 160, 10. Make them different colours if you like.

Skip to 1 minute and 56 secondsNow, let's add some code to make the turtle objects race. Underneath your four turtle objects, add this code.

Skip to 2 minutes and 18 secondsYou will also need to go back to the top of your program and add this line of code so that we can generate random integers.

Skip to 2 minutes and 32 secondsSave and run your code and see which turtle wins.

Turtle race

turtle race

Let’s have a go at writing a program using objects. We will use Python’s turtle module, which should be included when you install Python. You can complete almost all of the activities in this course in a web browser by creating a free Trinket account. Open the Trinket page in a new tab so you can continue to work through the course at the same time.

Alternatively, you might want to install Python 3 on your computer and use the IDLE code editor which comes with the installation.

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

from turtle import Turtle

If you are using Trinket, create a new Python trinket – these are free to create:

1.6 which trinket

You can type this code straight into main.py in your new Trinket. If you are using a text editor, save your code as turtle_race.py - or at least ensure you do not call your file turtle.py otherwise the code will try to import itself and will not work.

Next, we will create an instance of a Turtle object. I’m going to name my Turtle object ‘laura’ because I’m going to get my turtle to race against some of the Raspberry Pi team. You can give your turtle whatever name you like.

laura = Turtle()

Since the name of the Turtle object is a variable name, it must start with a letter and it cannot contain any spaces. In fact, we are creating a variable in exactly the same way as we usually do, except that the data type of the variable is not an integer or a string, but a Turtle! We need to give each Turtle object a different name, so that, when we give instructions, we can be specific about which object we are giving the instructions to.

Now, let’s tell our 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 color and shape, so let’s customise those attributes:

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

We can also tell our Turtle object what to do by calling other methods. With the code below, we are instructing the object 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()

Save your code and run it. What happens?

Now it’s your turn. Create three more instances of a Turtle object, each with a different name. We don’t want to send all of the turtles to the same starting point, so tell one new turtle to goto(-160, 70), one to goto(-160, 40) and one to goto(-160, 10). You can set a different colour for each turtle if you like.

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

Now let’s add some code to make the Turtle objects race. Below your four turtle objects, add this code, replacing the names (laura, rik, etc.) with the names of your own turtle objects:

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))

You will also need to go back to the top of your program and add this line of code so that we can generate random integers:

from random import randint

Just as when we used methods to tell the turtle to penup(), pendown(), and goto(), with this code we are calling a method on each turtle object: we are asking it to move forward() a random distance between one and five units.

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

A similar version of the code from this section of the course is available as a Code Club project, in case you would like to make it 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