I’m a fairly new programmer, but I am trying to make a fighting game code, but I have been stumped on one part, within my code I am trying to make sure that when the enemy health = 0, it will end. I can make the program end, well. But I don’t want to enemy health to go under 0, it can work with me fine, but I really want to change this.
JavaScript
x
21
21
1
import random
2
gameactive = 0
3
x= 100
4
5
while gameactive == 0:
6
if x > 0:
7
crit = random.randint(8,9)
8
base = random.randint(5,6)
9
a = random.randint(1,10)
10
if a == 10:
11
x -= crit
12
print("nITS A CRITICAL HIT!") # Dont mind this please, this chunk about critical hits.
13
print("n")
14
print(str(x) + " Health left")
15
else:
16
x -= base
17
print("n")
18
print(str(x) + " Health left")
19
else:
20
break
21
so what happens when the program is ran, it will generate numbers and decreases x using those generated numbers. But when running the program, I want the code to only limit itself 0 health and not go under. Sorry if i horribly explained it simply put, I want a way so x will be capped at 0, and it will not print negative numbers and instead print some sample text like enemy died.
Advertisement
Answer
use max function like t his:
JavaScript
1
20
20
1
import random
2
gameactive = 0
3
x= 100
4
5
while gameactive == 0:
6
if x > 0:
7
crit = random.randint(8,9)
8
base = random.randint(5,6)
9
a = random.randint(1,10)
10
if a == 10:
11
x -= crit
12
print("nITS A CRITICAL HIT!") # Dont mind this please, this chunk about critical hits.
13
else:
14
x -= base
15
print("n")
16
print(str(max(x,0)) + " Health left")
17
18
else:
19
break
20