Write a program using an object
Start to create your object-oriented text-based adventure game
Extend other people’s classes
Create an enemy in the room
Recap Week 3
Extending your knowledge of OOP
Finish your game
End of the course
In this step you will be writing a program using objects, using Python’s turtle module to create a turtle race.
Create a new Python program and save it as turtlerace.py. (Note: Please do NOT call the file turtle.py).
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.
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.
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.
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()
Run your program. What happens?
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.
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.
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.
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.
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.