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
In this step, I will show you some examples of my solutions to the challenges in the previous step.
You can look at this code if you get stuck or if you would like to see how I implemented something.
Here is an example of a game where all of the challenges have been attempted.
There are lots of different ways you could extend your game and there is no one right solution, so be creative!
Add some functionality to the game loop so that different commands can be recognised. For example, your program could react to the following user inputs:
‘talk’: talk to the inhabitant of this room (if there is one)
‘fight’: ask what item the player would like to fight with, and then fight the inhabitant of this room (if there is one)
Example solution
I added a new command to ‘talk’ to the inhabitant of the room.
elif command == "talk":
# Talk to the inhabitant - check whether there is one!
if inhabitant is not None:
inhabitant.talk()
I added a command to ‘fight’, which:
Checks to see if there is a character to fight with and that they are an Enemy, using the isinstance function
Asks the player what they want to use to fight with, before checking to see if they won the fight
elif command == "fight":
if inhabitant is not None and isinstance(inhabitant, Enemy):
# Fight with the inhabitant, if there is one
print("What will you fight with?")
fight_with = input()
if inhabitant.fight(fight_with) == True:
# What happens if you win?
print("Hooray, you won the fight!")
current_room.set_character(None)
else:
# What happens if you lose?
print("Oh dear, you lost the fight.")
else:
print("There is no one here to fight with")
If you lose a fight with an enemy, the game should end.
Hint: The fight method you wrote in the Enemy class returns True if you survive, and False if you do not.
Example solution
I created a new variable to keep track of whether the player was still alive.
dead = False
I modified the main while loop in the game to only loop while the player was alive.
while dead == False:
If the player loses a fight, the dead variable will be set to True and the game will end.
elif command == "fight":
...
if inhabitant.fight(fight_with) == True:
...
else:
print("Oh dear, you lost the fight")
print("That's the end of the game")
dead = True
Add an additional character to inhabit a different room in your game. Perhaps you might want to add more methods in the Enemy class in order to be able to interact with enemies in other ways. For example, can you steal from an enemy, bribe an enemy, or send it to sleep?
Example solution
I added a steal method to the Enemy class so that I could take something from them.
class Enemy(Character):
...
def steal(self):
print("You steal from " + self.name)
# How will you decide what this character has to steal?
For a bigger challenge, why not extend the Character class in a different way? Perhaps you could create a Friend subclass with some custom methods and attributes. For example, you could hug the friendly character, or offer them a gift. You can use the built-in Python function isinstance() to check whether an object is an instance of a particular class.
Example solution
I created a new Friend class that inherits from Character so that I could create friendly characters in the game.
class Friend(Character):
def __init__(self, char_name, char_description):
super().__init__(char_name, char_description)
self.feeling = None
def hug(self):
print(self.name + " hugs you back!")
# What other methods could your Friend class have?
I modified the character import to include the Friend class so that I could use it in my game.
from character import Enemy, Friend
I created catrina, a friendly skeleton, and added them to a room.
catrina = Friend("Catrina", "A friendly skeleton")
catrina.set_conversation("Why hello there")
ballroom.set_character(catrina)
Thinking it would be fun, I created a new command so that I could try and hug the inhabitant of a room.
elif command == "hug":
if inhabitant is not None:
if isinstance(inhabitant, Enemy):
print("I wouldn't do that if I were you...")
else:
inhabitant.hug()
else:
print("There is no one here to hug :(")
Share a Pastebin link to your code in the comments section. You can also share your thoughts on the example solutions and how they compare to your solutions.