def get_available_actions(position, player): """Only make valid actions available. Actions are stored in a dictionary""" # store actions in a dictionary actions = OrderedDict() print("Choose an action: ") # print inventory option if there are any items if player.inventory: action_adder(actions, "i", player.print_inventory, "Print Inventory") # print the ship's map if isinstance(position, ship.ViewMapTile): action_adder(actions, "m", position.print_map, "Ship's Map") # add supplies option if there are any supplies left if isinstance(position, ship.SuppliesTile) and position.inventory: action_adder(actions, "s", player.add_supplies, "Add Supplies") # attack and protect options if the enemy is alive elif isinstance(position, ship.EnemyTile) and position.enemy.is_alive(): action_adder(actions, "a", player.attack, "Attack") action_adder(actions, "p", player.protect, "Protection") # option to move to another tile once all other actions are completed else: if ship.tile_at(position.x, position.y - 1): action_adder(actions, "f", player.move_forward, "Go forward") if ship.tile_at(position.x, position.y + 1): action_adder(actions, "a", player.move_aftward, "Go aftward") if ship.tile_at(position.x - 1, position.y): action_adder(actions, "p", player.move_port, "Go portside") if ship.tile_at(position.x + 1, position.y): action_adder(actions, "s", player.move_starboard, "Go starboard side") # healing option if the player has less than 100 HP if player.hp < 100: action_adder(actions, "h", player.heal, "Heal") return actions
def move_player(actions, player, position): """Define player movement dependent on position""" if ship.tile_at(position.x, position.y - 1): return action_adder(actions, "f", player.move_forward, "Go forward") if ship.tile_at(position.x, position.y + 1): return action_adder(actions, "a", player.move_aftward, "Go aftward") if ship.tile_at(position.x - 1, position.y): return action_adder(actions, "p", player.move_port, "Go portside") if ship.tile_at(position.x + 1, position.y): return action_adder(actions, "s", player.move_starboard, "Go starboard side")
def add_supplies(self): """Add supplies to the player's inventory when supplies are found""" # define the position in the ship position = ship.tile_at(self.x, self.y) # add the inventory from the supply tile to the player's inventory current_inventory = self.inventory position.add_inventory(current_inventory)
def attack(self): """Attack the enemy by removing health points""" # always use the best weapon in the inventory best_weapon = self.most_powerful_weapon() # define the enemy's poition in the ship position = ship.tile_at(self.x, self.y) enemy = position.enemy # declare which weapon is used and change the value of the enemy's hp print("You use {} against {}!".format(best_weapon.name, enemy.name)) enemy.hp -= best_weapon.damage # print out if the enemy is alive and how many hps remain if not enemy.is_alive(): print("You killed a {}".format(enemy.name)) else: print("{} HP is {}.".format(enemy.name, enemy.hp))
def protect(self): """Check and use items for protection""" # add protection items from Inventory protection = [ item for item in self.inventory if isinstance(item, items.Protection) ] # print a message if there are no protection items if not protection: print("You do not have any items to protect you!") return position = ship.tile_at(self.x, self.y) enemy = position.enemy # bread = items.CrustyBread() if enemy.name == "Flock of Blue Space Ducks": print("Bread protection value against ducks is -100 Damage") # print out a list of available protection items print("Choose an item to use to protect yourself: ") for i, item in enumerate(protection, 1): print("{}. {}".format(i, item)) # choose a protection item from the list and use it valid = False while not valid: choice = input("") try: to_use = protection[int(choice) - 1] # remove used item from the inventory self.inventory.remove(to_use) if enemy.name == "Flock of Blue Space Ducks": if to_use.name == "Crusty Bread": to_use.protect_value = 100 else: if to_use.name == "Crusty Bread": to_use.protect_value = 0 # decrease damage by using a protection item # limit enemy damage to not drop below 0 enemy.damage = enemy.damage - to_use.protect_value if enemy.damage > 0: return enemy.damage else: enemy.damage = 0 return enemy.damage print("Potential Damage: {}".format(enemy.damage)) valid = True except (ValueError, IndexError): print("Invalid choice, try again.")
def play(): """ Input for character movement and accessing inventory""" print("Escape from the Ship!") ship.parse_ship_dsl() player = Player() # Possible player directions and actions continue as long as the player is # alive and they have not reached the escape pod while player.is_alive() and not player.victory: # define the player's start position position = ship.tile_at(player.x, player.y) # print(player.x, player.y) # print the intro at the start position print(position.intro_text()) # modify health points of player when attacked position.modify_player(player) if player.is_alive() and not player.victory: choose_action(position, player)