Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stacking Problem in a game.
#1
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.
Reply
#2
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.
Reply
#3
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with my pong game code Than999 8 3,876 May-15-2022, 06:40 AM
Last Post: deanhystad
  Problem restricting user input in my rock paper scissors game ashergreen 6 4,637 Mar-25-2021, 03:54 AM
Last Post: deanhystad
  Guessing game problem IcodeUser8 7 3,657 Jul-19-2020, 07:37 PM
Last Post: IcodeUser8
  Python Hangman Game - Multiple Letters Problem t0rn 4 4,708 Jun-05-2020, 11:27 AM
Last Post: t0rn
  game of the goose - dice problem koop 4 3,501 Apr-11-2020, 02:48 PM
Last Post: ibreeden
  Image Stacking Help Rhys_SSD 2 2,666 Feb-25-2019, 10:49 AM
Last Post: Rhys_SSD
  Dice Throwing Game in Python (I have a problem) yuseinali 1 3,128 Jun-22-2017, 10:22 PM
Last Post: metulburr
  Stacking generators Ofnuts 2 19,630 Sep-18-2016, 08:36 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020