Contact FutureLearn for Support
Skip main navigation
We use cookies to give you a better experience, if that’s ok you can close this message and carry on browsing. For more info read our cookies policy.
We use cookies to give you a better experience. Carry on browsing if you're happy with this, or read our cookies policy for more information.

Skip to 0 minutes and 1 secondThis week, we're going to extended an existing class. But it's also perfectly normal to write your own classes and then extend them. We're just going to test this class out first. So create a new Trinket. If you're using a text editor, create a new file called character test . py. Copy the code from this link and save it as character . py. If you're using a text editor, make sure this file is in the same folder as all of your previous code.

Skip to 0 minutes and 29 secondsThis is the character class, which we've written for you. You can see from looking at the code that to create a character object, we must provide two arguments to the constructor, the character's name, and a description of the character. Go back to your test file. Inside this file, create a character object.

Skip to 0 minutes and 55 secondsCall the method describe on the object you created to show the character's description on the screen. Save and run your programme, and you should see the description of the character you just created.

Extending an existing class

Dave

This week we’re going to extend an existing class. This means we will create another class which uses the functionality of the existing class, but also adds to or overwrites some of it. It’s also perfectly normal to write your own classes and then extend them.

If you are using a text editor, copy the code from this script, and paste and save it as a new file named character.py in the folder containing all your earlier code. If you are using Trinket, start a new trinket and create a character.py file to store the code, but keep the main.py file as well.

The code is the Character class which we have written for you – if you have a look at it, you should recognize some familiar things.

You’ll see that to create a Character object, the constructor needs two parameters – the character’s name, and a description of the character:

def __init__(self, char_name, char_description):
    self.name = char_name
    self.description = char_description
    self.conversation = None

If you are using a text editor, create a new Python file and save it as character_test.py in the folder with your other code. If you are using Trinket, put this code inside the main.py file in the new trinket. Inside this file, create a character object.

from character import Character
dave = Character("Dave", "A smelly zombie")

Call the method describe on the object you created to show the character’s description on the screen.

dave.describe()

Save and run your program. You should see the description of the character you just created:

Dave is here!
A smelly zombie

Challenge

challengeicon

  • Examine the Character class inside character.py to find the name of the method which sets the conversation attribute
  • Add code to main.py to call this method and give Dave a line of dialogue
  • Add code to main.py to call a different method which talks to Dave

So far, so good – we can use this code as the basis for our characters. However, not every character in the game will have the same characteristics. Some will be friends and some will be enemies, and they may behave differently.

Share this video:

This video is from the free online course:

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

Raspberry Pi Foundation