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
current_room = kitchen
while True:
print("\n")
current_room.get_details()
command = input("> ")
current_room = current_room.move(command)
Now that you have characters in some rooms, you need to expand this loop so that it performs different actions depending on the command that is given. Here is some code to start you off:
command = input("> ")
# Check whether a direction was typed
if command in ["north", "south", "east", "west"]:
current_room = current_room.move(command)
elif command == "talk":
# Add code here
Here are some tasks for you to complete. If you would like to check how I solved them, example solutions are shown in the next step.
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)
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.
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?
Tip: To add a non-enemy character to your game you will need to import the Character class from the character module, using:
from character import Character
For a bigger challenge, extend the Character class in a different way. You could create a Friend subclass with some custom methods and attributes. For example, you could hug a 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.