Skip to content

Instantly share code, notes, and snippets.

@Gwith
Created October 27, 2017 15:04
Show Gist options
  • Save Gwith/b01cf8cec4ad3fbc0aec939bf4c79cba to your computer and use it in GitHub Desktop.
Save Gwith/b01cf8cec4ad3fbc0aec939bf4c79cba to your computer and use it in GitHub Desktop.
import sys, logging
class Room:
def __init__(self, description, directions, items, monsters):
self.description = description
self.directions = directions
self.items = items
self.monsters = monsters
def __repr__(self):
return "{}\n{}".format(self.description, self.directions)
def interact(self):
direction_items = list(self.directions.items())
for index, (key, value) in enumerate(direction_items, 1):
print(index, key)
direction = int(input('Select a direction to go.\n>>>'))
for index, (key, value) in enumerate(direction_items, 1):
if index == direction:
return value
#assert 1 <= new_direction <= len(direction_items)
# return direction_items[new_direction - 1][1] # -1 since we enumerate from 1
courtyard = Room(
'This is a courtyard',
{'North' : 'Entrance'},
['item1', 'item2'],
['monster1', 'monster2']
)
entrance = Room(
'This is the entrance',
{'South' : 'Courtyard', 'North' : 'Corridor'},
['item1', 'item2'],
['monster1', 'monster2']
)
corridor = Room(
'This is corridor',
{'South' : 'Entrance'},
['item1', 'item2'],
['monster1', 'monster2']
)
all_rooms = {
'Entrance': entrance,
'Corridor': corridor,
'Courtyard': courtyard
}
def play():
current_room = all_rooms['Courtyard']
game_running = True
while game_running:
direction = current_room.interact()
current_room = all_rooms[direction]
if __name__ == "__main__":
code = play()
sys.exit(code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment