diff --git a/src/adv.py b/src/adv.py index c9e26b0f85..597fcd1447 100644 --- a/src/adv.py +++ b/src/adv.py @@ -1,24 +1,33 @@ from room import Room +from player import Player +from item import Item +import textwrap # Declare all the rooms +# knife = Item('knife', 'small knife') +# map = Item('map', 'map for navigation to room') +# sword = Item('sword', ' long sward for battle') +# bike = Item('bike', 'bike for fast navigation') +# shoe = Item('shoe', 'magic shoe') + room = { - 'outside': Room("Outside Cave Entrance", - "North of you, the cave mount beckons"), + 'outside': Room(name="Outside Cave Entrance", + description="North of you, the cave mount beckons", items=['knife1', 'map1']), - 'foyer': Room("Foyer", """Dim light filters in from the south. Dusty -passages run north and east."""), + 'foyer': Room(name="Foyer", description="""Dim light filters in from the south. Dusty +passages run north and east.""", items=['sword1', 'magic_shoe1']), - 'overlook': Room("Grand Overlook", """A steep cliff appears before you, falling + 'overlook': Room(name="Grand Overlook", description="""A steep cliff appears before you, falling into the darkness. Ahead to the north, a light flickers in -the distance, but there is no way across the chasm."""), +the distance, but there is no way across the chasm.""", items=['power_booster', 'magic_mirror']), - 'narrow': Room("Narrow Passage", """The narrow passage bends here from west -to north. The smell of gold permeates the air."""), + 'narrow': Room(name="Narrow Passage", description="""The narrow passage bends here from west +to north. The smell of gold permeates the air.""", items=['map2', 'lightening_wand']), - 'treasure': Room("Treasure Chamber", """You've found the long-lost treasure + 'treasure': Room(name="Treasure Chamber", description="""You've found the long-lost treasure chamber! Sadly, it has already been completely emptied by -earlier adventurers. The only exit is to the south."""), +earlier adventurers. The only exit is to the south.""", items=['majic_key', 'sword2']), } @@ -38,6 +47,13 @@ # # Make a new player object that is currently in the 'outside' room. +player = Player('John', 'outside') + +# knife = Item('knife', 'small knife') +# map = Item('map', 'map for navigation to room') +# sword = Item('sword', ' long sward for battle') +# bike = Item('bike', 'bike for fast navigation') +# shoe = Item('shoe', 'magic shoe') # Write a loop that: # @@ -49,3 +65,65 @@ # Print an error message if the movement isn't allowed. # # If the user enters "q", quit the game. + +def find_key(value, my_dict): + key_list = list(my_dict.keys()) + value_list = list(my_dict.values()) + return key_list[value_list.index(value)] + +while True: + print('\n','I am in the room called {}'.format(player.current_room), '\n') + room[player.current_room].get_items() + user_item = input('Please enter "take item1 item2" or drop item": ') + item_arr = user_item.split(' ') + + action = item_arr[0] + items = item_arr[1:] + print('item_arr', items) + if item_arr[0] == 'take': + player.pickup_items(*items) + for item in items: + room[player.current_room].delete_items(item) + + # text = f'I am in the room of {player.current_room}. I do not know where to go now However. I will explore around to find a room for treasure.' + # print(textwrap.wrap(text)[0]) + user_input = input('Please enter the direction to go (for exit, enter "q"): ') + + if user_input == 'q': + break + # print('user_input',user_input) + if user_input == 'n': + print('\n', 'going north') + if room[player.current_room].s_to != None: + player.current_room = find_key(room[player.current_room].s_to, room) + print('\n',f'now I am in {player.current_room}') + else: + print('\n', 'no room is in the direction, please choose others') + + elif user_input == 's': + print('\n', 'going south') + if room[player.current_room].n_to != None: + player.current_room = find_key(room[player.current_room].n_to, room) + print('\n',f'now I am in {player.current_room}') + else: + print('\n', 'no room is in the direction, please choose others') + + elif user_input == 'e': + print('\n', 'going east') + if room[player.current_room].w_to != None: + player.current_room = find_key(room[player.current_room].w_to, room) + print('\n',f'now I am in {player.current_room}') + else: + print('\n', 'no room is in the direction, please choose others') + + elif user_input == 'w': + print('\n', 'going west') + if room[player.current_room].e_to != None: + player.current_room = find_key(room[player.current_room].e_to, room) + print('\n',f'now I am in {player.current_room}') + else: + print('\n', 'no room is in the direction, please choose others') + else: + print('Wrong key is entered, please try again') + + diff --git a/src/item.py b/src/item.py new file mode 100644 index 0000000000..5ab1036f73 --- /dev/null +++ b/src/item.py @@ -0,0 +1,5 @@ + +class Item: + def __init__(self, name, description=""): + self.name = name + self.desciption = description diff --git a/src/player.py b/src/player.py index d79a175029..087a021e5d 100644 --- a/src/player.py +++ b/src/player.py @@ -1,2 +1,20 @@ # Write a class to hold player information, e.g. what room they are in # currently. +class Player: + def __init__(self, name, current_room, inventory=[]): + self.name = name + self.current_room = current_room + self.inventory = inventory + + + def __str__(self): + return f"" + + def pickup_items(self, *items): + print('\nPlayer picked up items...\n') + + for item in items: + self.inventory.append(item) + print(f'{item}') + print('\n') + print('self.inventory', self.inventory) \ No newline at end of file diff --git a/src/room.py b/src/room.py index 24c07ad4c8..beaf55aef8 100644 --- a/src/room.py +++ b/src/room.py @@ -1,2 +1,26 @@ # Implement a class to hold room information. This should have name and -# description attributes. \ No newline at end of file +# description attributes. +from item import Item + +class Room: + def __init__(self, name, description, items=[], n_to=None, e_to=None, s_to=None, w_to=None): + self.name = name + self.description = description + self.n_to = n_to + self.e_to = e_to + self.s_to = s_to + self.w_to = w_to + self.items = [Item(p) for p in items] + def __str__(self): + return f"\n" + + def get_items(self): + print(f'{self.name} room has items as follows...') + for item in self.items: + print(f'{item.name}') + print('\n') + + def delete_items(self, item): + + self.items = [item1 for item1 in self.items if item1['name'] != item] + print(f'item deleted in the room: {item}') \ No newline at end of file