基于回合的游戏打破

时间:2015-11-24 08:45:06

标签: python variables

我想做一个小转弯游戏。我只编码到轮到我轮到我了。但是,当我执行转弯时,我不会对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...")

2 个答案:

答案 0 :(得分:2)

第一个问题在于if-line:

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

小写值不起作用。执行此操作时,首先将评估"Fire Blast" or "fire blast"部分,得到"Fire Blast"值,因为两个非空字符串的布尔or是第一个字符串。相反,你应该使用:

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": 
    #(...)

第二个问题是hp减法线:

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

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

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

答案 1 :(得分:0)

答案

python中的比较(测试值是否相等)是==,python中的赋值是=。您需要使用cpuHP = cpuHP - fireBlast代替cpuHP == cpuHP - fireBlast

上面粘贴的代码中有一些缩进错误。

您应该在or的任意一侧使用两个比较,例如。 x == y or a == b。字符串始终计算为true,因此您的第一个if将始终运行。

提示(未经请求的建议)

您可以使用.lower().upper()来更轻松地检查字符串。例如:

x = input('stuff').lower()

或为了更好的调试:

x = input('stuff')
x = x.lower()

将程序包装在函数中并将其分解为相互帮助的较小函数(“辅助函数”)非常有用,因为它允许您进行测试。在早期,进行测试的最佳方法是使用assert。例如:

def execute_damage_move(dmg, hp):
    hp = hp - dmg
    return hp

fireBlastDmg = 20
cpuHP = 40

assert HP_after_damage_move(fireBlastDmg, cpuHP) == 20
# this statement tests to make sure the output of execute_damage_move is as expected

fireBlastDmg = 15
cpuHP = 75

assert HP_after_damage_move(fireBlastDmg, cpuHP) == 60
# this statement tests to make sure the output of execute_damage_move is as expected