Ejemplo n.º 1
0
 def full_info(self):
     "Format everything."
     return "{}, {}\n{}\nInventory:\n{}\nEquipped:\n{}".format(
         self.name,
         self.description,
         self.format_stats(),
         format_objects(self.inventory),
         format_objects(self.equipped),
     )
Ejemplo n.º 2
0
    def play_turn(self):
        "Plays a turn of the game."
        self.turn_count += 1
        print "Turn {0.turn_count}:\n".format(self)

        self.player.health += int(self.player.max_health / 5)
        spaced_print("You healed {} hp.".format(
            int(self.player.max_health / 5)))

        spaced_print("Location:", pre=False)
        print self.current_room.full_info()

        while True:
            main_choice = prompt(self.current_room.valid_options + ['player'])
            # Valid options are:
            #    doors -- poem and regular
            #    items -- regular
            #    monsters -- regular
            #    player -- poem and regular
            #    treasure -- poem
            #    guardian -- poem
            #    charactercs -- regular, but not included.

            # Could be better implemented with a callback pattern but there are
            # time constraints here.
            if main_choice == 'doors':
                spaced_print(format_objects(self.current_room.doors))
                door_choice = prompt(self.current_room.doors.keys() + ['back'])

                if door_choice == 'back':
                    continue

                self.current_room = self.current_room.doors[door_choice]
                break

            elif main_choice == 'items':
                assert_with_dump(self.current_room, BrainRoom,
                                 callback=isinstance)

                spaced_print(format_objects(self.current_room.items))
                item_choice = prompt(
                    bumped_range(len(self.current_room.items)) + ['back'])

                if item_choice == 'back':
                    continue

                item_index = int(item_choice) - 1
                current_item = self.current_room.items[item_index]

                spaced_print(str(current_item))
                item_action = prompt(['info', 'take', 'back'])

                if item_action == 'back':
                    continue
                elif item_action == 'take':
                    self.player.inventory.append(
                        self.current_room.items.pop(item_index))
                    continue
                elif item_action == 'info':
                    spaced_print(current_item.full_info())
                    continue

            elif main_choice == 'player':
                spaced_print(self.player.full_info())
                player_choice = prompt(["inventory", "back"])

                if player_choice == 'back':
                    continue
                elif player_choice == 'inventory':
                    spaced_print("Inventory:\n{}".format(format_objects(
                        self.player.inventory)))
                    inventory_choice = prompt([str(i)
                                               for i in bumped_range(
                                                   len(self.player.inventory))
                                               ] + ['back'])

                    if inventory_choice == 'back':
                        continue

                    inventory_choice = int(inventory_choice) - 1
                    if inventory_choice in range(len(self.player.inventory)):

                        inventory_item = self.player.inventory[
                            inventory_choice]
                        if isinstance(inventory_item, BaseEquipment):
                            spaced_print(inventory_item.full_info())
                            item_choice = prompt(['equip', 'back'])

                            if item_choice == 'back':
                                continue
                            elif item_choice == 'equip':
                                assert_with_dump(self.player.equipped,
                                                 inventory_item.eq_type,
                                                 callback=contains)
                                self.player.equip(inventory_item,
                                                  inventory_item.eq_type)

                        elif isinstance(inventory_item, BaseTreasure):
                            spaced_print(inventory_item.full_info())
                            treasure_choice = prompt(['back'])

                            if treasure_choice == 'back':
                                continue

            elif main_choice == 'monsters':
                assert_with_dump(self.current_room, BrainRoom,
                                 callback=isinstance)

                spaced_print("Monsters:\n{}".format(
                    format_objects(self.current_room.monsters)))
                monster_choice = prompt([str(i)
                                         for i in bumped_range(len(
                                             self.current_room.monsters))] \
                                        + ['back'])

                if monster_choice == 'back':
                    continue

                monster_index = int(monster_choice) - 1
                if monster_index in range(len(
                        self.current_room.monsters)):

                    current_monster = self.current_room.monsters[monster_index]
                    spaced_print(current_monster.full_info())
                    monster_action = prompt(['attack', 'back'])

                    if monster_action == 'back':
                        continue
                    else:
                        try:
                            fight(self.player, current_monster)
                        except MonsterDeadException:
                            spaced_print("The monster dropped:\n\n{}".format(
                                current_monster.drop))
                            self.player.inventory.append(current_monster.drop)
                            del self.current_room.monsters[monster_index]
                        except AbortFightException:
                            spaced_print("You escaped.")

            elif main_choice == 'guardian':
                assert_with_dump(self.current_room, PoemRoom,
                                 callback=isinstance)

                spaced_print("Guardian:\n\n{}".format(
                    self.current_room.guardian.full_info()))
                guardian_choice = prompt(['attack', 'back'])

                if guardian_choice == 'back':
                    continue

                spaced_print("You cannot run from a guardian. You must fight.",
                             pre=False)

                sleep(0.3)

                try:
                    fight(self.player, self.current_room.guardian,
                          monster_name='guardian', can_run=False)
                except MonsterDeadException:
                    if self.current_room.guardian.monster_type == 'FinalBoss':
                        raise FinalBossDeadException

                    spaced_print("The guardian dropped:\n\n{}".format(
                        self.current_room.guardian.drop))
                    self.player.inventory.append(
                        self.current_room.guardian.drop)
                    self.current_room.guardian = None

            elif main_choice == 'treasure':
                if self.current_room.guardian is not None:
                    spaced_print("You must defeat the guardian first.",
                                 pre=False)
                    continue
                spaced_print("Treasure:\n{}".format(
                    str(self.current_room.treasure)))
                treasure_choice = prompt(['take', 'back'])

                if treasure_choice == 'back':
                    continue
                elif treasure_choice == 'take':
                    spaced_print(self.current_room.treasure.full_info())
                    print "You acquired {}".format(
                        self.current_room.treasure.name)
                    self.player.inventory.append(self.current_room.treasure)
                    self.current_room.treasure = None
Ejemplo n.º 3
0
 def pretty_format(self, attribute):
     "Pretty format for an attribute."
     return "{0}:\n{1}".format(attribute.capitalize(), format_objects(getattr(self, attribute)))
Ejemplo n.º 4
0
 def format_stats(self):
     "Formats stats for pretty-printing"
     return "Stats:\n" + format_objects(self.stats)