As part of a school assignment, I have been writing an algorithm where 2 fighters are given a health and power value, and fight using randomly generated numbers, which are increased by their own power value, and decreased by the enemy's power. For some reason, the health of an enemy will go up at random. I have no clue why.
import random
import time
HealthA = 0
HealthB = 0
PowerA = 0
PowerB = 0
print("A battle is occuring!")
HealthA = int(input("What is fighter A's health?"))
HealthB = int(input("What is fighter B's health?"))
PowerA = int(input("What is Fighter A's attack/defense bonus?"))
PowerB = int(input("What is Fighter B's attack/defense bonus?"))
def AttackA(HealthA,HealthB,PowerA,PowerB):
Aroll = random.randint(1,12)
Adamage = Aroll (PowerA-PowerB)
print("Rolling damage for fighter A...")
time.sleep(1)
print("The damage roll was",Aroll)
time.sleep(1)
print("Calculating final damage...")
time.sleep(1)
print("Fighter A dealt",Adamage,"damage to Fighter B! Fighter B's defence stat negated ",PowerB,"damage.")
time.sleep(1)
HealthB-=Adamage
print("Fighter B has",HealthB,"health remaining")
Broll = random.randint(1,12)
Bdamage = Broll (PowerB-PowerA)
print("Rolling damage for fighter B...")
time.sleep(1)
print("The damage roll was",Broll)
time.sleep(1)
print("Calculating final damage...")
time.sleep(1)
print("Fighter B dealt",Bdamage,"damage to Fighter A! Fighter A's defence stat negated ",PowerB,"damage.")
time.sleep(1)
HealthA-=Bdamage
print("Fighter A has",HealthA,"health remaining")
time.sleep(1)
while HealthA>0 and HealthB>0:
AttackA(HealthA,HealthB,PowerA,PowerB)
CodePudding user response:
the values that you are changing inside the function dont get changed outside the function.
return the values at the end of the function like this:
return HealthA, HealthB, PowerA, PowerB
and put them in front of where you call the function
HealthA, HealthB, PowerA, PowerB = AttackA(HealthA,HealthB,PowerA,PowerB)
CodePudding user response:
your problem is in these two lines
Aroll = random.randint(1,12)
Adamage = Aroll (PowerA-PowerB)
and it's not about programming it's about math
in fact Adamage
== (Aroll
PowerA
)-PowerB
so whenever PowerB
(inputed by user) is greater than (Aroll
PowerA
) your Adamage
variable is negative number and that means player A doesn't decrease enemy's health but increase it.