diff --git a/src/adv.py b/src/adv.py index c9e26b0f85..f36d1c17cd 100644 --- a/src/adv.py +++ b/src/adv.py @@ -1,4 +1,6 @@ from room import Room +from player import Player +from items import Item # Declare all the rooms @@ -22,6 +24,7 @@ } + # Link rooms together room['outside'].n_to = room['foyer'] @@ -36,6 +39,27 @@ # # Main # +directions = ['n', 's', 'e', 'w', 'q'] +player = Player(room['outside']) + +while True: + print(player.current_room) + print(player.current_room.description) + + usr_input = input("Where would you like to go? ") + + try: + while usr_input not in directions: + print("\nInput a valid direction [n, s, e, w]\n") + usr_input = input("Where would you like to go? ") + + if usr_input == 'q': + break + + player.move(usr_input) + + except AttributeError as error: + print("\nYou can't move that way!!\n") # Make a new player object that is currently in the 'outside' room. diff --git a/src/items.py b/src/items.py new file mode 100644 index 0000000000..f3a997675f --- /dev/null +++ b/src/items.py @@ -0,0 +1,4 @@ +class Item: + def __init__(self, name, description): + self.name = name + self.description = description \ No newline at end of file diff --git a/src/player.py b/src/player.py index d79a175029..01aaede024 100644 --- a/src/player.py +++ b/src/player.py @@ -1,2 +1,19 @@ # Write a class to hold player information, e.g. what room they are in # currently. + +class Player: + def __init__(self, current_room, items: List[Item]=None): + self.current_room = current_room + self.items: List[Item] = items + + def move(self, direction): + next_room = self.current_room.get_direction(direction) + if next_room is not None: + self.current_room = next_room + else: + print("You can't move that way!!") + + + def __repr__(self): + return f'Room({repr(self.current_room)})' + diff --git a/src/room.py b/src/room.py index 24c07ad4c8..3ef8daf1b7 100644 --- a/src/room.py +++ b/src/room.py @@ -1,2 +1,23 @@ # Implement a class to hold room information. This should have name and -# description attributes. \ No newline at end of file +# description attributes. + +class Room: + def __init__(self, name, description, items: List[Item]=None): + self.name = name + self.description = description + self.items: List[Item] = items + + def get_direction(self, direction): + if direction == 'n': + return self.n_to + elif direction == 's': + return self.s_to + elif direction == 'e': + return self.e_to + elif direction == 'w': + return self.w_to + + def add_item(self, item): + + def __repr__(self): + return f'Room({repr(self.name)})'