def choose_character(): """Function to select Wizard or Ranger as character. Returns: str: A string that is Wizard or Ranger. """ scroll_print("\nBefore embarking on this tumultuous adventure, " "would you like to play as a fearless Ranger " "or a brilliant Wizard? ") invalid_selection = True # Validate user input to ensure it is either ranger or wizard. while invalid_selection: character_choice = input().lower().strip() if character_choice != "ranger" and character_choice != "wizard": scroll_print("\nYou entered an invalid selection, please choose " "between Ranger and Wizard: ") else: invalid_selection = False character_choice = character_choice.capitalize() return character_choice
def unequip_item(self): """Unequip current equipped item.""" item = self.equipped_item if item is not None: # Remove item stats that were added when item was first equipped. stat_list = item.get_stats() scroll_print("Unequipped {}".format(item.get_name())) # Check the specific item stat list for the presence of stats. # Remove stats from player if found. for stat in stat_list: if stat == 'health': self.set_health(self.get_health() - stat_list['health']) if stat == 'magic': self.set_magic(self.get_magic() - stat_list['magic']) if stat == 'level': self.set_level(self.get_level() - stat_list['level']) if stat == 'magic_defense': self.set_magic_defense(self.get_magic_defense() - stat_list['magic_defense']) if stat == 'magic_power': self.set_magic_power(self.get_magic_power() - stat_list['magic_power']) if stat == 'defense': self.set_defense(self.get_defense() - stat_list['defense']) if stat == 'attack_power': self.set_attack_power(self.get_attack_power() - stat_list['attack_power']) self.equipped_item = None
def room_5_item_handler(current_room, verb, item_name, feature): """Handle room 5, kitchen, player and item interactions. Args: current_room (:obj:`Room`): The current room the player is in. verb (str): The action the user would like to take. item_name: The name of the item the user would like to use. """ player = current_room.get_player() # Pick up acidic ooze by using the jar. if (verb == "use" and item_name == "jar" and "jar" in player.get_item_names() and "acidic ooze" in current_room.get_item_names() and (feature is None or feature == "acidic ooze")): scroll_print("You use the jar to grab some of the ooze.") player.use_item("jar") ooze = current_room.get_item("acidic ooze") # Remove ooze as the player has now picked it up. current_room.remove_item(ooze) current_room.set_puzzle_status("ooze", False) # Add acidic ooze item to player. jar_name = "acidic ooze" jar_description = ("The jar is doing a good job of holding the " "ooze.") jar_durability = 1 jar_stats = None jar = Item(jar_name, jar_description, jar_durability, jar_stats) player.add_item(jar) # Player cannot pick up the ooze. They must use the jar. elif (verb == "take" and item_name == "acidic ooze" and current_room.get_puzzle_status("ooze")): scroll_print("You reach out to grab some of the ooze. When you touch " "it you feel a sharp burning sensation. Ouch! You " "quickly run to the sink to wash your hand off. You " "better find a better way to carry this ooze.") # These verbs do not need unique room interactions. else: general_item_handler(current_room, verb, item_name, feature) save_object_state(current_room)
def use_item(self, item_name): """Simulate using item by removing item durability.""" if item_name in self.get_item_names(): item = self.get_item(item_name) item.decrement_durability() # Item destroyed if durability is 0. if item.get_durability() == 0: scroll_print( "Item {} used and removed from inventory.\n".format( item_name)) self.backpack.remove(item)
def level_up(self, new_level): """Increase character stats when certain experience reached.""" self.level += 1 self.health += 10 self.magic += 5 self.magic_defense += 1 self.magic_power += 1 self.defense += 1 self.attack_power += 1 if new_level == 3: scroll_print("%s has learned Cleave! " % (self.name)) elif new_level == 5: scroll_print("%s has learned Sever! " % (self.name))
def choose_name(character_choice): """Ask user to enter their character name. Args: character_choice (str): String that is either Wizard or Ranger. Returns: str: The name the player entered for themselves. """ scroll_print("\nExcellent choice! I am sure your %s will make a fine " "adventurer. What would you like to name your %s? " % (character_choice, character_choice)) player_name = input().strip() scroll_print("\nSalutations %s! It is now time to embark on the " "adventure...\n" % (player_name)) return player_name
def room_8_item_handler(current_room, verb, item_name, feature): """Handle room 8, sleeping chambers, player and item interactions. Args: current_room (:obj:`Room`): The current room the player is in. verb (str): The action the user would like to take. item_name: The name of the item the user would like to use. """ player = current_room.get_player() # Use the emerald key to unlock the emerald lock box. if (verb == "use" and item_name == "emerald key" and "emerald key" in player.get_item_names() and current_room.get_puzzle_status("nightstand") and (feature is None or feature == "box")): # Set puzzle to false so that this puzzle cannot be completed twice. current_room.set_puzzle_status("nightstand", False) scroll_print("You use the emerald key on the lock box and it opens. " "Inside is a healing potion.") # Add healing potion to player inventory. healing_potion_name = "healing potion" healing_potion_description = ("A glass vial of thick red liquid. ") healing_potion_durability = 1 healing_potion_stats = None healing_potion = Item(healing_potion_name, healing_potion_description, healing_potion_durability, healing_potion_stats) player.use_item("emerald key") player.add_item(healing_potion) # These verbs do not need unique room interactions. else: general_item_handler(current_room, verb, item_name, feature) save_object_state(current_room)
def npc_attack(self, attack_type): """Calculate the monster attack damage. Args: attack_type: The monster chosen attack. Returns: int: The calculated monster attack damage. """ # Randomly select a melee or magic attack. if attack_type == 0: scroll_print("\n%s swung their weapon at you! " % (self.name)) # Randomize the damage based on the move and applicable equipment attack_damage = randint(0, self.attack_power) return attack_damage elif attack_type == 1: scroll_print("\n%s is casting a spell! " % (self.name)) if self.magic < 3: scroll_print("%s doesn't have enough magic! " % (self.name)) attack_damage = 0 else: # Randomize the damage based on the move and # applicable equipment. attack_damage = randint(0, self.magic_power) # Adjust the player's stats. self.magic -= 3 return attack_damage
def room_12_item_handler(current_room, verb, item_name, feature): """Handle room 12, archives, player and item interactions. Args: current_room (:obj:`Room`): The current room the player is in. verb (str): The action the user would like to take. item_name: The name of the item the user would like to use. """ player = current_room.get_player() # Use charcoal to get the painting scrap. if (verb == "use" and item_name == "charcoal" and "charcoal" in player.get_item_names() and (feature is "charcoal" or feature == "fireplace")): # Charcoal removed from inventory. player.use_item("charcoal") scroll_print("You throw the charcoal into the fire. There is a green " "flash of light. Ash floats up from the fire and forms " "into a small scrap of fabric. You see that it is a " "torn piece of a painting. A face is on the paper.") # Add scrap to player inventory. scrap_name = "scrap" scrap_description = ("A torn piece of a painting. You can make out a " "face on the canvas scrap.") scrap_durability = 1 scrap_stats = None scrap = Item(scrap_name, scrap_description, scrap_durability, scrap_stats) player.add_item(scrap) # These verbs do not need unique room interactions. else: general_item_handler(current_room, verb, item_name, feature) save_object_state(current_room)
def room_11_item_handler(current_room, verb, item_name, feature): """Handle room 11, tower hall, player and item interactions. Args: current_room (:obj:`Room`): The current room the player is in. verb (str): The action the user would like to take. item_name: The name of the item the user would like to use. """ player = current_room.get_player() # Use scrap of painting on painting to receive blessing giving you # plus attack power. Scrap item got by using the charcoal item in room 12. # Charcoal got in this room. if (verb == "use" and item_name == "scrap" and "scrap" in player.get_item_names() and (feature is None or feature == "painting")): scroll_print("You place the painting scrap on the area of the " "painting that was torn away. The scrap magically " "stitches to the painting. You hear a ghostly voice " "say, \"Thank you for restoring me to my rightful place, " "in doing so you have freed the portion of my soul that " "the warlock Zlor had trapped here many years ago. As " "thanks, take my blessing, it will aid you in your " "fight.\" As the voice fades you feel your strength " "increase. +1 has been added to you attack power.\n") # Scrap disappears from inventory. player.use_item("scrap") attack_power = player.get_attack_power() player.set_attack_power(attack_power + 1) # These verbs do not need unique room interactions. else: general_item_handler(current_room, verb, item_name, feature) save_object_state(current_room)
def room_1_item_handler(current_room, verb, item_name, feature): """Handle room 1, fortress entrance, player and item interactions. Args: current_room (:obj:`Room`): The current room the player is in. verb (str): The action the user would like to take. item_name: The name of the item the user would like to use. """ player = current_room.get_player() if (verb == "use" and item_name == "sword" and "sword" in player.get_item_names() and feature is None): scroll_print("You start swinging your sword around like a " "lunatic. If anyone were around to see you I'm sure " "they'd be terrified.") # These verbs do not need unique room interactions. else: general_item_handler(current_room, verb, item_name, feature) save_object_state(current_room)
def drop_item(self, item_name): """Removes item from inventory and returns it. Args: item_name: Name of item to be removed. Returns: :obj:`Item`: The item object removed from inventory. """ # Check if item is equipped. Unequip it if it is. if (self.equipped_item is not None and self.equipped_item.get_name() == item_name): self.unequip_item() # Remove item and return it. for item in self.backpack: if item.get_name() == item_name: scroll_print( "Removed item {} from inventory.".format(item_name)) self.backpack.remove(item) return item
def room_2_item_handler(current_room, verb, item_name, feature): """Handles room 2, entrance hall, player and item interactions. Args: current_room (:obj:Room): The current room that the player is in. verb (str): The action a user would like to take. item_name (str): The name of the item the user would like to use. """ player = current_room.get_player() # Triggers rope trap if trap hasn't already been triggered. if (verb == "look at" and item_name == "golden key" and (current_room.get_puzzle_status("rope") or "rope" in current_room.get_features())): scroll_print("The golden key glitters on the floor among the " "rubble. You walk forward to take a closer look.\n") rope_trap(current_room) # Triggers rope trap if trap hasn't already been triggered. elif (verb == "take" and item_name == "golden key" and (current_room.get_puzzle_status("rope") or "rope" in current_room.get_features())): scroll_print("The golden key glitters on the floor among the " "rubble. You walk forward to pick up the key.\n") rope_trap(current_room) # Verb use uniquely interacts with this rooms features. # After a user inspects the rubble in the room, the rope feature is # added. Then, if a player uses the sword, it will disable the rope # trap and remove it as a feature from the room. elif (verb == "use" and item_name == "sword" and "sword" in player.get_item_names() and (feature is None or feature == "rope")): if "rope" in current_room.get_features(): scroll_print("You kneel down and cut the rope. As soon as the " "rope is cut you here a click and a crossbow " "bolt zooms over your head. Good thing you saw " "this trap ahead of time.") current_room.remove_feature("rope") # These verbs do not get unique handlers for this room. else: general_item_handler(current_room, verb, item_name, feature) save_object_state(current_room)
def is_game_over(player): """Checks to see if the player still has lives. Args: player (:obj:`Player`): The main character of the game. Returns: bool: False if the character is dead or the game is complete. """ if player.get_lives() <= 0: return True if player.rescue_evelyn is True: scroll_print("\n The End \n") scroll_print("Game created by Ian Lumiere, Sunny Huang, and Nathan " "Zimmerman.") scroll_print("Thanks for playing!") return True else: return False
def travel(current_room, direction): """Move player from one room to another. Args: current_room (:obj:`Room`): The current room that the player is in. direction (str): Either north, east, south, west. """ if current_room.get_adjacent_room(direction) is not None: scroll_print("Moving " + direction + " to the " + current_room.get_adjacent_room(direction)) # Check if the door is locked. current_door_map = current_room.get_door_map() if current_door_map[direction] is False: scroll_print("\nThe door is unlocked!\n") # Move the character into the new room. player = current_room.get_player() current_room.set_player(None) # Save state to ensure that player is no longer in old room. save_object_state(current_room) new_room_name = current_room.get_adjacent_room(direction) new_room = load_object(new_room_name) new_room.set_player(player) # Warlock voice interaction number 1. if (new_room.get_name() == "store room" and new_room.get_puzzle_status("voice")): scroll_print("As you walk through the door your vision " "suddenly goes black. A voice enters your mind. " "It speaks in a low raspy growl: \"Ahh it " "appears I have an intruder. Although you cannot " "see me, I assure you I can see you. I am Zlor " "and this is my fortress. Your brother will not " "be returned. You cannot save him, I will make " "sure of that...\" The voice fades and your " "vision returns. Just as it does, there is a " "flash of light and the sound of the stone floor " "shifting. Dark black vines suddenly shoot up " "out of the cracks in the floor. The vines " "quickly grow to block the stairwell entrance to " "the north...Things quiet down and you take a " "look around\n") # Set to false so it only triggers once. new_room.set_puzzle_status("voice", False) # Warlock voice interaction number 2. elif (new_room.get_name() == "sauna room" and new_room.get_puzzle_status("sauna voice")): scroll_print("As you walk through the door your vision goes " "black. A voice enters your mind. It is the same " "voice as the one you encountered in the store " "room. It says, \"So you've made it this far...I " "am tired of these games. You will go no " "farther. I have prepared a special potion that " "has been added to the sauna you are now in. Do " "whatever you like, but while the steam remains " "you will not find the way forward.\". The voice " "fades and your vision returns.\n") # Set to false so that it only triggers once. new_room.set_puzzle_status("sauna voice", False) # scroll_print out room description. if player.has_memory(new_room_name): scroll_print(new_room.get_short_description()) else: player.add_memory(new_room_name) scroll_print(new_room.get_description()) print_item_descriptions(new_room) # Save state so that room tracks player in game files. save_object_state(new_room) else: # Print custom message to indicate that room 4 stairwell is # blocked by vines. if (current_room.get_name() == "store room" and current_room.get_puzzle_status("vines")): scroll_print("\nYou can't enter the stairwell. The black " "vines are blocking the way. You'll need to " "find a way to remove them.") # Print custom message to give a hint concerning how to get the # key for the last door. elif (current_room.get_name() == "room of last rites" and current_room.get_puzzle_status("cage")): scroll_print("\nYou try and open the door but it is locked. " "A voice calls to you from the cage. \"Hey " "if you let me out of here I might be able to " "help you get the key to that door.\"") else: scroll_print("The door is locked!") else: scroll_print("There is no room in that direction!")
def handle_standard_action(current_room, player, action): """Handles standard one word game commands. Args: current_room (:obj:`Room`): The current room that the player is in. player (:obj:`Player`): The current player. action (str): The user entered command to be handled. """ if action["standard_action"] == "gamemenu" or\ action["standard_action"] == "game menu": game_menu(current_room) elif action["standard_action"] == "help": help_menu() # Look always prints the long form room description. elif action["standard_action"] == "look": scroll_print(current_room.get_description()) print_item_descriptions(current_room) elif action["standard_action"] == "inventory": if not player.get_inventory(): scroll_print("Your backpack is empty!") else: scroll_print("Your backpack has: ") for item in player.get_inventory(): scroll_print(item.get_name()) # Also print equipped item. equipped_item = player.get_equipped_item() if equipped_item is not None: scroll_print("\nEquipped item: {}" .format(equipped_item.get_name())) else: scroll_print("\nEquipped item: None") elif (action["standard_action"] == "savegame" or action["standard_action"] == "save game"): save_game(current_room) elif (action["standard_action"] == "loadgame" or action["standard_action"] == "load game"): if get_num_saved_games() < 1: scroll_print("\nNo saved games to load.\n") else: load_game() # Get information to scroll_print description of current room the # player is in. room = get_current_room() player = room.get_player() # scroll_print out room description. if player.has_memory(room.get_name()): scroll_print(room.get_short_description()) else: player.add_memory(room.get_name()) scroll_print(room.get_description()) print_item_descriptions(room) elif action["standard_action"] == 'stats' or\ action["standard_action"] == 'get stats' or\ action["standard_action"] == 'my stats' or\ action["standard_action"] == 'player stats': room = get_current_room() player = room.get_player() player.print_stats() save_object_state(current_room)
def print_item_descriptions(current_room): """Prints the descriptions of all of the items in a room. Args: current_room (:obj:`Room`): The current room that the player is in. """ # Custom message if there is only one item in the room. item_list = current_room.get_item_names() for i, item_name in enumerate(item_list): if item_name == "sword": item_list[i] = "a sword" elif item_name == "golden key": item_list[i] = "a golden key" elif item_name == "bread": item_list[i] = "a piece of bread" elif item_name == "letter": item_list[i] = "a letter" elif item_name == "jar": item_list[i] = "a jar" elif item_name == "oven mitt": item_list[i] = "an oven mitt" elif item_name == "emerald key": item_list[i] = "an emerald key" elif item_name == "healing potion": item_list[i] = "a healing potion" elif item_name == "book": item_list[i] = "a book" elif item_name == "rapier": item_list[i] = "a rapier" elif item_name == "charcoal": item_list[i] = "a piece of charcoal" elif item_name == "scrap": item_list[i] = "a painting scrap" elif item_name == "iron key": item_list[i] = "an iron key" elif item_name == "skull key": item_list[i] = "a skull key" if len(current_room.get_item_names()) == 1: scroll_print("\nAs you enter the area you also notice {} on the " "ground.".format(item_list[0])) # Custom message if there is only two items in the room. elif len(current_room.get_item_names()) == 2: scroll_print("\nAs you enter the area you also notice {} and {} " "on the ground.".format(item_list[0], item_list[1])) # Custom message if there is more than two items in the room. elif len(current_room.get_item_names()) > 2: scroll_print("\nAs you enter the area you also notice ") count = 0 for i, item_name in enumerate(item_list): if i == len(current_room.get_item_names()) - 1: if count > 6: print("") count = 0 scroll_print("and {} on the ground.".format(item_name)) else: if count > 6: count = 0 print("") print("{}, ".format(item_name), end='') count += 1
def take_action(current_room, action): """Handles parsed input to perform actions in game. Args: current_room (:obj:`Room`): Current room that the player is in. action (dictionary(str, str)): A dictionary mapping possible actions to either features or items. """ player = current_room.get_player() # Handle any error output. if action["error"] is not None: scroll_print(action["error"]) # Handle the standard actions. elif action["standard_action"] is not None: return handle_standard_action(current_room, player, action) # Handle the directions to pass correct arguments to travel function. elif action["direction"] is not None: # Check for room 2 rope trap trigger. if (current_room.get_name() == "entrance hall" and (action["direction"] == 'east' or action["direction"] == "mess hall") and (current_room.get_puzzle_status("rope") or "rope" in current_room.get_features())): rope_trap(current_room) # Handle input indicating character wants to move rooms. elif action["direction"] != "north" and \ action["direction"] != "east" and \ action["direction"] != "south" and \ action["direction"] != "west": desired_room = action["direction"] # Get list of rooms connected to the current room. adjacent_rooms = current_room.get_adjacent_rooms() direction = "" if desired_room in adjacent_rooms.values(): # Get the key to get the direction. for key in adjacent_rooms.keys(): if adjacent_rooms[key] == desired_room: direction = key if direction != "": travel(current_room, direction) else: scroll_print("That room is not connected to the " "current room!") else: if current_room.get_name() == desired_room: scroll_print("You're already in that room.") else: scroll_print("That room is not connected to the current " "room!") else: travel(current_room, action["direction"]) # Handle player / item interaction for a given room. elif (action["verb"] is not None and action["item"] is not None and (action["item"] in player.get_item_names() or action["item"] in current_room.get_item_names())): # Check for incorrect use of "on" preposition. It can only be used # with use commands. if (action["verb"] != "use" and action["feature"] is not None and (action["feature"] != "charcoal" and action["item"] != "charcoal")): scroll_print("You can't {} {} on {}".format(action["verb"], action["item"], action["feature"])) elif current_room.get_name() == "fortress entrance": room_1_item_handler(current_room, action["verb"], action["item"], action["feature"]) elif current_room.get_name() == "entrance hall": room_2_item_handler(current_room, action["verb"], action["item"], action["feature"]) elif current_room.get_name() == "mess hall": room_3_item_handler(current_room, action["verb"], action["item"], action["feature"]) elif current_room.get_name() == "store room": room_4_item_handler(current_room, action["verb"], action["item"], action["feature"]) elif current_room.get_name() == "kitchen": room_5_item_handler(current_room, action["verb"], action["item"], action["feature"]) elif current_room.get_name() == "washroom": room_6_item_handler(current_room, action["verb"], action["item"], action["feature"]) elif current_room.get_name() == "smoking room": room_7_item_handler(current_room, action["verb"], action["item"], action["feature"]) elif current_room.get_name() == "sleeping chambers": room_8_item_handler(current_room, action["verb"], action["item"], action["feature"]) elif current_room.get_name() == "supplies closet": room_9_item_handler(current_room, action["verb"], action["item"], action["feature"]) elif current_room.get_name() == "sauna room": room_10_item_handler(current_room, action["verb"], action["item"], action["feature"]) elif current_room.get_name() == "tower hall": room_11_item_handler(current_room, action["verb"], action["item"], action["feature"]) elif current_room.get_name() == "archives": room_12_item_handler(current_room, action["verb"], action["item"], action["feature"]) elif current_room.get_name() == "reading room": room_13_item_handler(current_room, action["verb"], action["item"], action["feature"]) elif current_room.get_name() == "room of last rites": room_14_item_handler(current_room, action["verb"], action["item"], action["feature"]) elif current_room.get_name() == "final lair": room_15_item_handler(current_room, action["verb"], action["item"], action["feature"]) # Handle player / feature interaction for a given room. elif (action["verb"] is not None and action["feature"] is not None and action["feature"] in current_room.get_features()): if current_room.get_name() == "fortress entrance": room_1_feature_handler(current_room, action["verb"], action["feature"]) elif current_room.get_name() == "entrance hall": room_2_feature_handler(current_room, action["verb"], action["feature"]) elif current_room.get_name() == "mess hall": room_3_feature_handler(current_room, action["verb"], action["feature"]) elif current_room.get_name() == "store room": room_4_feature_handler(current_room, action["verb"], action["feature"]) elif current_room.get_name() == "kitchen": room_5_feature_handler(current_room, action["verb"], action["feature"]) elif current_room.get_name() == "washroom": room_6_feature_handler(current_room, action["verb"], action["feature"]) elif current_room.get_name() == "smoking room": room_7_feature_handler(current_room, action["verb"], action["feature"]) elif current_room.get_name() == "sleeping chambers": room_8_feature_handler(current_room, action["verb"], action["feature"]) elif current_room.get_name() == "supplies closet": room_9_feature_handler(current_room, action["verb"], action["feature"]) elif current_room.get_name() == "sauna room": room_10_feature_handler(current_room, action["verb"], action["feature"]) elif current_room.get_name() == "tower hall": room_11_feature_handler(current_room, action["verb"], action["feature"]) elif current_room.get_name() == "archives": room_12_feature_handler(current_room, action["verb"], action["feature"]) elif current_room.get_name() == "reading room": room_13_feature_handler(current_room, action["verb"], action["feature"]) elif current_room.get_name() == "room of last rites": room_14_feature_handler(current_room, action["verb"], action["feature"]) elif current_room.get_name() == "final lair": room_15_feature_handler(current_room, action["verb"], action["feature"]) elif action["verb"] == "duck": scroll_print("You duck quickly and then stand back up.") else: scroll_print("I can't do that.") save_object_state(current_room)
def help_menu(): """scroll_print a list of available commands for the user.""" scroll_print("\nHere is a list of available commands: ") scroll_print("game menu") scroll_print("save game") scroll_print("load game") scroll_print("inventory") scroll_print("stats") scroll_print("take <item>") scroll_print("use <item>") scroll_print("drop <item>") scroll_print("go <direction>") scroll_print("look at <feature>") scroll_print("look at <item>") scroll_print("eat <feature>") scroll_print("eat <item>") scroll_print("drink <feature>") scroll_print("drink <item>") scroll_print("smell <feature>") scroll_print("listen to <feature>") scroll_print("climb <feature>") scroll_print("rotate <feature>") scroll_print("duck") scroll_print("equip <item>")
def game_menu(current_room): """Calls in game menu for use during play. Args: current_room (:obj:`Room`): The current room that the player is in. """ invalid_selection = True # Validate user menu selection. while invalid_selection: scroll_print("\nGame Menu: ") scroll_print(" Save Game File ") scroll_print(" Load Game File ") scroll_print(" Return to Game ") scroll_print(" Quit Game ") scroll_print("Please select an option by entering: ") scroll_print("Save, Return, or Quit ") menu_choice = input().lower().strip() if menu_choice != "save" and menu_choice != "return" and \ menu_choice != "quit" and menu_choice != "load": scroll_print("You entered an invalid option! ") elif menu_choice == "load" and get_num_saved_games() < 1: scroll_print("\nNo saved games to load. Select another option.") else: invalid_selection = False if menu_choice == "save": save_game(current_room) elif menu_choice == "load": load_game() room = get_current_room() player = room.get_player() # scroll_print out room description. if player.has_memory(room.get_name()): scroll_print(room.get_short_description()) else: # If this is the players first time in a room, store the room name # so that the short form description can be used next time. player.add_memory(room.get_name()) scroll_print(room.get_description()) print_item_descriptions(room) elif menu_choice == "quit": scroll_print("\nThank you for playing Nightfall. " "Have a fortuitous evening... \n") exit() else: scroll_print("Returning to the game!")
def print_combat_stats(self): """Prints the players combat stats.""" scroll_print("\nYour Current Stats") scroll_print("Remaining Lives: {}".format(self.num_lives)) scroll_print("Health: {}".format(self.health)) scroll_print("Magic: {}".format(self.magic))
def starting_menu(): """Creates menu viewed when game first starts. Returns: str: A string that is the menu option chosen by the user. """ # Intro message printed for user at start of game. scroll_print("\nOh no! While wandering the woods of Tardatheon, your " "younger brother Evelyn was snatched by the evil warlock, " "Zlor. Following your brother’s screams, you race through " "the thick brambles and branches of the forest as they whip " "and slash against your skin. All of a sudden, you find " "yourself in front of the warlock’s looming tower. \n") scroll_print("Zlor is holding your brother hostage here. All you have is " "the knapsack on your back and a fearless spirit, but you " "must carry on and save your brother.\n ") scroll_print("Welcome to Nightfall. The path that lies ahead is dark and " "full of terrors.\n") scroll_print("Before beginning the game please ensure that your " "terminal window is wide enough to allow a minimum of 80 " "characters to be printed on a single line. Here is 80 " "characters: ") scroll_print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" "XXXXXXXXXXXXXXXXXXXXX\n") scroll_print("Make sure all the X's show on one line.\n") # Check user input for valid menu option. invalid_selection = True while invalid_selection: scroll_print("\nStarting Menu:") scroll_print(" Start New Game") scroll_print(" Load Game File") scroll_print(" Quit Game") scroll_print("Please select an option by entering:") scroll_print("Start, Load, or Quit") menu_choice = input().lower().strip() if menu_choice != "load" and menu_choice != "start" and \ menu_choice != "quit": scroll_print("You entered an invalid option!") elif menu_choice == 'load' and get_num_saved_games() < 1: scroll_print("\nNo saved games to load. Select another option.\n") else: invalid_selection = False if menu_choice != "quit" and shutil.get_terminal_size()[0] < 80: scroll_print("\nTerminating program. Your terminal window is too " "small. Please adjust your terminal window size so it " "has a width of at least 80 chars and restart the " "program.") menu_choice = "quit" return menu_choice
def add_item(self, item): """Add item from room to player inventory.""" scroll_print("Added {} to your inventory.".format(item.get_name())) self.backpack.append(item)
def equip_item(self, item): """Equip item that is in player inventory to get stats. Args: item: The item object to be equipped """ if item in self.backpack and item.get_stats() is not None: if self.equipped_item is not None: self.unequip_item() stat_list = item.get_stats() scroll_print("You equipped the {}.\n".format(item.get_name())) scroll_print("Stats gained:") # Check specific item stat list for stats. For each stat found, # add it to the player stats. for stat in stat_list: if stat == 'health': scroll_print("+{} health".format(stat_list['health'])) self.set_health(self.get_health() + stat_list['health']) if stat == 'magic': scroll_print("+{} magic".format(stat_list['magic'])) self.set_magic(self.get_magic() + stat_list['magic']) if stat == 'level': scroll_print("+{} level".format(stat_list['level'])) self.set_level(self.get_level() + stat_list['level']) if stat == 'magic_defense': scroll_print("+{} magic defense".format( stat_list['magic_defense'])) self.set_magic_defense(self.get_magic_defense() + stat_list['magic_defense']) if stat == 'magic_power': scroll_print("+{} magic power".format( stat_list['magic_power'])) self.set_magic_power(self.get_magic_power() + stat_list['magic_power']) if stat == 'defense': scroll_print("+{} defense".format(stat_list['defense'])) self.set_defense(self.get_defense() + stat_list['defense']) if stat == 'attack_power': scroll_print("+{} attack power".format( stat_list['attack_power'])) self.set_attack_power(self.get_attack_power() + stat_list['attack_power']) self.equipped_item = item else: scroll_print("You cannot equip that item.")
def check_invalid_attack(self, attack_choice, level): """Checks if the player selected an invalid attack. Args: attack_choice: The user choice of attack. level: The player's level. """ if level > 4: if attack_choice != 'bash' and attack_choice != 'thunder' and \ attack_choice != 'singe' and attack_choice != 'inferno' and \ attack_choice != 'corrupt': scroll_print("\nYou entered an invalid choice! ") scroll_print("Please enter: Bash, Thunder, Singe, Inferno, or " "Corrupt: ") return True elif level > 2: if attack_choice != 'bash' and attack_choice != 'thunder' and \ attack_choice != 'singe' and attack_choice != 'inferno': scroll_print("\nYou entered an invalid choice! ") scroll_print( "Please enter: Bash, Thunder, Singe, or Inferno: ") return True else: if attack_choice != 'bash' and attack_choice != 'thunder' and \ attack_choice != 'singe': scroll_print("\nYou entered an invalid choice! ") scroll_print("Please enter: Bash, Thunder, or Singe: ") return True return False
def combat(current_room, player, monster): """Function to allow for player monster rpg combat. Args: player (:obj:`Player`): The user selected main character. monster (:obj:`Monster`): A character that the player fights. """ # Begin combat dialogue time.sleep(1) scroll_print("\nOh no! {}".format(monster.get_description())) time.sleep(1) scroll_print("\nYou have encountered %s! Let's begin combat..." % (monster.get_name())) time.sleep(1) combat_continues = True while combat_continues: # Print short stats for combat player.print_combat_stats() # Allow the player to choose their move # Output player combat options scroll_print("\nPlease select which move you want: ") player.get_attack_description(player.get_level()) # Get the player's choice attack_choice = input().lower().strip() invalid_choice = True while(invalid_choice): if player.check_invalid_attack(attack_choice, player.get_level()): attack_choice = input().lower().strip() else: invalid_choice = False # Execute the player's attack total_damage = player.execute_attack(attack_choice) # Idenfity the player's attack type attack_type = player.get_attack_type(attack_choice) # Get monster's defensive result, which is a random # number in the range of 0 and the monster's defense # or magic defense depending on the type of the attack if attack_type == 0: total_defense = randint(0, monster.get_defense()) elif attack_type == 1: total_defense = randint(0, monster.get_magic_defense()) else: total_defense = randint(0, (monster.get_defense() + monster.get_magic_defense())) total_damage -= total_defense if total_damage < 1: scroll_print("\nYou missed! ") else: # Deal the damage to the enemy scroll_print("\nYou did %d damage! " % (total_damage)) current_monster_health = monster.get_health() monster.set_health(current_monster_health-total_damage) # Check if the enemy is dead, if so, exit combat and gain experience if monster.get_health() <= 0: time.sleep(1) scroll_print("\nYou have slain %s" % (monster.get_name())) experience_gained = monster.get_loot() scroll_print("You have gained %d experience points!" % (experience_gained)) new_experience_total = experience_gained + player.get_experience() # Level up the player if they have enough experience if new_experience_total >= 10: # we will need to do balancing!!! time.sleep(1) scroll_print("\n%s has leveled up! " % (player.get_name())) new_level = player.get_level() + 1 player.level_up(new_level) # Carry over the excess experience into the new level new_experience_total = new_experience_total - 10 player.set_experience(new_experience_total) combat_continues = False else: # Randomly choose what ability the enemy will use time.sleep(1) attack_type = randint(0, 1) total_damage = monster.npc_attack(attack_type) # Get player's defensive result, which is a random # number in the range of 0 and the player's defense # or magic defense depending on the type of the attack if attack_type == 0: total_defense = randint(0, player.get_defense()) else: total_defense = randint(0, player.get_magic_defense()) total_damage -= total_defense time.sleep(1) # Calculate the damage if total_damage < 1: scroll_print("%s missed! " % (monster.get_name())) time.sleep(1) else: # Deal the damage to the enemy scroll_print("\n%s did %d damage! " % (monster.get_name(), total_damage)) time.sleep(1) current_player_health = player.get_health() player.set_health(current_player_health-total_damage) # Check if the player is dead if player.get_health() <= 0: player.lose_life() scroll_print("\nYou blacked out! ") # Check if the player has any lives left if player.get_lives() <= 0: return False else: time.sleep(1) scroll_print("\nA small pink fairy flies around your " "body... ") scroll_print("You woke up! Your health and magic have " "been restored. ") # Reset the player's health and magic # based on their level player.revive(player.get_level()) # Save the stats from the combat interactions current_room.set_player(player) save_object_state(current_room) return True
def check_invalid_attack(self, attack_choice, level): """Checks if the player selected an invalid attack. Args: attack_choice: The user choice of attack. level: The player's level. """ if level > 4: if attack_choice != 'slash' and attack_choice != 'snare' and \ attack_choice != 'sharpshot' and attack_choice != 'cleave' and \ attack_choice != 'sever': scroll_print("\nYou entered an invalid choice! ") scroll_print("Please enter: Slash, Snare, Sharpshot, " "Cleave or Sever: ") return True elif level > 2: if attack_choice != 'slash' and attack_choice != 'snare' and \ attack_choice != 'sharpshot' and attack_choice != 'cleave': scroll_print("\nYou entered an invalid choice! ") scroll_print("Please enter: Slash, Snare, Sharpshot, or " "Cleave: ") return True else: if attack_choice != 'slash' and attack_choice != 'snare' and \ attack_choice != 'sharpshot': scroll_print("\nYou entered an invalid choice! ") scroll_print("Please enter: Slash, Snare, or Sharpshot: ") return True return False
def print_stats(self): """Prints the players stats.""" scroll_print("Current Stats") scroll_print("Player Name: {}".format(self.name)) scroll_print("Level: {}".format(self.level)) scroll_print("Experience: {}".format(self.experience)) scroll_print("Remaining Lives: {}".format(self.num_lives)) scroll_print("Health: {}".format(self.health)) scroll_print("Magic: {}".format(self.magic)) scroll_print("Attack Power: {}".format(self.attack_power)) scroll_print("Defense: {}".format(self.defense)) scroll_print("Magic Power: {}".format(self.magic_power)) scroll_print("Magic Defense: {}".format(self.magic_defense))
def main(): # Run the starting menu of the game and start, load, or exit the game starting_selection = starting_menu() if starting_selection == "start": # need to add Ranger and Wizard class to initialize character_choice = choose_character() player_name = choose_name(character_choice) # set up the game and use the name chosen by the player for # their character current_room = start_game(player_name, character_choice) player = current_room.get_player() # scroll_print("Initial location: {}".format(current_room.get_name())) # scroll_print out room description. if player.has_memory(current_room.get_name()): scroll_print(current_room.get_short_description()) else: player.add_memory(current_room.get_name()) scroll_print(current_room.get_description()) print_item_descriptions(current_room) save_object_state(current_room) elif starting_selection == "load": current_room = initial_load_game() player = current_room.get_player() if player.has_memory(current_room.get_name()): scroll_print(current_room.get_short_description()) else: scroll_print(current_room.get_description()) print_item_descriptions(current_room) else: scroll_print("\nThank you for playing Nightfall. " "Have a fortuitous evening. \n") exit() while not is_game_over(player): current_room = get_current_room() if len(current_room.get_monsters()) != 0: survival_check = combat(current_room, player, current_room.get_monsters()[0]) if survival_check is True: current_room.monster_killed() else: scroll_print("Oh no! You ran out of lives! ") scroll_print("GAME OVER ") continue scroll_print("\nWhat would you like to do? ") # Grab the command from the user and execute the action if valid user_input = get_input() action = parse_input(user_input, current_room) take_action(current_room, action) current_room = get_current_room() player = current_room.get_player()
def execute_attack(self, option): """Calculates attack damage for user chosen attack. Args: option: The user chosen attack. Returns: int: The player's attack damage that will be done. """ if option == 'slash': # Randomize the damage based on the move and applicable equipment attack_damage = randint(0, self.attack_power) return attack_damage elif option == 'snare': if self.magic < 2: scroll_print("You don't have enough magic! ") attack_damage = 0 else: # Randomize the damage based on the move and # applicable equipment attack_damage = randint(0, self.magic_power) # Adjust the player's stats self.magic -= 2 return attack_damage elif option == 'sharpshot': if self.magic < 3: scroll_print("You don't have enough magic! ") attack_damage = 0 else: # Randomize the damage based on the move and # applicable equipment attack_damage = randint(0, (self.magic_power + self.attack_power)) # Adjust the player's stats self.magic -= 3 return attack_damage elif option == 'cleave': # Randomize the damage based on the move and applicable equipment attack_damage = randint(0, (self.attack_power * 2)) return attack_damage elif option == 'sever': if self.magic < 5: scroll_print("You don't have enough magic! ") attack_damage = 0 else: # Randomize the damage based on the move and # applicable equipment attack_damage = randint(0, (self.magic_power + (self.attack_power * 2))) # Adjust the player's stats self.magic -= 5 return attack_damage