2014-03-04 35 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所示。