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 the previous step, you created a class called Room and a constructor method with attributes. You also instantiated an object from the Room class. In this step, you are going to learn more about class and objects.
You can think of class as being a blueprint or a design of a house; it contains details about the make-up of the house such as the doors, windows, floors, and roof. When the design is built, a house is created; in this analogy, the built house is the object. You can build many houses using the same design, and you can also create many objects from a class.
An object is also called an instance of a class, and the process of creating this is instantiation.
In the main part of your game, the line below creates a new Room object with an attribute name set to kitchen. Then the object is assigned to a variable called kitchen:
kitchen = Room("kitchen")
To fully understand how this works, you need to know how the ‘self’ parameter works in the constructor. The self parameter automatically receives a reference to the object invoking the method. By using self, a method can invoke the object and access the attributes and methods of that object. In the previous constructor method, the parameter self automatically receives a reference to a new Room object while the parameter name receives kitchen. The line below creates the attribute:
self.name = room_name
Now you are going to create objects for the ballroom and dining room so that you have a total of three rooms in your game.
Open your main.py file.
Add the following line of code to create a ballroom:
ballroom = Room("ballroom")
Add another line of code to create a dining_hall room.