Home > Software design >  Loop in function stops randomly
Loop in function stops randomly

Time:03-31

So yea, im making an RPG and the battle isnt too reliable... if you can figure out why then that would be great!

#Enemy System
def MMEnemy(PL, HP, STR, DEF, SLTH, Name, CLASSNO):
    EnemyLvl = random.randint(PL, (PL * 2))
    EnemyHP = random.randint(EnemyLvl * 40, EnemyLvl * 80)
    Defend = False
    while HP >= 1 & EnemyHP >= 1:
        HPPercent = HP / PL
        HPPercent = int(HPPercent)
        if HPPercent >= 90:
            HealthBar = ("█████████▓  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 80 and HPPercent <= 89:
            HealthBar = ("████████▓░  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 70 and HPPercent <= 79:
            HealthBar = ("███████▓░░  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 60 and HPPercent <= 69:
            HealthBar = ("██████▓░░░  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 50 and HPPercent <= 59:
            HealthBar = ("█████▓░░░░  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 40 and HPPercent <= 49:
            HealthBar = ("████▓░░░░░  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 30 and HPPercent <= 39:
            HealthBar = ("███▓░░░░░░  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 20 and HPPercent <= 29:
            HealthBar = ("██▓░░░░░░░  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 10 and HPPercent <= 19:
            HealthBar = ("█▓░░░░░░░░  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 0 and HPPercent <= 9:
            HealthBar = ("▓░░░░░░░░░  |  ", HP, " / ",(100 * PL))
        else:
            HealthBar = "Unknown Health!!"
        print("The ", Name," is still standing...  (", HealthBar, " HP)")
        print("What should you do?")
        print("1: Attack\n2: Heal\n3: Defend\n4: Run")
        choice = input()
        choice = int(choice)
        if choice == 1:
            roll = random.randint(STR, STR   2)
            print("You attack for ", roll)
            EnemyHP -= (roll)
            print(Name, " is at ", EnemyHP)
        elif choice == 2:
            roll = random.randint(PL * -2, PL * 5)
            if CLASSNO == 2:
                roll  = PL * 3
                if roll >= 1:
                    print("You healed for ", roll)
                if roll <= 0:
                    print("You failed your heal [", roll," HP]")
                HP  = roll
        elif choice == 3:
            Defend = True
        elif choice == 4:
            print("Run failed, not implemented yet")
        else:
            print("None were chosen, you stood still")
        DamageTaken = random.randint(5, EnemyLvl * 8)
        if Defend == True:
            prin("Your Defend was successful")
            Defend = False
        else:
            HP -= DamageTaken
            DamageTaken = str(DamageTaken)
            print("You got damaged for ", DamageTaken,"!")
        if HP <= 0:
            print("You have been defeated by the ", Name,"...")
            return "Lose"
        if EnemyHP <= 0:
            print("You defeated ", Name,"!")
            return "Win"
CLASSNO = 1
level = 1
Strength = 5
Defence = 5
Stealth = 5
HP = 100
GameEnd = False
battle = MMEnemy(level, HP, Strength, Defence, Stealth, "Irc", CLASSNO)
battle = str(battle)
if battle != "Win" or battle != "Lose":
    while battle != "Win" or battle != "Lose":
        print("Restarted")
        battle = MMEnemy(level, HP, Strength, Defence, Stealth, "Irc", CLASSNO)
        battle = str(battle)
        if battle == "Win":
            print("Battle Won")
            GameEnd = True
        elif battle == "Lose":
            print("Battle lost")
            GameEnd = True
        else:
            print("Nothing Worked")

Ive tried changing the flags for the loop and simplifying it but it doesnt seem to do much. Its supposed to load and put you into battle but it stops halfway without returning anything making it so it wont get out of the loop

CodePudding user response:

Wanted to suggest an improvement to the code, and was having trouble posting the code in the comments, so here it is:

hp_string = ""
if HPPercent > 0:
    full_hp = "█"
    current_hp = "▓"
    used_hp = "░"
    for i in range(int((HPPercent-HPPercent % 10)/10)):
        hp_string  = full_hp
    hp_string  = current_hp
    if HPPercent <= 90:
        for i in range(9 - int((HPPercent-HPPercent % 10)/10)):
            hp_string  = used_hp
    HealthBar = (hp_string   " |  ", HP, " / ",(100 * PL))
else:
    HealthBar = "Unknown Health!!"

Also, as someone mentioned in the comments, & is not the same as and in python.

  • Related