The most obvious thing you are missing is any use of Python's various container types, relying instead on too many individual variables. Two examples spring immediately to mind, using the most common containers: list
and dict
.
Inventory
At the moment, you are using global
variables for your inventory, which is a bad sign. The function for displaying the inventory:
def inventory():
if crowbar == 0:
print('Inventory: empty')
elif crowbar == 1 and mallet == 0:
print('Inventory: crowbar')
elif crowbar == 1 and mallet == 1 and key == 0:
print('Inventory: crowbar, mallet')
else:
print('Inventory: crowbar, mallet, key')
is also somewhat awkward. Contrast this with:
def display_inventory(inventory):
print("Inventory: {0}".format(", ".join(inventory) if inventory else "empty"))
How have I made it so neat? By making inventory
a list of the items your character has, e.g.:
>>> display_inventory([])
Inventory: empty
>>> display_inventory(['mallet', 'crowbar'])
Inventory: mallet, crowbar
Checking whether the user has the item needed is now the relatively clear:
if 'hammer' in inventory:
as opposed to:
if item_use == 1:
This has also removed reliance on the order in which things are collected, so if you later decide to extend your game it will be much easier.
Rooms
Second, you have the function room_sides
which gets passed lots of arguments, each corresponding to the same variables for different rooms. Compare to:
def perform_action():
choices = {1: True, 2: False}
while True:
try:
choice = int(input('(1 for yes, 2 for no)\n'))
except ValueError:
print("Not valid input")
else:
if choice in choices:
return choices[choice]
print("Not valid input")
def room_sides(room, inventory):
if room['completed']:
input('\nNothing of interest here.\n')
else:
input(room['intro'])
print(room['question'])
if perform_action():
if room['item needed'] is None or room['item needed'] in inventory:
for key in ['process', 'outcome', 'item found']:
input(room[key])
inventory.append(room['item'])
room['completed'] = True
else:
print("That won't work.\n")
else:
input("\nI'll leave it alone for now.\n")
main_hub()
Again, how have I achieved this? Firstly, using the inventory
already defined, rather than the global
variables. Secondly, by moving the logic required to take user input out to a separate function (you could so something similar in main_hub
, too). Finally, by making each room_side
a dictionary, rather than a series of variables:
side1 = {'intro': '\nI see a box.',
'question': 'I think I can open this. Should I?',
'item needed': None,
'process': '\n*box opening sounds*',
'outcome': 'I opened the box with ease.',
'item found': 'A crowbar? This will help.\n',
'item': 'crowbar',
'completed': False}
Again, note that the reliance on the collection order has been factored out - we don't care about the order items are collected in, just whether the player has what is needed right now.
- 14k
- 2
- 36
- 62