我是一个相当新的程序员,但我试图做一个战斗游戏代码,但我已经在一个部分,在我的代码中,我试图确保当敌人的健康= 0,它将结束。我可以让节目结束,好吧。但是我不想把敌人的健康降到0以下,它可以和我一起工作很好,但是我真的想改变这一点。
import random
gameactive = 0
x= 100
while gameactive == 0:
if x > 0:
crit = random.randint(8,9)
base = random.randint(5,6)
a = random.randint(1,10)
if a == 10:
x -= crit
print("\nITS A CRITICAL HIT!") # Dont mind this please, this chunk about critical hits.
print("\n")
print(str(x) + " Health left")
else:
x -= base
print("\n")
print(str(x) + " Health left")
else:
break所以当程序运行时会发生什么,它将生成数字,并使用这些生成的数字减少x。但是,在运行程序时,我希望代码只限制自身的0健康,而不允许运行。很抱歉,如果我简单的解释它,我想要一种方式,使x将被限制在0,它将不会打印负数,而是打印一些样本文本,像敌人死亡。
发布于 2020-11-13 19:51:30
使用max函数,如:
import random
gameactive = 0
x= 100
while gameactive == 0:
if x > 0:
crit = random.randint(8,9)
base = random.randint(5,6)
a = random.randint(1,10)
if a == 10:
x -= crit
print("\nITS A CRITICAL HIT!") # Dont mind this please, this chunk about critical hits.
else:
x -= base
print("\n")
print(str(max(x,0)) + " Health left")
else:
break发布于 2020-11-13 19:52:01
一种方法是更改打印"x Health left“的位置。因此,在下面的代码中,每次迭代时,x的值都会在命中之前报告,直到x低于零为止,在这种情况下,可以打印自定义消息"x死掉“或类似的消息,并且循环中断。
while gameactive == 0:
if x > 0:
#report current health here, before hit
print(str(x) + " Health left")
crit = random.randint(8,9)
base = random.randint(5,6)
a = random.randint(1,10)
if a == 10:
x -= crit
print("\nITS A CRITICAL HIT!") # Dont mind this please, this chunk about critical hits.
print("\n")
else:
x -= base
print("\n")
else:
#x died
print("x died.")
breakhttps://stackoverflow.com/questions/64827133
复制相似问题