title: Text-Based Battle mechanics - Python
body: <p>Recently, my friend and I began working on a text-based battle sequence (in console) on Python for a fantasy style game.</p>
  <p><strong>Overview</strong></p>
  <p>The way it works is that the user is presented with: the current situation of their Attack (Att) + HP (HP), and whoever has the higher Intellect (Inl) will go first. If the user goes first, they get given three options - attack, scare away and any other move. in the example I've used 'Bide your time'.</p>
  <p>The opponent (who is not user controlled) then randomly picks between any 3 of their moves,with varying chances for it, and then carries it out.It should then switch back to the users turn, now regardless of whether one has a higher Intellect than the other.</p>
  <p>This should continue alternating until one of the HPs are = 0. If the user defeats the opponent, then it should have the choice of absorbing one of the opponent's stats/attributes.</p>
  <p>In the code, the 'battle' variable depends whether the battle is initiated or not. If the 'battle' is 1, the loop should continue, but if it is 0, then it should stop (the 0 is added after either you scare it away, or</p>
  <p><strong>The Problem:</strong></p>
  <p>Whenever I run the code, it just continues the loop regardless to anything that happens, and is continuously my turn.</p>
  <ul>
  <li>Have I misunderstood how while loops actually work?</li>
  </ul>
  <p><strong>Future Plans</strong></p>
  <ul>
  <li><p>I am planning to add different races (i.e dwarves, elves, orcs etc.) that will change the users HP, Attack and Intellect in a balanced way.</p>
  </li>
  <li><p>I am also planning to add classes, which will eventually replace the 'Bide your time' move with a class specific move.</p>
  </li>
  </ul>
  <p>Thanks for reading and in advance for any suggestions/fixes you make!</p>
  <pre><code>import random
  import time
  
  Att = 10
  Inl = 5
  HP = 100
  
  OpAtt = 10
  OpInl = 4
  OpHP = 100
  
  FirstMove = 1
  OpFirstMove = 0
  
  battle = 1
  
  while battle == 1:
      if Inl &gt; OpInl:
          FirstMove == 1
          OpFirstMove == 0
      else:
          FirstMove == 0
          OpFirstMove == 1
      if FirstMove &gt; OpFirstMove:
          print(&quot;Your Current stats are:&quot;)
          print(&quot;Attack:&quot;, Att)
          print(&quot;HP:&quot;, HP)
          time.sleep(2)
          print(&quot;Your Opponent's current stats are:&quot;)
          print(&quot;Attack:&quot;, OpAtt)
          print(&quot;HP:&quot;, OpHP)
          time.sleep(2)
          print(&quot;Your turn!&quot;)
          time.sleep(1)
      else:
          print(&quot;Your Current stats are:&quot;)
          print(&quot;Attack:&quot;, Att)
          print(&quot;HP:&quot;, HP)
          time.sleep(2)
          print(&quot;Your Opponent's current stats are:&quot;)
          print(&quot;Attack:&quot;, OpAtt)
          print(&quot;HP:&quot;, OpHP)
          time.sleep(2)
          print(&quot;Opponent's turn!&quot;)
          time.sleep(1)
      if FirstMove &gt; OpFirstMove:
          print(&quot;Select a move:&quot;)
          Move1 = str(input(print(&quot;[A] Attack opponent\n[B] Bide your time\n[C] Try to scare your opponent off&quot;)))
          if Move1 == 'A' or Move1 == 'a':
              Hit1 = random.randint(1, 4)
              if Hit1 == 4 or 3 or 2:
                  damageRand = random.randint(8, 12)
                  OpHP = OpHP - damageRand
                  print(&quot;Success!, Opponet's HP is now&quot;, OpHP, &quot;.&quot;)
                  FirstMove == 0
                  OpFirstMove == 1
              else:
                  print(&quot;Failure, no damage dealt.&quot;)
                  FirstMove == 0
                  OpFirstMove == 1
          elif Move1 == 'b' or Move1 == &quot;B&quot;:
              print(&quot;You do nothing&quot;)
              FirstMove == 0
              OpFirstMove == 1
          elif Move1 == &quot;c&quot; or &quot;C&quot;:
              Scare1 = random.randint(1, 7)
              if OpHP &gt; 71:
                  if Scare1 == 7 or 6:
                      print(&quot;Congratulations!, you scared the opponent away, however, this means you gained no loot.&quot;)
                      battle == 0
              elif OpHP &lt; 70:
                  if Scare1 == 7 or 6 or 5 or 4:
                      print(&quot;Congratulations!, you scared the opponent away, however, this means you gained no loot.&quot;)
                      battle == 0
              else:
                  print(&quot;Failure, the opponent stands his ground&quot;)
                  FirstMove == 0
                  OpFirstMove == 1
      if OpFirstMove &gt; FirstMove:
          OpMove1 = random.randint(1, 8)
          if OpMove1 == 1 or OpMove1 == 2 or OpMove1 == 3 or OpMove1 == 4 or OpMove1 == 5:
              OpHit1 = random.randint(1, 4)
              if OpHit1 == 4 or 3 or 2:
                  damageRand = random.randint(8, 12)
                  HP = HP - damageRand
                  print(&quot;Your Opponent Attacks!, Your HP is now&quot;, HP, &quot;.&quot;)
                  FirstMove = 1
                  OpFirstMove = 0
              else:
                  print(&quot;Your Opponent failed to attack you, no damage received.&quot;)
                  FirstMove = 1
                  OpFirstMove = 0
          elif OpMove1 == 6:
              print(&quot;Your Opponent does nothing&quot;)
              FirstMove = 1
              OpFirstMove = 0
          elif OpMove1 == 7 or OpMove1 == 8:
              OpScare1 = random.randint(1, 7)
              if HP &gt; 71:
                  if OpScare1 == 7 or 6:
                      print(&quot;Aargh!, you were scared away, don't worry though, this has no bad affects.&quot;)
                      battle == 0
              elif HP &lt; 70:
                  if OpScare1 == 7 or 6 or 5 or 4:
                      print(&quot;Aargh!, you were scared away, don't worry though, this has no bad affects.&quot;)
                      battle == 0
              else:
                  print(&quot;Your Opponent tried to scare you away, but failed&quot;)
                  FirstMove = 1
                  OpFirstMove = 0
  else:
      print(&quot;Pies are tasty&quot;)
  
  
  if OpHP &lt; 1:
          print (&quot;Congratulations! You deafeated your Opponent, which one of their stats would you like to absorb?&quot;)
          print(&quot;Your Opponent's current stats are:&quot;)
          print(&quot;Attack:&quot;, OpAtt)
          print(&quot;HP:&quot;, OpHP)
          print (&quot;Intellect:&quot;, OpInl)
          absorb1 = print (str(input(&quot;[A] Intellect\n[B] Attack\n[C] HP\n[D] None of the above&quot;)))
          if absorb1 == &quot;a&quot; or absorb1 == &quot;A&quot;:
              Inl == OpInl
              print (&quot;Opponent's intellect absorbed&quot;)
          elif absorb1 == &quot;b&quot; or absorb1 == &quot;B&quot;:
              Att == OpAtt
              print (&quot;Opponent's Attack absorbed&quot;)
          elif absorb1 == &quot;c&quot; or absorb1 == &quot;C&quot;:
              HP == OpHP
              print (&quot;Opponent's HP absorbed&quot;)
          elif (absorb1 == &quot;d&quot; or absorb1 == &quot;D&quot;):
              print (&quot;None of your Opponent's stats absorbed&quot;)
          print(&quot;Your current stats are:&quot;)
          print(&quot;Attack:&quot;, Att)
          print(&quot;HP:&quot;, HP)
          print (&quot;Intellect:&quot;, Inl)
  elif HP &lt; 1:
      print (&quot;Oh no! You were defeated by your Opponent!&quot;)
      battle == 0
  </code></pre>