Skip main navigation

New offer! Get 30% off your first 2 months of Unlimited Monthly. Start your subscription for just £29.99 £19.99. New subscribers only. T&Cs apply

Find out more

Adding new attributes and methods

Adding new attributes and methods
0.5
Let’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.
46.9
If 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.
58.8
Otherwise, print out a message saying that the player lost the fight and return false.
67
Go 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.
90.1
Save and run your code. You will see this outcome of the fight if you choose to fight him with a melon.
98.9
Run the code again. But this time, type in cheese as the item you’ll fight with. This time, you should do a bit better.

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

  • 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, combat_item):
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
This article is from the free online

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

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now