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...
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
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' |
Where I'm falling down is how to store the variable, and continuously update it based on the damage rolls.