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 are going to create your own class, which will be a blueprint for the rooms in your text-based adventure game.
Open a new Python file.
Save it as room.py.
Create a class and give it a name like this:
class Room():
Your class is called Room because it will represent the concept of a room. I have deliberately given the class a name starting with a capital letter, as this helps to distinguish between class names and variables.
You are now going to add a constructor to your class. This is a special method to tell Python how to create an object of this class, and it is always called __init__ with a double underscore on each side of ‘init’.
Take extra care to get this special method name right, or your constructor will not work!
Complete the following instructions in your room.py file:
Move to the next empty line.
If your editor has not done this for you, indent your cursor by pressing the tab key to tell Python that the code you are about to write is part of the Room class.
Add the code below to define the constructor method:
def __init__(self):
Here, ‘init’ stands for ‘initialise’, as a constructor initalises (that is, creates) an object.
Now you are going to add attributes for your room to the constructor. The room should have a name — for example, it might be a kitchen, a bathroom, or a cellar. You are also going to store a description of the room to provide some atmosphere — the cellar could be dark and dusty, for instance.
Add these attributes to your constructor method like this:
def __init__(self):
self.name = None
self.description = None
Always refer to attributes within the object in the format self.name_of_attribute to tell Python that you are referring to a piece of data within the object; self means ‘this object’. Setting the attribute values to None means that they will start off with no value.
Sometimes you will want to allow people to set the values of these parameters when they use your class to create an object.
Add a parameter to the constructor called room_name by altering the existing code like this:
def __init__(self, room_name):
This means that when you create an object, you must provide a room name.
Change the code inside the constructor to tell Python to set the value of the attribute self.name to the room_name provided, by altering the existing code like this:
def __init__(self, room_name):
self.name = room_name
Your Room class should now look like this:
Test your code out by creating an object of the Room class you have written.
Create a new Python file called main.py and save it in the same folder as the file room.py.
In the main.py file, add the following line of code:
from room import Room
This command looks for a file in the same folder called room.py, and looks inside that file for a class called Room (note the upper case R); this is the class you just wrote. The Room class is then made available for use inside the main.py file. (If you did not save your room code as room.py, this will not work; in this case, you will need to rename the file.)
Now instantiate (create) a Room object by executing the code in your main.py file.
Add the following code in the next empty line in your main.py file:
kitchen = Room("kitchen")
This stores the object in a variable called ‘kitchen’ so that you can refer to it later.
When you save and run your program, nothing will happen. Your Room object is being created, but you can’t see it or interact with it, because you didn’t write any methods when you created the Room class. You will move onto this later in the course.
Your Room class has two attributes: a name and a description. Can you think of any other attributes a room in our game might have? Why do you think that attribute would be useful? Share your thoughts in the comments section.