May-06-2020, 05:31 PM
Hi...I'm a noob teaching myself python. I've got a simple turn based RPG program. Where I'm running into trouble is with updating the character hit points (warrior_hp). I'm trying to get the attack and damage functions to loop back and forth until one of the characters hit point counters hit zero...
Where I'm falling down is how to store the variable, and continuously update it based on the damage rolls.
class OrcBattle(Scene): # Roll for initiative. def enter(self): print(dedent("Let's first roll for initiative...")) min = 1 max = 20 warrior_hp = 25 orc_hp = 16 initiative_roll = random.randint(min, max) if initiative_roll <= 10: print(dedent(f"""You rolled a {initiative_roll} and your initiative fails...The orc hears your footfall on some loose pebbles. He turns around and is now fully aware of you. He snarls and hefts an axe in both hands. You draw your sword and now it's time to roll for turns... """)) OrcBattle.turns(self) else: print(dedent(f"""You rolled a {initiative_roll} which means that you have the element of surprise....You quietly draw your sword and swing at the back of the orc's head! """)) OrcBattle.warrior_attack(self) # If initiative fails, roll to take turns. def turns(self): min = 1 max = 20 turn_roll = random.randint(min, max) print(turn_roll) if turn_roll >= 10: print(dedent(f"You rolled a {turn_roll} therefor you go first!")) OrcBattle.warrior_attack(self) else: print(dedent(f"""You rolled a {turn_roll} therefor the orc gets the first attack. """)) OrcBattle.orc_attack(self) # Method for if you win the turn roll or win initiative. def warrior_attack(self): min = 1 max = 20 print("You swing your sword at the Orc!") turn_roll = random.randint(min, max) print(turn_roll) if turn_roll <= 10: print (dedent(f"""You rolled a {turn_roll} You missed! Now the Orc attacks... """)) OrcBattle.orc_attack(self) else: print(f"You rolled a {turn_roll} that's a hit.") OrcBattle.damage_roll_warrior(self) # Method for everytime it's the Orc's turn to attack. def orc_attack(self): min = 1 max = 20 print("The orc swings its ax") turn_roll = random.randint(min, max) print(turn_roll) if turn_roll <= 10: print(dedent(f"""The orc rolled a {turn_roll}. That's a miss! It's your turn.""")) OrcBattle.warrior_attack(self) else: print(dedent(f"""The orc rolled a {turn_roll}! That's a hit. Lets roll for damage.""")) OrcBattle.damage_roll_warrior(self) def damage_roll_warrior(self): warrior_hp = 25 min = 1 max = 20 if warrior_hp >= 0: die_roll = random.randint(min, max) print(die_roll) damage = warrior_hp - die_roll warrior_hp = damage print(dedent(f""" You rolled {die_roll} points of damage. that leaves you with {warrior_hp} hit points left. """)) OrcBattle.orc_attack(self) else: Print("You are dead!") return 'death' def damage_roll_orc(self): orc_hp = 16 min = 1 max = 20 if orc_hp >= 1: die_roll = random.randint(min, max) print(die_roll) damage = orc_hp - die_roll orc_hp = damage print(dedent(f""" The orc rolled {die_roll} points of damage. That leaves the orc with {orc_hp} hit points left. """)) OrcBattle.warrior_attack(self) else: print(dedent(""" You have slain the orc! As it's black blood begins to pool under it's corpse you see that the only exit is the way you came. It looks like your only choice is not the South Tunnel. """)) return 'south_tunnel'I'm getting a "RecursionError: maximum recursion depth exceeded while calling a Python object". Because the hit point variables never update, the functions are essentially looping back and forth.
Where I'm falling down is how to store the variable, and continuously update it based on the damage rolls.