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 an example solution I made when I completed the challenges in the previous step.
If your solution is different, that is absolutely fine. There is no right or wrong way to complete the challenges.
Here is an example of how the finished game might look.
Add the Item class you wrote in week two to your game. Follow the same process as for the Character class: add an attribute to Room so that it can store an Item, and then create the Item and assign it to the Room. Inside the game loop, check whether there is an Item in the room, and if so, describe it.
Example solution
I added an item attribute to the Room class in room.py.
class Room():
def __init__(self, room_name):
...
self.item = None
def get_item(self):
return self.item
def set_item(self, item_name):
self.item = item_name
I added a describe() method to the Item class so that I could display what the item was.
def describe(self):
print("The [" + self.name + "] is here - " + self.description)
I created some items and added them to rooms when setting up the game.
cheese = Item("cheese")
cheese.set_description("A large and smelly block of cheese")
ballroom.set_item(cheese)
book = Item("book")
book.set_description("A really good book entitled 'Knitting for Dummies'")
dining_hall.set_item(book)
In the main game loop, I got the item from the room and used the describe method to display it:
while dead == False:
...
item = current_room.get_item()
if item is not None:
item.describe()
Add a backpack to allow the player to store items. This could be a list in the main part of the program, or a separate class:
backpack = []
When a player enters a room that contains an Item, the command ‘take’ should put the name of the current room’s Item into the backpack, and also set the Item attribute of the Room to None.
Example solution
I created a list to hold the items in my backpack.
backpack = []
I added a new command, take, to the game, which would remove the item from the room and add it to my backpack list.
elif command == "take":
if item is not None:
print("You put the " + item.get_name() + " in your backpack")
backpack.append(item.get_name())
current_room.set_item(None)
Change your game so that when the player chooses an item to use in a fight, the game checks whether the player actually has an item with that name in their backpack.
Example solution
I added an if statement to check that I had the item in the backpack list before using it to fight with:
elif command == "fight":
...
# Do I have this item?
if fight_with in backpack:
...
else:
print("You don't have a " + fight_with)
If you want your game to last a bit longer, you can use a class variable to allow the player to win the game only after they have defeated a specific number of enemies, rather than winning it the instant they defeat a single enemy.
Example solution
I added an enemies_to_defeat class variable to the Enemy class, and increment the value each time an Enemy object was created:
class Enemy(Character):
enemies_to_defeat = 0
def __init__(self, char_name, char_description):
...
Enemy.enemies_to_defeat = Enemy.enemies_to_defeat + 1
If an enemy was defeated, I reduced the value of enemies_to_defeat by 1:
class Enemy(Character):
...
def fight(self, combat_item):
if combat_item == self.weakness:
...
Enemy.enemies_to_defeat = Enemy.enemies_to_defeat - 1
return True
In the main game loop, I checked to see if I had defeated all the enemies before ending the game:
elif command == "fight":
...
if inhabitant.fight(fight_with) == True:
...
if Enemy.enemies_to_defeat == 0:
print("Congratulations, you have vanquished the enemy horde!")
dead = True
When dead is set to True, the while loop exits and the game finishes.
Did you approach some of the challenges in a different way? Share your thoughts in the comments section.