創建戰鬥系統所以這是代碼非常簡單的Python遊戲
# TEST.PY
import sys
import random
class Fight(object):
def enter(self):
print "\n", "-" * 10
print "There are two muchachos:"
print "MUCHACHO1"
print "MUCHACHO2"
print "One of them looks wounded or something."
your_hit_points = 20
muchacho1_hit_points = 6
muchacho2_hit_points = 11
muchacho1 = True
muchacho2 = True
while your_hit_points > 0 or (not muchacho1 and not muchacho2):
print "\n", "-" * 10
your_attack = random.randint(4,12)
muchacho1_attack = random.randint(1,4)
muchacho2_attack = random.randint(4,8)
attack = int(raw_input("Type 1 to attack MUCHACHO1, 2 to attack MUCHACHO2 >"))
if attack == 1:
muchacho1_hit_points - your_attack
print "You hit MUCHACHO1 for %d hit points." % your_attack
if muchacho1_hit_points <= 0 and muchacho1:
muchacho1 = False
print "MUCHACHO1 killed!"
else:
pass
elif attack == 2:
muchacho2_hit_points - your_attack
print "You hit MUCHACHO2 for %d hit points." % your_attack
if muchacho2_hit_points <= 0 and muchacho2:
muchacho2 = False
print "MUCHACHO2 killed!"
else:
pass
else:
print "DOES NOT COMPUTE"
pass
your_hit_points - muchacho1_attack
print "MUCHACHO1 hit you for %d points, you have %d hit points left." % (muchacho1_attack, your_hit_points)
your_hit_points - muchacho2_attack
print "MUCHACHO2 hit you for %d points, you have %d hit points left." % (muchacho2_attack, your_hit_points)
exit(1)
a_fight = Fight()
a_fight.enter()
我有什麼困難。基本上,WHILE循環永遠不會結束,似乎每個人的生命值都不減。我有一種感覺,在我的腸道中,我失去了一些非常非常基本的東西,因爲我現在已經編寫了幾個小時,所以我可能看不到簡單的東西。
我知道這可以通過使用更多的類或功能更好地完成,但現在我想這樣做(對80 + char行也抱歉)。
一個忠告,儘量經常測試你的代碼,你將看到的問題更快,你會知道問題出在哪裏 –
當我死了muchacho時,我討厭它。如果我在他們殺了我之前設法殺死他們,那麼他們的鬼魂就會永遠打敗我,即使我死了。 (請檢查你的while邏輯) – cmd