In a game, we may want to have the user have to select which item they would like to fight the enemy with. For this functionality we need to:
display the inventory when a fight is initiated
select which item the user would like to fight with
change the fight command
Display the backpack
Add a searched attribute to the Room constructor
In room.py
class Room():
number_of_rooms = 0
def __init__(self, room_name):
self.name = room_name
self.description = None
self.linked_rooms = {}
self.character = None
self.item = None
# add the searched flag attribute
self.searched = False
Room.number_of_rooms = Room.number_of_rooms + 1
2. Display items only if searched
The goal is to have the room only display the item if it has been searched
in main.py
.
.
.
while dead == False:
print("\n")
current_room.get_details()
inhabitant = current_room.get_character()
if inhabitant is not None:
inhabitant.describe()
# Check if the room has been searched
if current_room.searched:
item = current_room.get_item()
if item is not None:
item.describe()
command = input("> ")
.
.
3. Add a found_text attribute to Item, and setters and getters
Add flair to the game by adding text when you find an item in your search
in item.py
class Item():
def __init__(self, item_name):
self.name = item_name
self.description = None
# add the found_text attribute
self.found_text = "You find " + self.name
def set_found_text(self, text):
self.found_text = text
def get_name(self):
return self.name
.
.
4. Create a command to search the rooms
Now that we have changed the display,, we need to change the attribute in game.
In main.py
.
.
.
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)
# Add a command to change the searched attribute
elif command == "search":
if current_room.searched:
print("\nYou find nothing more here.")
else:
current_room.searched = True
if current_room.item:
print(current_room.item.found_text)
else:
print("\nThere is nothing to find here.")
.
.
.