Skip to 0 minutes and 1 secondLet's get started with writing our own class, which would be a general blueprint for a room in our text adventure game. Begin by opening a new Python file and save it as Room.py. We create a class and give it a name like this. We have deliberately given the class a name starting with a capital letter because this helps us to distinguish between class names and variables. Move to the next line and indent your cursor by pressing the Tab key. Add code to define the constructor method, which is always called init with a double underscore either side of the word init it like this. Now let's add the attributes for our room. Perhaps the room should have a name, for example.

Skip to 0 minutes and 46 secondsWe could also store a description of the room to provide some atmosphere. Add these attributes to your constructor method like this. Setting the attribute values to none means that they will start off with no value. Sometimes we might want to allow people to set the values of these parameters when they create the object. Let's add a parameter to the constructor called room_name. Now change the code inside the constructor to tell Python to set the value of the attribute self.name to the room_name provided.

Creating a constructor and defining attributes

Let’s get started with writing our own class, which will be a blueprint for the rooms in our text-based adventure game. Begin by opening a new Python file and save it as room.py. If you are using Trinket, you can click the + symbol in the editor to create a new file.

We create a class and give it a name like this:

class Room():

Our class is called Room since it will represent the concept of a room. We have deliberately given the class a name starting with a capital letter, because this helps us to distinguish between class names and variables.

Move to the next 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.

Now we will add a constructor to our 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!

Code to define the constructor method looks like this:

def __init__(self):

‘init’ stands for ‘initialise’, since a constructor initalises – i.e. creates – an object.

Now let’s add attributes for our room to the constructor. Perhaps the room should have a name – for example, it might be a kitchen, a bathroom, or a cellar. We could also 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

We always refer to attributes within the object in the format self.name_of_attribute to tell Python that we 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 we want to allow people to set the values of these parameters when they use our class to create an object. Let’s 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 we create an object, we must provide a room name. Now, change the code inside the constructor to tell Python to set the value of the attribute self.name to the room_name provided.

def __init__(self, room_name):
    self.name = room_name

Share this video:

This video is from the free online course:

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

Raspberry Pi Foundation