diff --git a/src/adv.py b/src/adv.py index c9e26b0f85..11c8b883cf 100644 --- a/src/adv.py +++ b/src/adv.py @@ -1,4 +1,9 @@ from room import Room +from player import Player +from item import Item +import sys + + # Declare all the rooms @@ -33,9 +38,8 @@ room['narrow'].n_to = room['treasure'] room['treasure'].s_to = room['narrow'] -# + # Main -# # Make a new player object that is currently in the 'outside' room. @@ -49,3 +53,107 @@ # Print an error message if the movement isn't allowed. # # If the user enters "q", quit the game. + +player = Player("player1", room['outside']) + +room['treasure'].item_list.append(Item("sword", "Level 1 sword")) +room['overlook'].item_list.append(Item("map", "Level 1 map")) + +while True: + print(f"Current room: {player.current_room.name}") + print(f"Description: {player.current_room.description}") + + print("Items in the room: ") + for item in player.current_room.item_list: + print(f"{item.name} - {item.description}") + + print("Player inventory: ") + for item in player.inventory: + print(f"{item.name} - {item.description}") + + move = input("Enter a move: ") + + if len(move.split()) == 1: + if move != "s" and move != "n" and move != "e" and move != "w" and move != "q": + print("Invalid move!!!") + print("Enter 's' to move south") + print("Enter 'n' to move north") + print("Enter 'w' to move west") + print("Enter 'e' to move east") + print("Enter 'q' to quit the game") + + elif move == "q": + print("The player quit the game!") + sys.exit() + + elif move == "s": + if player.current_room.n_to: + player.current_room = player.current_room.n_to + else: + print("Unable to move in that direction!") + + elif move == "n": + if player.current_room.s_to: + player.current_room = player.current_room.s_to + else: + print("Unable to move in that direction!") + + elif move == "w": + if player.current_room.e_to: + player.current_room = player.current_room.e_to + else: + print("Unable to move in that direction!") + + elif move == "e": + if player.current_room.w_to: + player.current_room = player.current_room.w_to + else: + print("Unable to move in that direction!") + + elif len(move.split()) == 2: + if move.split()[0] == "get": + if len(player.current_room.item_list) > 0: + for item in player.current_room.item_list: + if item.name == move.split()[1]: + player.current_room.item_list.remove(item) + player.inventory.append(item) + else: + print(f"There is no {move.split()[1]} in this room!") + else: + print("There are no items in this room!") + + elif move.split()[0] == "take": + if len(player.current_room.item_list) > 0: + for item in player.current_room.item_list: + if item.name == move.split()[1]: + player.current_room.item_list.remove(item) + player.inventory.append(item) + else: + print(f"There is no {move.split()[1]} in this room!") + else: + print("There are no items in this room!") + + elif move.split()[0] == "drop": + if len(player.inventory) > 0: + for item in player.inventory: + if item.name == move.split()[1]: + player.inventory.remove(item) + player.current_room.item_list.append(item) + else: + print(f"You do not have {move.split()[1]} in your inventory!") + + else: + print("You have no items in your inventory!") + + else: + print("Invalid move!!!") + + else: + print("Invalid move!!!") + + + + + + + diff --git a/src/item.py b/src/item.py new file mode 100644 index 0000000000..10274947cd --- /dev/null +++ b/src/item.py @@ -0,0 +1,5 @@ +class Item: + def __init__(self, name, description): + self.name = name + self.description = description + diff --git a/src/player.py b/src/player.py index d79a175029..6d45b2e051 100644 --- a/src/player.py +++ b/src/player.py @@ -1,2 +1,9 @@ # Write a class to hold player information, e.g. what room they are in # currently. + +class Player: + def __init__(self, name, current_room): + self.name = name + self.current_room = current_room + self.inventory = [] + diff --git a/src/room.py b/src/room.py index 24c07ad4c8..641de1fda1 100644 --- a/src/room.py +++ b/src/room.py @@ -1,2 +1,13 @@ # 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): + self.name = name + self.description = description + self.s_to = "" + self.n_to = "" + self.w_to = "" + self.e_to = "" + self.item_list = [] +