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
So that you are able to interact with the room object, you need to create some getters and setters. These are methods that get and set the values of the object’s attributes.
For every method within a class, you first need to specify the self parameter — the object itself — followed by any data to pass in, just as you did with the constructor. You will need to add these methods inside the room.py file, after the constructor method.
class Room():
def __init__(self, room_name):
self.name = room_name
self.description= None
# Add your code here (make sure it is indented)
Here is a method to set the description of the room:
def set_description(self, room_description):
self.description = room_description
Here is a method to get the description of the room:
def get_description(self):
return self.description
Use the set_description method you created earlier to give your kitchen object a description. Go back to your main.py file and add this line of code below the line where you instantiate the object:
kitchen.set_description("A dank and dirty room buzzing with flies")
Did you notice that the method you wrote had two arguments (self and room_description), but in the code above I only had to provide the room description? The self argument is always given first when you write a method, but when you use a method, you do not have to give this argument to it. This is because it is automatically passed to the method when you use Python.
You can also use the get_description method you created earlier. Add this line at the bottom of your main.py file:
kitchen.get_description()
Save and run your program. You will probably be expecting to see the description of your kitchen room on the screen. However, when you run the program, you will not see anything.
This is because your get_description() method returns the description, rather than printing it. This is useful if you want to do something else with the description, such as concatenate it with some other text, but if you want to display the description, you need to use the print command.
Instead of amending the get_description() method, you will use a different approach. Go back to room.py and add an additional method called describe(), which will print out the object’s description when it is called.
def describe(self):
print( self.description )
In main.py, delete the kitchen.get.description() and add kitchen.describe() on the same line, then run it to see the description appear.
Add a getter method called get_name and a setter method called set_name to your class, to allow someone to get and set the attribute name in the same way.
You can see a solution for this at the end of the next step.
You don’t strictly need getters and setters in Python, because you can access the attributes of the object directly (for example, you could just print( kitchen.description )). Including getters and setters in your class is good programming etiquette, because they are useful tools for ensuring that people are using your code in the way you expect, and for minimising the risk of unexpected errors.
Python also has a more advanced way of specifying getters and setters for attributes using the @property decorator. While this is probably considered more pythonic, I have chosen not to cover this method until later in this course, to keep things simple and avoid teaching a syntax that is specific to Python.