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 secondLet's add a new attribute to specify the weakness of the enemy character so that we can defeat the enemy in the game. An object of the class character doesn't have an attribute called weakness, so this is a customisation we are adding to the enemy class. On the line after you call a superclass constructor, set the value of the weakness attribute to be initialised as none. Inside the enemy class, we will add a new implementation of the fight method to allow us to fight the enemy. This will override the implementation of fight provided inside character. Start your method like this. Combat item is a string containing the name of an item, for example, sword, or banana.

Skip to 0 minutes and 47 secondsIf the combat item the player used to fight with the enemy is the enemy's weakness, print out a message saying that the player won the fight and returned true.

Skip to 0 minutes and 59 secondsOtherwise, print out a message saying that the player lost the fight and return false.

Skip to 1 minute and 7 secondsGo back to your test file and alter the code. First, add a weakness for Dave by calling the setter method you just wrote. In my game, Dave is weak against cheese because he just loves to eat it. Then call the fight method on Dave choosing an object to fight with.

Skip to 1 minute and 30 secondsSave and run your code. You will see this outcome of the fight if you choose to fight him with a melon.

Skip to 1 minute and 39 secondsRun the code again. But this time, type in cheese as the item you'll fight with. This time, you should do a bit better.

Adding new attributes and methods

daveandcatrinafight

At the moment our Enemy class is functionally identical to the Character class, which is a bit pointless. That’s why we’ll start to add new functionality to customise it.

Let’s add a new attribute to specify the weakness of the enemy character, so that we can defeat the character in-game. An object of the class Character doesn’t have an attribute called weakness – this is a customisation we are adding to the Enemy class.

Each enemy will be vulnerable to an item which can be found in the game. For example, Superman would be vulnerable to Kryptonite. Eventually we will use the Item class you wrote last week to add items to the game, but for now we’ll just represent an item as a string.

Inside your Enemy class, move to the line below the one calling the superclass constructor.

3.5 add code screenshot

Set the value of the weakness attribute to be initialised as None.

self.weakness = None

Challenge

challengeicon

  • Add getter and setter methods to Enemy so that you can add a weakness for an enemy

Fighting Dave

The Character class has a method called fight(). Since we don’t want to fight with non-hostile characters, it simply returns a message that the character “doesn’t want to fight with you”.

def fight(self):
    print(self.name + " doesn't want to fight with you")

Inside the Enemy class, we will add a new implementation of the fight method to allow us to fight an enemy. This will override the implementation of fight() provided inside Character.

Start your method like this:

def fight(self, combat_item):

combat_item is a string containing the name of an item, for example “sword” or “banana”.

If the combat_item the player uses to fight with the enemy is the enemy’s weakness, print out a message saying that the player won the fight, and return True. Otherwise, print out a message saying that the player lost the fight, and return False.

def fight(self, combat_item):
    if combat_item == self.weakness:
        print("You fend " + self.name + " off with the " + combat_item )
        return True
    else:
        print(self.name + " crushes you, puny adventurer")
        return False

Go back to your character_test.py (or main.py on Trinket) file and add some new code at the bottom of the page. First, add a weakness for Dave by calling the setter method you just wrote. In my game, Dave is vulnerable to cheese because he just loves to eat it!

dave.set_weakness("cheese")

Then call the fight() method on Dave, choosing an object to fight with:

print("What will you fight with?")
fight_with = input()
dave.fight(fight_with)

Save and run your code. If you choose to fight Dave with a melon, you will see this outcome of the fight:

Dave is here!
A smelly zombie

[Dave says]: What's up, dude!
What will you fight with?
melon
Dave crushes you, puny adventurer

Run the code again, but this time enter “cheese” as the item you will fight with:

Dave is here!
A smelly zombie
[Dave says]: What's up, dude!
What will you fight with?
cheese
You fend Dave off with the cheese

Share this video:

This video is from the free online course:

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

Raspberry Pi Foundation