我已经开始在python上制作一个新游戏,如果player1或player2的生命值降到零或更低,代码就会结束,但我不想在结束时显示玩家的生命值是-12。代码如下:
player1 = 50
player2 = 50
while player1 >= 0 or player2 >= 0:
import random
slash = random.randint(5, 9)
stab = random.randint(1, 15)
swing = random.randint(15, 20)
heal = random.randint(10, 15)
a = [slash, stab, swing]
ai = random.choice(a)
hit1 = input("Press 1 2 3 or 4")
if hit1 == "1":
print("You dealt " + str(slash))
player2 = player2 - slash
print("Player 2 now has " + str(player2))
if hit1 == "2":
print("You dealt " + str(stab))
player2 = player2 - stab
print("Player 2 now has " + str(player2))
if hit1 == "3":
print("You dealt " + str(swing))
player2 = player2 - swing
print("Player 2 now has " + str(player2))
if hit1 == "4":
print("You healed by " + str(heal))
player2 = player1 + heal
print("Player 1 now has " + str(player1))
hit2 = print("player 2 has dealt " + str(ai))
player1 = player1 - ai
print("player1 is now on " +str(player1))
发布于 2019-11-27 04:36:03
您可以在python中使用max
函数。
对于您的相关行:
player2 = max(player2 - slash, 0)
player2 = max(player2 - stab, 0)
player2 = max(player2 - swing, 0)
player1 = max(player1 - ai, 0)
此外,你需要改变你的while条件:
while player1 > 0 or player2 > 0:
编辑
player1 = 50
player2 = 50
while player1 > 0 and player2 > 0:
import random
slash = random.randint(5, 9)
stab = random.randint(1, 15)
swing = random.randint(15, 20)
heal = random.randint(10, 15)
hit_number_to_hit_type = {'1': slash,
'2': stab,
'3': swing}
a = [slash, stab, swing]
ai = random.choice(a)
hit1 = input("Press 1 2 3 or 4\n")
if "1" <= hit1 <= "3":
hit = hit_number_to_hit_type[hit1]
print("You dealt " + str(hit))
player2 = max(player2 - hit, 0)
print("Player 2 now has " + str(player2))
if hit1 == "4":
print("You healed by " + str(heal))
player1 += heal
print("Player 1 now has " + str(player1))
hit2 = print("player 2 has dealt " + str(ai))
player1 = max(player1 - ai, 0)
print("player1 is now on " + str(player1))
https://stackoverflow.com/questions/59058940
复制相似问题