我想在敌人生命值为0时破译密码。我在哪里破译密码
如何让两个while循环不断重复,直到敌人的生命值达到0?我是否将第二个while循环嵌套在第一个while循环中
p1.hp = 100
e1.hp = 100
g = False
while g == False:
g = True
chanceR = chance[randint(0,1)]
choiceR = choice[randint(0,1)]
print("Your turn!")
p1c = input("Choose an option: [attack] or [block] ")
if p1c == "attack":
if choiceR == "attack" and chanceR == "succcess":
print("Enemy countered your attack!")
elif choiceR == "attack" and chanceR == "fail":
print("Attack sucessful! Enemy loses 10HP!")
e1.hp -= 10
print("Enemy's health:", e1.hp)
elif choiceR == "block" and chanceR == "success":
print("Enemy blocked your attack!")
elif choiceR == "block" and chanceR == "fail":
print("Enemy failed to block! Enemy loses 10HP!")
e1.hp -= 10
print("Enemy's health:", e1.hp)
elif p1c == "block":
if choiceR == "attack" and chanceR == "success":
print("Attack blocked successfully!")
elif choiceR == "attack" and chanceR == "fail":
print("Enemy failed to attack. Nothing happens")
elif choiceR == "block":
print("Enemy blocks. Nothing happens")
else:
print("That is not a valid input. Please try again")
g = False
while g == True:
print("Enemy's turn")
chanceRp = chance[randint(0,1)]
choiceRp = choice[randint(0,1)]
chanceRe = chance[randint(0,1)]
choiceRe = choice[randint(0,1)]
if choiceRe == "attack" and chanceRe == "success":
if choiceRp == "attack" and chanceRp == "success":
print("You successfully countered the enemy's attack!")
elif choiceRp == "attack" and chanceRp == "fail":
print("Enemy successfully attacked you! You lose 10 HP!")
p1.hp -= 10
print("Your health:", p1.hp)
elif choiceRp == "block" and chanceRp == "success":
print("You successfully blocked the enemy's attack!")
elif choiceRp == "block" and chanceRp == "fail":
print("You failed to block the enemy's attack! You lose 10HP!")
p1.hp -= 10
print("Enemy's health:", p1.hp)
elif choiceRe == "attack" and chanceRe == "fail":
if choiceRp == "attack" and chanceRe == "success":
print("You successfully attacked the enemy! Enemy loses 10HP!")
e1.hp -= 10
print("Enemy's health:", e1.hp)
elif choiceRp == "attack" and chanceRp == "fail":
print("Both the enemy and you failed to attack! Nothing happens!")
elif choiceRp == "block" and chanceRp == "success":
print("Enemy failed to attack! Nothing happens!")
elif choiceRp == "block" and chanceRp == "fail":
print("Enemy failed to attack! Nothing happens!")
elif choiceRe == "block" and chanceRe == "success":
if choiceRp == "attack" and chanceRp == "success":
print("Enemy successfully blocked your attack!")
elif choiceRp == "attack" and chanceRp == "fail":
print("You failed to attack!")
elif choiceRp == "block" and chanceRp == "success":
print("Both the enemy and you blocked! Nothing happens!")
elif choiceRp == "block" and chanceRp == "fail":
print("Both the enemy and you blocked! Nothing happens!")
elif choiceRe == "block" and chanceRe == "fail":
if choiceRp == "attack" and chanceRp == "success":
print("Enemy failed to block! Enemy loses 10HP!")
e1.hp -= 10
print("Enemy's health:", e1.hp)
elif choiceRp == "attack" and chanceRp == "fail":
print("You failed to attack!")
elif choiceRp == "block" and chanceRp == "success":
print("Both the enemy and you blocked! Nothing happens!")
elif choiceRp == "block" and chanceRp == "fail":
print("Both the enemy and you blocked! Nothing happens!")
g = False
g意味着游戏 p表示玩家 e意味着敌人
这段代码基本上显示了玩家和敌人在各自回合中可能出现的结果
chance = ["success", "fail"]
choice = ["attack", "block"]
下面是代码的机会和选择变量
我是个新手,如果你能帮我,我会非常感激。谢谢:)
您的两个独立转弯是同一环路的一部分。按照现在的结构,游戏将运行AAAAAAA,然后是BBBBBBB而不是abababab
把while条件想象成实际发生的事情——一场战斗
玩家回合和敌人回合是在战斗的每个循环中按顺序发生的事件序列。现在,if和elifs的事件树有点杂乱无章,因此比需要的要详细得多。除此之外,一个回合和另一个回合的编码方式几乎没有区别,因此您可以编写一个函数来定义一个回合,传递哪个玩家在该回合中扮演哪个角色,但这超出了本文的范围。在休息时,可以这样想:
在找到hp部件之前,请注意:不要重复!这里定义了两个完全相同的类,并使用丢弃的值初始化它们:
相反,你可以做:
现在
player
和enemy
都有100 hp,并且它们都有name
属性,这些属性是您传入的(在以前的代码中,您传入的名称刚刚被覆盖,因为您将它们作为health
参数传入)另一件需要注意的事情是,通过使用字符串文字来表示所有内容,您会遇到输入字符串的错误:
注意
"succcess"
中的拼写错误!这张支票将一直是False
。一个更好的选择是使用Enum
,如果您试图以明显不正确的方式使用它们,则会出现错误(在运行时或运行类型检查工具时)为了保持攻击/反击的顺序,是的,你需要把所有的东西都放在另一个循环中。如果您为每个“回合”(包含它们自己的循环)定义一个函数,然后在外部循环中运行这些函数,则更容易阅读,如:
另一种方法是使用例外,例外具有自动脱离任何上下文的良好特性:
实现这些功能后,它可能看起来像:
相关问题 更多 >
编程相关推荐