我正在製作一個基於python 2.7文本的RPG,而我的戰鬥功能運行不正常。這裏是我的代碼:函數運行不正確
def attack(self):
print "A %r appears! It wants to fight!" % (self.name)
while (player.health > 0) or (self.health > 0):
player.health = player.health - ((randint(0,5)) + attack_multiplier(self.level))
print "%r strikes! Your health is down to %r" %(self.name, player.health)
try:
player.weapon = (raw_input("What do you attack with? >>").lower)
if (player.inventory.get(player.weapon) > 0) and (player.health > 0) and (self.health > 0):
if weapon_probability() == "critical hit":
self.health = self.health - (((randint(0,5))) + (attack_multiplier(weapon_levels.get(player.weapon))) * 2)
print "Critical Hit!"
elif weapon_probability() == "hit":
self.health = self.health - ((((randint(0,5))) + (attack_multiplier(weapon_levels.get(player.weapon)))))
print "Hit!"
elif weapon_probability() == "miss":
self.health = self.health
print "Miss"
print "Enemy health down to %r!" % (self.health)
elif player.health <= 0:
print "Your health...it’s falling"
break
elif self.health <= 0:
print "Enemy vanquished!"
break
except ValueError:
print "You don't have that"
我看到的是:
'Bat' strikes! Your health is down to 95
What do you attack with? >>sword
'Bat' strikes! Your health is down to 91
What do you attack with? >>sword
'Bat' strikes! Your health is down to 87
What do you attack with? >>sword
'Bat' strikes! Your health is down to 85
What do you attack with? >>sword
'Bat' strikes! Your health is down to 82
What do you attack with? >>
這只是不斷重複和player.health
甚至不斷下降到底片。我找不到錯誤。這個函數是一個類的方法,並且player
是另一個類的一個實例。
OMG我怎麼沒有看到這一點。我很抱歉 – frg100 2015-04-03 17:39:23
對不起,接受這麼長的時間來接受答案...它的第一個問題 – frg100 2015-04-04 19:06:08
@ frg100:不是問題!有些人已經等待了*年*,標記職位被接受,這一天幾乎沒有*長*。 :-) – 2015-04-05 15:33:08