需要帮助在Python中修复我的游戏

时间:2014-03-04 03:36:21

标签: python python-3.x python-3.3

import random

hp = 100
eh = 100
x = 0
y = 0

print("Hello welcome to the beta version of my game.  
       This game is a 1 v 1 fight to the death against an unknown enemy.  
       In this game you and the enemy  both start out with 100 health. 
       You can choose to either attack or heal each turn. Have fun and 
       pay attention to the following rules.")

print("Rule 1: You cannot heal while your health is over 84 points.  
       If you break this rule your turn will be void.")

print("Rule 2: You can only enter attack, Attack, heal, or Heal.  
       If you break this rule your turn will be void.")

print("Please press enter to start")


while hp > 0 and eh > 0:
    print("Action? (attack, heal, nothing):")
    act = input(">")
    attack = random.randint(1, 30)
    heal = random.randint(1, 15)
    enemy_attack = random.randint(1, 30)
    enemy_heal = random.randint(1, 15)
    enemy_heal_within_5 = random.randint(1, 5)
enemy_decision = random.randint(1, 2)

if act == "attack" or act == "Attack":
    eh = eh  - attack
    print(attack)
    print("You have dealt %s damage" % attack)

elif act == "heal" and hp < 85:
    hp = hp + heal
    print("You have healed %s points" % heal)

elif act == "heal" and hp > 84:  
    while x == 0:
        if act == "attack":
            x +=1
        else:
            print("You cannot heal at this time, please try again.")
            act = input(">")
if enemy_decision == 1:
    hp = hp - enemy_attack
    print("The enemy has dealt %s damage" % enemy_attack)
elif enemy_decision == 2 and eh > 94:
    pass
elif enemy_decision == 2:
    eh = eh + enemy_heal_within_5
    print("The enemy has healed %s points" % enemy_heal_within_5)
elif enemy_decision == 2 and eh < 85:
    eh = eh + enemy_heal
    print("The enemy has healed %s points" % enemy_heal)
else: 


print("Your health is now %s" %  hp)
print("The enemy's health is now %s" %  eh)

if hp <= 0:
    print("You have lost")
elif eh <= 0:
    print("You have won")

我需要帮助创建一个else语句,如果玩家进入攻击或治疗以外的其他东西,它会要求他们尝试输入攻击或再次治疗。我试着重复我在hp&gt;中所做的事情。 84节,但它最终运行该部分而不是else部分。

2 个答案:

答案 0 :(得分:1)

你可以做类似的事情:

...
while hp > 0 and eh > 0:
    act = "empty"
    print("Action? (attack, heal, nothing):")
    # With this while, you are accepting anything like "aTTaCk", "AttaCK", etc
    while act.lower() not in ["attack","heal", "", "nothing"]:
        act = input(">")
    attack = random.randint(1, 30)
    heal = random.randint(1, 15)
    enemy_attack = random.randint(1, 30)
    enemy_heal = random.randint(1, 15)
    enemy_heal_within_5 = random.randint(1, 5)
    enemy_decision = random.randint(1, 2)
    ...

如果播放器不想制作任何内容,我添加了选项nothing和空字符串("")作为选项。如果您不需要其中任何一个,只需从while语句中的列表中删除它们。

答案 1 :(得分:0)

import random

hp = 100
eh = 100
x = 0
y = 0

print("Hello welcome to the beta version of my game.  This game is a 1 v 1 fight to the death against an unknown enemy.  In this game you and the enemy  both start out with 100 health.  You can choose to either attack or heal each turn. Have fun and pay attention to the following rules.")
print("Rule 1: You cannot heal while your health is over 84 points.  If you break this rule your turn will be void.")
print("Rule 2: You can only enter attack, Attack, heal, or Heal.  If you break this rule your turn will be void.")
print("Please press enter to start")


while hp > 0 and eh > 0:
    act = ""
    print("Action? (attack, heal, nothing):")
    while act.lower() != "attack" and act.lower() != "heal":
        act = input(">")
    attack = random.randint(1, 30)
    heal = random.randint(1, 15)
    enemy_attack = random.randint(1, 30)
    enemy_heal = random.randint(1, 15)
    enemy_heal_within_5 = random.randint(1, 5)
    enemy_decision = random.randint(1, 2)

if act == "attack" or act == "Attack":
    eh = eh  - attack
    print(attack)
    print("You have dealt %s damage" % attack)

elif act == "heal" and hp < 85:
    hp = hp + heal
    print("You have healed %s points" % heal)

elif act == "heal" and hp > 84:  
    while x == 0:
        if act == "attack":
            x +=1
        else:
            print("You cannot heal at this time, please try again.")
            act = input(">")
if enemy_decision == 1:
    hp = hp - enemy_attack
    print("The enemy has dealt %s damage" % enemy_attack)
elif enemy_decision == 2 and eh > 94:
    pass
elif enemy_decision == 2:
    eh = eh + enemy_heal_within_5
    print("The enemy has healed %s points" % enemy_heal_within_5)
elif enemy_decision == 2 and eh < 85:
    eh = eh + enemy_heal
    print("The enemy has healed %s points" % enemy_heal)
else: 


print("Your health is now %s" %  hp)
print("The enemy's health is now %s" %  eh)

if hp <= 0:
    print("You have lost")
elif eh <= 0:
    print("You have won")

使用while循环检查输入是否为"attack",是否为"heal",或两者的任何大写版本。我使用!=,但你也可以使用not,正如Ruben Bermudez在下面所示。