Skip to 0 minutes and 1 secondNow you've written a class for creating an enemy, let's add Dave the zombie to our game. Open up the file main game . py from last week if you're using a text editor, or find the Trinket that you were using if you're using Trinket. If you're using Trinket, you'll need to copy across the character class into your main game Trinket as well.

Skip to 0 minutes and 27 secondsImport the enemy class you wrote by adding this line of code at the top. Remember, that in this code, character is the name of the Python file, and enemy is the name of the class you are importing from that file. Underneath the code where you linked the rooms together, paste in your enemy object code, and also include the code you wrote earlier to set Dave's conversation and weakness attributes. We would like to situate Dave inside a room. But to do this, we have to add a new parameter inside the room class in room . py so that a room object knows when it has a character inside it. Edit your room class constructor to add a new field.

Skip to 1 minute and 9 secondsOf course, some rooms will have no characters inside them, in which case, this attribute will stay as none. Add getter and setter methods in the usual way to be able to put a character within a room. This is called aggregation. A room object has a character object inside it. In reality, the room may have a character inside it, or it may be empty. But the important thing is that room now has the capability to contain a character. Let's put Dave in the dining hall using the new setter method you just wrote. Add this code underneath the code where you create Dave. You may be thinking, hang on a second. The room can contain a character, but Dave is an enemy.

Skip to 1 minute and 54 secondsWe are allowed to add an enemy or a character because of polymorphism. An enemy is a character. Now in the main game loop, before you ask the user for a command, check whether there is an enemy in this room using the getter method you just wrote. If so, describe the enemy using the describe method. Save and run your programme. Move from the kitchen to the dining hall, and you should see the description of Dave the zombie appear.

Aggregation – objects within objects

davecheese

We have written a class for creating an enemy, so let’s add Dave the zombie to our game. Open up the file or trinket from last week where you began creating your game. If you are using Trinket, you will need to create a character.py file in your game trinket and copy across the contents of the character.py file from the test trinket we were using in the previous steps.

Add the following line of code at the top of main.py to import your Enemy class.

from character import Enemy

Remember that character is the name of the Python file from which you are importing, and Enemy is the name of the class you are importing from it.

Below the code which links the rooms together, we need to recreate Dave.

3.7 add code screenshot

Make sure you include some code to set Dave’s conversation and weakness attributes.

dave = Enemy("Dave", "A smelly zombie")
dave.set_conversation("Brrlgrh... rgrhl... brains...")
dave.set_weakness("cheese")

We would like to situate Dave inside a room. To do this, we have to add a new parameter inside the Room class in room.py so that a room object knows when it has a character inside it. Edit your Room class constructor to add a new field.

2nd screenshot

self.character = None

A room object now has a character object inside it – this is called aggregation. In practice a room may be empty, in which case this attribute will stay as None. The important thing is that a room now has the capability to contain a character.

Challenge

challengeicon

  • Add getter and setter methods to enable putting a character inside a room

Putting Dave in a room

Let’s put Dave in the dining hall using the new setter method you just wrote. Add this code to main.py, immediately below the code where you created Dave:

dining_hall.set_character(dave)

You may be thinking “Hang on a second, the room can contain a character, but Dave is an enemy!” We are allowed to add an enemy instead of a character because of polymorphism – an enemy is a character.

Now, in the main game loop, before you ask the user for a command, check whether there is an enemy in the room using your getter method.

3rd screenshot

If the answer is yes, describe the enemy using the describe method which was inherited by Enemy from the Character class.

inhabitant = current_room.get_character()
if inhabitant is not None:
    inhabitant.describe()

Save and run your program. Move from the kitchen to the dining hall, and you should see the description of Dave the zombie appear.

Share this video:

This video is from the free online course:

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

Raspberry Pi Foundation