0

Recently, my friend and I began working on a text-based battle sequence (in console) on Python for a fantasy style game.

Overview

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'.

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.

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.

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

The Problem:

Whenever I run the code, it just continues the loop regardless to anything that happens, and is continuously my turn.

  • Have I misunderstood how while loops actually work?

Future Plans

  • 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.

  • I am also planning to add classes, which will eventually replace the 'Bide your time' move with a class specific move.

Thanks for reading and in advance for any suggestions/fixes you make!

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 > OpInl:
        FirstMove == 1
        OpFirstMove == 0
    else:
        FirstMove == 0
        OpFirstMove == 1
    if FirstMove > OpFirstMove:
        print("Your Current stats are:")
        print("Attack:", Att)
        print("HP:", HP)
        time.sleep(2)
        print("Your Opponent's current stats are:")
        print("Attack:", OpAtt)
        print("HP:", OpHP)
        time.sleep(2)
        print("Your turn!")
        time.sleep(1)
    else:
        print("Your Current stats are:")
        print("Attack:", Att)
        print("HP:", HP)
        time.sleep(2)
        print("Your Opponent's current stats are:")
        print("Attack:", OpAtt)
        print("HP:", OpHP)
        time.sleep(2)
        print("Opponent's turn!")
        time.sleep(1)
    if FirstMove > OpFirstMove:
        print("Select a move:")
        Move1 = str(input(print("[A] Attack opponent\n[B] Bide your time\n[C] Try to scare your opponent off")))
        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("Success!, Opponet's HP is now", OpHP, ".")
                FirstMove == 0
                OpFirstMove == 1
            else:
                print("Failure, no damage dealt.")
                FirstMove == 0
                OpFirstMove == 1
        elif Move1 == 'b' or Move1 == "B":
            print("You do nothing")
            FirstMove == 0
            OpFirstMove == 1
        elif Move1 == "c" or "C":
            Scare1 = random.randint(1, 7)
            if OpHP > 71:
                if Scare1 == 7 or 6:
                    print("Congratulations!, you scared the opponent away, however, this means you gained no loot.")
                    battle == 0
            elif OpHP < 70:
                if Scare1 == 7 or 6 or 5 or 4:
                    print("Congratulations!, you scared the opponent away, however, this means you gained no loot.")
                    battle == 0
            else:
                print("Failure, the opponent stands his ground")
                FirstMove == 0
                OpFirstMove == 1
    if OpFirstMove > 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("Your Opponent Attacks!, Your HP is now", HP, ".")
                FirstMove = 1
                OpFirstMove = 0
            else:
                print("Your Opponent failed to attack you, no damage received.")
                FirstMove = 1
                OpFirstMove = 0
        elif OpMove1 == 6:
            print("Your Opponent does nothing")
            FirstMove = 1
            OpFirstMove = 0
        elif OpMove1 == 7 or OpMove1 == 8:
            OpScare1 = random.randint(1, 7)
            if HP > 71:
                if OpScare1 == 7 or 6:
                    print("Aargh!, you were scared away, don't worry though, this has no bad affects.")
                    battle == 0
            elif HP < 70:
                if OpScare1 == 7 or 6 or 5 or 4:
                    print("Aargh!, you were scared away, don't worry though, this has no bad affects.")
                    battle == 0
            else:
                print("Your Opponent tried to scare you away, but failed")
                FirstMove = 1
                OpFirstMove = 0
else:
    print("Pies are tasty")


if OpHP < 1:
        print ("Congratulations! You deafeated your Opponent, which one of their stats would you like to absorb?")
        print("Your Opponent's current stats are:")
        print("Attack:", OpAtt)
        print("HP:", OpHP)
        print ("Intellect:", OpInl)
        absorb1 = print (str(input("[A] Intellect\n[B] Attack\n[C] HP\n[D] None of the above")))
        if absorb1 == "a" or absorb1 == "A":
            Inl == OpInl
            print ("Opponent's intellect absorbed")
        elif absorb1 == "b" or absorb1 == "B":
            Att == OpAtt
            print ("Opponent's Attack absorbed")
        elif absorb1 == "c" or absorb1 == "C":
            HP == OpHP
            print ("Opponent's HP absorbed")
        elif (absorb1 == "d" or absorb1 == "D"):
            print ("None of your Opponent's stats absorbed")
        print("Your current stats are:")
        print("Attack:", Att)
        print("HP:", HP)
        print ("Intellect:", Inl)
elif HP < 1:
    print ("Oh no! You were defeated by your Opponent!")
    battle == 0
Community
  • 1
  • 1
Necrolio
  • 11
  • 1
  • 1
    Please see [How to ask a good question](http://stackoverflow.com/help/how-to-ask), and [Creating a **M**inimal, **C**omplete, and **V**erifiable **E**xample](http://stackoverflow.com/help/mcve). Generally speaking, questions that include code should be narrowed down to contain only the smallest amount of code needed to reproduce a problem -- so, say, the specific function (or few lines from that function) that's working improperly, and the smallest amount of support code needed to make it run. – Charles Duffy Nov 30 '16 at 19:17
  • `battle == 0` checks if `battle` is zero. `battle = 0` assigns the value of `battle` to zero – Patrick Haugh Nov 30 '16 at 19:17
  • You don't put print statements in `input()`. Not sure if this is what's wrong, but it isn't right... – Elliot Roberts Nov 30 '16 at 19:21

2 Answers2

0

The main problem that I see is that when you say battle == 0 you are not assigning 0 to the variable battle. You are testing if battle equals 0. So, for example, when you say:

            if OpScare1 == 7 or 6 or 5 or 4:
                print("Aargh!, you were scared away, don't worry though, this has no bad affects.")
                battle == 0

at the end you are not changing the value of battle you are just randomly checking if it equals 0. You need to change all of the ones where you are trying to assign a variable to battle = 0. There are many many of these examples.

However when you say OpScare1 == 7 that is correct because that is in an if statement where you are testing the condition.

rassar
  • 5,412
  • 3
  • 25
  • 41
0

It is always your turn because the code does not change any variables it only checks if they are true.

    if Inl > OpInl:
        FirstMove == 1
        OpFirstMove == 0
    else:
        FirstMove == 0
        OpFirstMove == 1

here if Inl is bigger than OpInl than it doesnt change FirstMove to 1, it only checks if FirstMove is equal to 1. if Inl is 1 and OpInl is 0 then it checks that FirstMove is 1 and that OpFirstMove is 0. it doesnt change these values.

        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("Success!, Opponet's HP is now", OpHP, ".")
                FirstMove == 0
                OpFirstMove == 1
            else:
                print("Failure, no damage dealt.")
                FirstMove == 0
                OpFirstMove == 1
        elif Move1 == 'b' or Move1 == "B":
            print("You do nothing")
            FirstMove == 0
            OpFirstMove == 1
        elif Move1 == "c" or "C":
            Scare1 = random.randint(1, 7)
            if OpHP > 71:
                if Scare1 == 7 or 6:
                    print("Congratulations!, you scared the opponent away, however, this means you gained no loot.")
                    battle == 0
            elif OpHP < 70:
                if Scare1 == 7 or 6 or 5 or 4:
                    print("Congratulations!, you scared the opponent away, however, this means you gained no loot.")
                    battle == 0
            else:
                print("Failure, the opponent stands his ground")
                FirstMove == 0
                OpFirstMove == 1  

Same goes here because it doesnt change FirstMove and doesnt change OpFirstMove. it just checks if it is true. So all that those FirstMove == 0, OpFirstMove == 1 and vice versa do is check instead of changing same applies to the battle == 0 variable. it checks but doesnt change.

The only place that those checks should happen is in if and while statements because then you are actually checking if that condition is true.