4

我正在用 python 编写文本冒险,我很新,但我知道我的方式。我遇到的问题是我的敌人(enemy1)没有失去健康。这是可以帮助您理解的代码。

hp=100
punch=10
kick=20
kill=99999999
burger=10
soup=10
glass=999999999
nothing=100
enemy1=100

from time import sleep

print("What's Your name?")
usr=raw_input("> ")
print("Your starting health is 100"), usr
sleep(1)
print("You awake in your prison cell. You can go to the gym or outside.")
sleep(.5)
print("type gym or outside to go there")
cell=raw_input("> ")
if cell=="gym":
    print("You go to the gym. There are 2 guards here and 5 prisoners. You can lift weights, go outside, go to the cells or fight a prisoner")
    sleep(1)
    print("Type fight, weights, outside or cells")
    gym=raw_input("> ")
    if gym=="fight":
        print("You walk up to a prisoner and he stabs you.")
        sleep(2)
        print("Game Over"), usr
    if gym=="weights":
        print("You go and pick up some weights. You forget overwork yourself and die of a heart attack")
    if gym=="outside":
        print("You go outside to ")
if cell=="outside":
    print("You go outside. There are some people playing basketball and some people doing drugs. You can do drugs, play bball or fight someone")
    outside=raw_input("> ")
    if outside=="fight":
        print("You walk up to a guy")
        sleep(1)
        print("Hey, wanna fight?")
        sleep(1)
        print("Sure!")
        sleep(1)
        print("The stranger punches you.")
        sleep(1)
        print("Your health is now"), hp-punch
        while enemy1>0:
            print("You can kick or punch")
            fight1=raw_input("> ")
            if fight1=="punch":
                print("You punch that mothafucka")
                sleep(1)
                print("His health is"), enemy1-punch
            if fight1=="kick":
                print("You kick that mothafucka")
                sleep(1)
                print("His health is now"), enemy1-kick
            if fight1=="kill":
                print("You evicerate that mothafucka")
                sleep(1)
                print("His health is now"), enemy1-kill
        print("You win!")

别想着你出去,你和人打架,他的健康回到一百。我发现这是因为我一直在输入enemy1-x。我想知道如何降低敌人的生命值并记住它较低,并且当它达到零时它会中断 while 循环。

4

1 回答 1

3
print("Your health is now"), hp-punch
print("His health is"), enemy1-punch
print("His health is now"), enemy1-kick
print("His health is now"), enemy1-kill

这些行实际上并没有改变hpenemy1。使用赋值语句来更改它们的值:a -= b,它是 的简写a = a - b

hp -= punch
print "Your health is now", hp

enemy1 -= punch
print "His health is", enemy1

enemy1 -= kick
print "His health is now", enemy1

enemy1 -= kill
print "His health is now", enemy1
于 2013-04-17T03:11:13.030 回答