Example #1
0
def combat(player: Player, monster: Monster) -> str:
    companion = player.monster

    clear_screen()
    print(f'\n{"BATTLE!":^70}')

    battle = True

    while battle:
        companion_hp = f"{companion.current_hp} / {companion.hp}"
        companion_element = f"{companion.element} ({companion.rarity})"
        monster_hp = f"{monster.current_hp} / {monster.hp}"
        monster_element = f"{monster.element} ({monster.rarity})"

        monster_display = (
            f'{indent:<10}{"Your Monster":^20}{" ":^10}{"Wild Monster":^20}{indent:>10}\n'
            f'{"-" * 50:^70}\n'
            f"{indent:<10}{companion.name:<25}{monster.name:>25}{indent:>10}\n"
            f'{indent:<10}{companion_element:<20}{"Element":^10}{monster_element:>20}{indent:>10}\n'
            f'{indent:<10}{companion.level:<20}{"Level":^10}{monster.level:>20}{indent:>10}\n'
            f"\n"
            f'{indent:<10}{companion_hp:<20}{"Health":^10}{monster_hp:>20}{indent:>10}\n'
        )

        print()
        print(monster_display)

        response = None

        while not response in ["f", "h", "r"]:
            try:
                response = input(
                    f'Would you like to (F)ight{", (H)eal," if player.blessing > datetime.now() else ""} or (R)un? '
                )
                response = response.lower()

            except KeyboardInterrupt:
                sys.exit()

            except:
                pass

        print()

        # Handle player's turn
        if response == "f":
            damage = companion.attack_target(monster)

            if damage == 0:
                print(f"Your {companion.name} missed the {monster.name}.")
            else:
                print(
                    f"Your {companion.name} hit the {monster.name} for {damage} damage",
                    end="",
                )

                if monster.damage_taken(damage) == 0:
                    print(f" and defeated it!")

                    response = None

                    while not response in ["y", "n"]:
                        try:
                            response = input(
                                f"\nWould you like to capture this monster to replace your {companion.name}? (Y/N) "
                            )
                            response = response.lower()

                        except KeyboardInterrupt:
                            sys.exit()

                        except:
                            pass

                        if response == "y":
                            player.monster = monster
                            print(
                                f"Let's head back to town with your new {player.monster.name} for some rest."
                            )

                            return "Captured"

                    if player.monster.gain_xp(5):
                        print(
                            f"\nCongratulations! Your monster has grown stronger!"
                        )
                        player.save_player()

                    return "Won"
                else:
                    print(".")

        if response == "h":
            if player.blessing > datetime.now():
                healing = random.randint(1, 3)
                print(
                    f"You healed your {companion.name} for {healing} health.")

                companion.healing(healing)

        if response == "r":
            # Monster gets a free hit on the way out
            print(f"You attempt to run away but the {monster.name} charges!")
            battle = False

        # Handle monster's attack
        damage = monster.attack_target(companion)

        if damage == 0:
            print(f"The {monster.name} missed your {companion.name}.")
        else:
            print(
                f"The {monster.name} hit your {companion.name} for {damage} damage",
                end="",
            )

            if companion.damage_taken(damage) == 0:
                # Player lost so we need to head back to town
                print(f" and knocked it out!")
                print("Let's head back to town...\n")

                return "Lost"
            else:
                print(".")

    return "Fled"