Battle logic not taking away or printing enemy health

In my repl, the character attacks the enemy in the encounter_monster function, but the monster doesn’t have any health that is subtracted from the total health. How do I fix this?

Here is a picture of the function:

def encounter_monster(location, player):
    monsterHP = goblin.max_hp
    print("A monster has appeared.")
    fight = input("Do you wish to fight or run? ")
    if fight.lower() == "f" or fight.lower() == "fight":
        print(f"The monster has {monsterHP} HP")
        while monsterHP > 0 and player.hp > 0:
            battlechoice = input("What would you like to do? (Attack, Heal, Run) ")
            if battlechoice.lower() == "attack" or battlechoice.lower() == "a":
                damage = player.attack + 5
                hit_chance = random.randint(1, 10) <= player.intelligence
                if hit_chance:
                    damage_dealt = max(0, damage - goblin.defense)
                    monsterHP -= damage_dealt
                    print("Your attack hit! You hit the monster.")
                    print(f"You dealt {damage_dealt} damage. The monster's HP: {monsterHP}")
                else:
                    print("Your attack missed! The monster evaded your attack")
            elif battlechoice.lower() == "heal" or battlechoice.lower() == "h":
                if player.role == "Trader":
                    player.heal(15)
                    print("You healed 15 health.")
                else:
                    player.heal(10)
                    print("You healed 10 health.")
            elif battlechoice.lower() == "run" or battlechoice.lower() == "r":
                print("You got away safely. However, you gained 0 points because you did not defeat the monster.")
                locationtext(location, player)
                break
            else:
                print("Invalid choice. Please try again.")

            if monsterHP <= 0:
                print("Congratulations! You defeated the monster!")
                goblin.hp = monsterHP  # Update goblin's HP
                break

            monster_attack(player)  # Pass the player object to monster_attack function
    elif fight.lower() == "r" or fight.lower() == "run":
        print("You got away safely. However, you gained 0 points because you did not defeat the monster.")

Repl:
https://replit.com/@DougieEarl/Adventure-Game#main.py

code:

def encounter_monster(location, player):
    monsterHP = goblin.max_hp
    print("A monster has appeared.")
    fight = input("Do you wish to fight or run? ")
    if fight.lower() == "f" or fight.lower() == "fight":
        print(f"The monster has {monsterHP} HP")
        while monsterHP > 0 and player.hp > 0:
            battlechoice = input("What would you like to do? (Attack, Heal, Run) ")
            if battlechoice.lower() == "attack" or battlechoice.lower() == "a":
                damage = player.attack + 5
                hit_chance = random.randint(1, 10) <= player.intelligence
                if hit_chance:
                    damage_dealt = max(0, damage - goblin.defense)
                    monsterHP -= damage_dealt
                    print("Your attack hit! You hit the monster.")
                    print(f"You dealt {damage_dealt} damage. The monster's HP: {monsterHP}")
                else:
                    print("Your attack missed! The monster evaded your attack")
            elif battlechoice.lower() == "heal" or battlechoice.lower() == "h":
                if player.role == "Trader":
                    player.heal(15)
                    print("You healed 15 health.")
                else:
                    player.heal(10)
                    print("You healed 10 health.")
            elif battlechoice.lower() == "run" or battlechoice.lower() == "r":
                print("You got away safely. However, you gained 0 points because you did not defeat the monster.")
                locationtext(location, player)
                break
            else:
                print("Invalid choice. Please try again.")

            if monsterHP <= 0:
                print("Congratulations! You defeated the monster!")
                goblin.hp = monsterHP  # Update goblin's HP
                break

            monster_attack(player)  # Pass the player object to monster_attack function
    elif fight.lower() == "r" or fight.lower() == "run":
        print("You got away safely. However, you gained 0 points because you did not defeat the monster.")

What does this report when actually running?

2 Likes

It doesn’t show up. The monster just takes its turn

To print the enemy health just use the print function, you can use this when you hit the monster. For example:

if hit_chance:
    damage_dealt = max(0, damage - goblin.defense)
    goblin.hp -= damage_dealt #considering that you will use goblin.hp
    print("Your attack hit! You hit the monster.")
    print(f"You dealt {damage_dealt} damage. The goblin's current HP is: {goblin.hp}")

It still doesn’t take away the health or print “Your attack hit!”

Have any other ideas?

You still didn’t change the monsterHP as I said.

Your code will look like this:

#ENCOUNTER MONSTER ------------------------
def encounter_monster(location, player):
    print("A monster has appeared.")
    fight = input("Do you wish to fight or run? ")
    if fight.lower() == "f" or fight.lower() == "fight":
        print(f"The monster has {goblin.hp} HP")
        while goblin.hp > 0:
            battlechoice = input("What would you like to do? (Attack, Heal, Run) ")
            if battlechoice.lower() == "a" or battlechoice.lower() == "attack":
                if characterchosen == "Swordsman":
                    damage = swordsman.attack + 5
                    hit_chance = random.randint(1, 10) <= swordsman.intelligence
                    if hit_chance:
                        goblin.hp -= damage
                        print("Your attack hit! You hit the monster with your sword")
                    else:
                        print("Your attack missed! The monster dodged your attack")
                elif characterchosen == "Trader":
                    damage = trader.attack + 5
                    hit_chance = random.randint(1, 10) <= trader.intelligence
                    if hit_chance:
                        goblin.hp -= damage
                        print("Your attack hit! You threw")
                    else:
                        print("Your attack missed! The monster evaded your attack")
                elif characterchosen == "Guard":
                    damage = guard.attack + 5
                    hit_chance = random.randint(1, 10) <= guard.intelligence
                    if hit_chance:
                        goblin.hp -= damage
                        print("Your attack hit! It works - guard")
                    else:
                        print("Your attack missed! The monster blocked your attack")
                print(f"The monster now has {goblin.hp} HP")
            if goblin.hp <= 0:
                break
        print("You beat the monster!")
    else:
        print("You are a scaredy cat and you ran away.")
        print("You ran to your home and grabbed a chip. You regained 1 HP")

I did change the code you asked

What do you mean??? I wasn’t working on the other files at all. I deleted them and the game still is the same.

Wait, I noticed the problem now.

Your variable characterchosen is not defined globally.
Also, its value does not correspond to the role chosen by the player.
You have to make characterchosen a global variable and use it inside the getrole() function to save the chosen role.
And also use it inside the encounter_monster() function to decide which character’s attributes to use in the fight.

For example:

global characterchosen  # You declare the variable as global

def getrole():
    time.sleep(1)
    print("First, you need to choose a character type! ")
    time.sleep(1)
    print(Fore.WHITE + 'You can be:')
    time.sleep(1)
    print("A Swordsman, a Trader, or a Guard")
    time.sleep(1)
    print("Type h to find out what each role does.")

    while True:
        rolequestion = input("Which role do you want? ")
        if rolequestion.lower() == 'swordsman' or rolequestion.lower() == 's':
            print("Your stats are: Str: 7 Def: 7 HP: 60 Intell: 7 ")
            characterchosen = "Swordsman"
            break
        elif rolequestion.lower() == 'trader' or rolequestion.lower() == 't':
            print("Your stats are: Str: 5 Def: 6 HP: 40 Intell: 10 ")
            characterchosen = "Trader"
            break
        elif rolequestion.lower() == 'guard' or rolequestion.lower() == 'g':
            print("Your stats are: Str: 9 Def: 5 HP: 80 Intell: 2 ")
            characterchosen = "Guard"
            break
        elif rolequestion.lower() == "h":
            print("Swordsmen have moderate health, intelligence, and defense, but have above-average strength.")
            print("Guards have high attack and health, but low intelligence and defense.")
            print("Traders have low health and attack, but have high intelligence and defense.")
        else:
            print("That is not an available role. Please try again.")

Also change your

player_role = rolequestion

To

player_role = characterchosen

That way you ensure that the characterchosen variable is passed to the Player object upon creation.

2 Likes

Even better try to use classes and get rid of global. They are truly a pain in writing and reading code

3 Likes

I’ll try that but I can’t now

Alright that worked, but now the monster doesn’t have a turn

Nevermind! I fixed it. Thanks

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.