2014-03-04 37 views
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: 
    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> 84部分所做的事情,但它最终运行了该部分而不是其他部分。需要别人帮助修理我的游戏在Python

+1

只是想知道,为什么倒票,downvoter?这个问题对我来说似乎很彻底...... –

+0

@ aj8uppal看起来downvoter认为downvote应该是“我不喜欢这个游戏的想法”。我只是在解决这个问题而已。这不应该有负面的投票。这并不糟糕。 – Guy

回答

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声明中的列表中删除。

+0

对不起,请原谅我的回滚,我没有意识到这是你自己编辑的答案。 –

+0

没问题,最后对我的回答是一个改进 –

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所示。