diff --git a/examples/guessing_game.py b/examples/guessing_game.py index 5c27a5443f..526f846ad3 100644 --- a/examples/guessing_game.py +++ b/examples/guessing_game.py @@ -25,4 +25,6 @@ def guessing_game(): print("Too big!") if __name__ == '__main__': - guessing_game() \ No newline at end of file + guessing_game() + +# init \ No newline at end of file diff --git a/src/adv.py b/src/adv.py index c9e26b0f85..e93c55ecd5 100644 --- a/src/adv.py +++ b/src/adv.py @@ -1,4 +1,11 @@ from room import Room +from player import Player +from item import Item + +# Declare items available + +sword = Item('Sword', 'A short and rusty blade, must of been left here ages ago!') +shield = Item('Shield', 'a small wooden shield') # Declare all the rooms @@ -21,7 +28,6 @@ earlier adventurers. The only exit is to the south."""), } - # Link rooms together room['outside'].n_to = room['foyer'] @@ -33,19 +39,93 @@ 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. - -# Write a loop that: -# -# * Prints the current room name -# * Prints the current description (the textwrap module might be useful here). -# * Waits for user input and decides what to do. -# -# If the user enters a cardinal direction, attempt to move to the room there. -# Print an error message if the movement isn't allowed. -# -# If the user enters "q", quit the game. +# ##################################################################### # +# Main # +# ##################################################################### # + +# Name input +name = input("What is your adventurer's name? ") + +# Player object starts 'outside' +player = Player(room['outside'], name) + +# Greeting for when you enter the game +print(f"\nGreetings {player.playerName}, you can control your adventure using the keys: N, E, S, W to move around the dungeon and Q to end your time here") + +# Add items to foyer room +def add_items_to_foyer(): + if player.currentRoom.name != room['foyer'].name: + room['foyer'].items.pop() + room['foyer'].items.pop() + if player.currentRoom.name == room['foyer'].name: + room['foyer'].items.append(sword) + room['foyer'].items.append(shield) + + +# game loop +game_over = False + +while game_over == False: + + player_move = input("\n[N] North, [E] East, [S] South, [W] West, [Q] Quit, [I] Items >> ").upper() + + # Inventory inputs + + if player_move == 'I': + + print(*player.playerItem, sep = "\n") + + item_input = input("[Drop] or [Take] [Item] >> ").upper() + + if item_input == 'DROP SWORD': + player.playerItem.remove(sword) + print(f'\n{sword.itemName} has been dropped') + + if item_input == 'TAKE SWORD': + player.playerItem.append(sword) + print(f"{sword.itemName} has been picked up") + + if item_input == 'DROP SHIELD': + player.playerItem.remove(shield) + print(f'\n{shield.itemName} has been dropped') + + if item_input == 'TAKE SHIELD': + player.playerItem.append(shield) + print(f"{shield.itemName} has been picked up") + + + # Player movement + + if player_move == 'N' or player_move == 'E' or player_move == 'S' or player_move == 'W' or player_move == 'Q': + + if player_move == 'N' and player.currentRoom.n_to != None: + player.currentRoom = player.currentRoom.n_to + add_items_to_foyer() + print(player.currentRoom) + + elif player_move == 'E' and player.currentRoom.e_to != None: + player.currentRoom = player.currentRoom.e_to + add_items_to_foyer() + print(player.currentRoom) + + elif player_move == 'S' and player.currentRoom.s_to != None: + player.currentRoom = player.currentRoom.s_to + add_items_to_foyer() + print(player.currentRoom) + + elif player_move == 'W' and player.currentRoom.w_to != None: + player.currentRoom = player.currentRoom.w_to + add_items_to_foyer() + print(player.currentRoom) + + elif player_move == 'N' and player.currentRoom.n_to == None or player_move == 'E' and player.currentRoom.e_to == None or player_move == 'S' and player.currentRoom.s_to == None or player_move == 'W' and player.currentRoom.w_to == None: + print('This seems to be the wrong way, please choose a different direction') + + elif player_move == 'Q': + print("Good bye!") + game_over = True + + + + # else: + # print('Invalid command, please choose from the given options') diff --git a/src/item.py b/src/item.py new file mode 100644 index 0000000000..9a0e282590 --- /dev/null +++ b/src/item.py @@ -0,0 +1,11 @@ +# This will be the base class for specialized item types to be declared later. +# The item should have name and description attributes. +# Hint: the name should be one word for ease in parsing later. + +class Item: + def __init__(self, itemName, itemDes): + self.itemName = itemName + self.itemDes = itemDes + + def __str__(self): + return f"{self.itemName}: {self.itemDes}" \ No newline at end of file diff --git a/src/player.py b/src/player.py index d79a175029..53f12f14ef 100644 --- a/src/player.py +++ b/src/player.py @@ -1,2 +1,12 @@ # Write a class to hold player information, e.g. what room they are in # currently. + + +class Player: + def __init__(self, currentRoom, playerName, playerItem = []): + self.currentRoom = currentRoom + self.playerName = playerName + self.playerItem = playerItem + + def __str__(self): + return f"{self.playerName} is currently in room: {self.currentRoom}" \ No newline at end of file diff --git a/src/room.py b/src/room.py index 24c07ad4c8..daf31c70e6 100644 --- a/src/room.py +++ b/src/room.py @@ -1,2 +1,19 @@ # 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, n_to = None, s_to = None, e_to = None, w_to = None, items = []): + self.name = name + self.description = description + self.n_to = n_to + self.s_to = s_to + self.e_to = e_to + self.w_to = w_to + self.items = items + + def __str__(self): + if len(self.items) > 0: + return f"\nYou are currently in {self.name}.\n{self.description}\n\nYou spot some items in the distance\n\n{self.items[0]}\n{self.items[1]}" + else: + return f"\nYou are currently in {self.name}.\n{self.description}" +