Python Forum

Full Version: Stacking Problem in a game.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm making a text adventure game as a beginner project and have already run into a problem. I'm making an encounter with an enemy (namely a 'boar') and the damage is doing some weird things.

Here's the code:
import random


def boarenc(weap):
    bhp = 10
    php = 20
    dmg = 0

    if weap == "Fists":
      dmg = 1
    elif weap == "Stick":
        dmg = random.randint(1, 2)
    elif weap == "Sword":
        dmg = random.randint(2, 4)
    elif weap == "Scythe":
        dmg = random.randint(4, 7)

    print("You encounter a boar!")
    while True:
        acti = str(input("Do you ATK or DFND (ATK does the amount of damage your weapon does and DFND reduces incoming damage by 15%: "))
        if acti == "ATK":
            bhp = bhp - dmg
            print(f"The boar now has {bhp} HP!")
        elif acti == "DFND":
            dmg = random.randint(2, 4)
            php = php - (dmg * 15/100)
            print(f"You now have {php} HP!")
            continue

        print("The boar attacks!")
        dmg = random.randint(2, 4)
        php = php - dmg
        print(f"You now have {php} HP.")

        if bhp <= 0:
            print("You win!")
            break
        elif php <= 0:
            print("You lose!")
            break





sti = str(input("""You spawn in a cave and see a stick on the floor, pick it up? (yes or no): """))

if sti == "yes":
    weapon = "Stick"
elif sti == "no":
  weapon = "Fists"

ca = str(input("""You see 2 paths branching of to the left and right,
do you go 'left' or 'right'?: """))

if ca == "right":
  boarenc(weapon)
and the output is this:
You spawn in a cave and see a stick on the floor, pick it up? (yes or no): no
You see 2 paths branching of to the left and right,
do you go 'left' or 'right'?: right
You encounter a boar!
Do you ATK or DFND (ATK does the amount of damage your weapon does and DFND reduces incoming damage by 15%: ATK
The boar now has 9 HP!
The boar attacks!
You now have 17 HP.
Do you ATK or DFND (ATK does the amount of damage your weapon does and DFND reduces incoming damage by 15%: ATK
The boar now has 6 HP!
The boar attacks!
You now have 14 HP.
Do you ATK or DFND (ATK does the amount of damage your weapon does and DFND reduces incoming damage by 15%: ATK
The boar now has 3 HP!
The boar attacks!
You now have 12 HP.
Do you ATK or DFND (ATK does the amount of damage your weapon does and DFND reduces incoming damage by 15%: ATK
The boar now has 1 HP!
The boar attacks!
You now have 8 HP.
Do you ATK or DFND (ATK does the amount of damage your weapon does and DFND reduces incoming damage by 15%: ATK
The boar now has -3 HP!
The boar attacks!
You now have 4 HP.
You win!


As you can see, it does 1 damage at first but then it does 3 and 3 and 2 even though the weapon is "Fists" which only does 1 damage. Sorry if this is a really simple question but I'm a beginner and I can't figure it out.
You set the damage for Fists to be 1 at line 7. And you reduce bhp by it at line 19.

But then at line 23, you change it to a random number between 2 and 4 and the original value is lost. You're using the same variable (dmg) for both player's damage. Probably you need to keep track of it separately.
(Aug-05-2022, 07:55 AM)bowlofred Wrote: [ -> ]You set the damage for Fists to be 1 at line 7. And you reduce bhp by it at line 19.

But then at line 23, you change it to a random number between 2 and 4 and the original value is lost. You're using the same variable (dmg) for both player's damage. Probably you need to keep track of it separately.

Thank you! Really helped!