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
Your game will have multiple characters to interact with. For example, I might create two characters:
Dave, the smelly zombie, and
Catrina, the friendly skeleton.
I might want to fight with enemies such as Dave, but I wouldn’t want to fight with friendly characters such as Catrina.
Inheritance is one of the key elements of OOP; it allows you to base a new class on an existing one. The new class automatically inherits all the methods and attributes of the existing class, removing the need to include the same methods and attributes in the new class.
You will make a subclass of Character called Enemy. It will use the Character class as a basis, but add some more functionality specific to enemies.
In the existing character.py file, create a new Enemy class below the Character class.
class Character():
...
# Fight with this character
def fight(self, combat_item):
print(self.name + " doesn't want to fight with you")
return True
# Add your code here (first line unindented)
class Enemy(Character):
The name of this new class is Enemy. Putting Character inside the brackets tells Python that the Enemy class will inherit all of the methods from Character.
Character is called the superclass of Enemy, and Enemy is a subclass of Character.
2. Add code in the next line to create a constructor for an enemy object; this looks identical to the constructor of Character.
def __init__(self, char_name, char_description):
Character is the superclass of Enemy, so you need to ensure that Enemy inherits from Character. To do this, you have to call the superclass constructor method inside the constructor of the Enemy class.
3. Add the code below, in the next line after the Enemy constructor.
super().__init__(char_name, char_description)
This line of code means ‘To make an Enemy, first make a Character object and then I will customise it.’
When the __init__() function is created in a subclass, the subclass will no longer inherit the superclass’s __init__() function.
The super() function allows you to call functions in the superclass. Calling super().__init__() from the Enemy class will call the constructor of the Character superclass. The Character constructor sets up the attributes for the class.
Calling the previously built methods with super() saves you from needing to rewrite those methods in your subclass.
Save this code. Next, you will prove that, with the help of these two lines of code, the Enemy class has inherited all of the properties of the Character class.
4. Go to your character_test.py program and change it so that all references to the Character class become references to the Enemy class:
from character import Enemy
dave = Enemy("Dave", "A smelly zombie")
5. Run the code and test it. You should be able to see the description of Dave and talk to him, just as you did before.
An object of a subclass is also considered to be an object of its superclass. In simple terms, a class that inherits from a <superclass> is called a <subclass>.
So in your example, an Enemy is a Character.
If a function requires a Character object as a parameter, you can give the function an Enemy object instead and the code will still work. However, this does not work the other way around: if the function requires an Enemy object, you cannot give it a Character object, because Enemy is a more specialised version of Character.
In the next step, you will modify the Enemy class so that it behaves differently to the Character class. This concept is called polymorphism.
How do you think an enemy object will behave differently to a character class in your game? Share your thoughts in the comments section.