gpt4 book ai didi

python - 回合制游戏坏了

转载 作者:行者123 更新时间:2023-12-01 04:19:23 24 4
gpt4 key购买 nike

我想制作一个小型回合制游戏。我只编码了轮到我的回合,并且只轮到我的回合。然而,当我执行回合时,我不会对CPU造成任何损害,并且在选择该选项时我也不会治愈自己。

import random
print("Let's play a turn based game")
print("Your moves are:")
print("")
print("1) Fire Blast(18 to 25 HP) 2) Aura Sphere(10 to 35 HP) 3) Recover(Recover 10 to 30 HP)")
print("You can go first")
playerHP = 100
cpuHP = 100
fireBlast = random.randint(18,25)
auraSphere = random.randint(10,35)
recover = random.randint(10,30)

while playerHP >= 0 or cpuHP >= 0:
print("")
print("You have",playerHP,"HP")
print("I have",cpuHP,"HP")
playerMove = input("Which move do you choose? ")
print("Initiate player's move!")
if playerMove == "Fire Blast" or "fire blast":
cpuHP == cpuHP - fireBlast
print("You used Fire Blast! I now have",cpuHP,"HP")
elif playerMove == "Aura Sphere" or "aura sphere":
cpuHP == cpuHP - auraSphere
print("You used Aura Sphere! I now have",cpuHP,"HP")
elif playerMove == "Recover" or "recover":
playerHP == playerHP + recover
print("Healed! You now have",playerHP,"HP")
else:
print("You didn't choose a move...")

最佳答案

第一个问题在于 if 行:

if playerMove == "Fire Blast" or "fire blast":
#(...)
elif playerMove == "Aura Sphere" or "aura sphere":
#(...)
elif playerMove == "Recover" or "recover":
#(...)

小写值不起作用。执行此操作时,首先将评估“Fire Blast”或“fireblast”部分,从而产生“Fire Blast”值,如 bool 或两个非空字符串中的 是第一个字符串。相反,你应该使用:

if playerMove == "Fire Blast" or playerMove ==  "fire blast":
#(...)
elif playerMove == "Aura Sphere" or playerMove == "aura sphere":
#(...)
elif playerMove == "Recover" or playerMove == "recover":
#(...)

或者为了简化你可以使用 lower() :

playerMove = playerMove.lower()
if playerMove == "fire blast":
#(...)
elif playerMove == "aura sphere":
#(...)
elif playerMove == "recover":
#(...)

第二个问题是马力减法线:

cpuHP == cpuHP - fireBlast
cpuHP == cpuHP - auraSphere
playerHP == playerHP + recover

== 运算符用于比较值。你想要的是赋值运算符=:

cpuHP = cpuHP - fireBlast
cpuHP = cpuHP - auraSphere
playerHP = playerHP + recover

关于python - 回合制游戏坏了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33888990/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com