我在游戏中找不到任何问题,除了它不会结束的事实。我看不出它有什么问题。
def fight(enemy, playerH, enemyH): #defining a function fight
slay =True
def testDeath(pHealth, eHealth, enemy): #defining a function to test if player/enemy is dead
if pHealth <= 0:
slay = False
print"You have lost the fight, better luck next time."
elif eHealth <= 0:
slay = False
print"You have slain the",enemy,". You win! Congratulations!"
else:
slay = True
#end of the testDeath function
print"You are fighting a",enemy
while slay: #while loop to perform the fight
playerD = randint(0,5) #making each characters damage using randint
dragonD = randint(0,7)
trollD = randint(0,4)
if enemy == "troll":
enemyD = trollD
elif enemy == "dragon":
enemyD = dragonD
print"Your health:",playerH
print enemy,"'s health:",enemyH
sleep(1) #sleep makes next message appear after (1) second(s)
if playerD == 5 or playerD == 4: #player's attack
print"You deal",playerD,"damage points. A critical hit!"
enemyH = enemyH - playerD #removing player damage from enemy health
testDeath(playerH, enemyH, enemy)
if slay == False: break
#sleep(1.5)
#print"The",enemy,"has",enemyH,"health points remaining."
sleep(2)
elif playerD == 0:
print"You attack has missed. You deal 0 damage points."
sleep(2)
else:
print"You deal",playerD,"damage points."
enemyH = enemyH - playerD
testDeath(playerH, enemyH, enemy)
if slay == False: break
#sleep(1.5)
#print"The",enemy,"has",enemyH,"health points remaining."
sleep(2)
if enemy == "troll":
if enemyD == 4:
print"The troll deals 4 damage points. A critical hit!"
playerH = playerH - enemyD
testDeath(playerH, enemyH, enemy)
if slay == False: break
#sleep(1.5)
#print"You have",playerH,"health points remaining."
sleep(2)
elif enemyD == 0:
print"The troll's attack missed. He deals 0 damage points."
sleep(2)
else:
print"The troll deals",enemyD,"damage points."
playerH = playerH - enemyD
testDeath(playerH, enemyH, enemy)
if slay == False: break
#sleep(1.5)
#print"You have",playerH,"health points remaining."
sleep(2)
elif enemy == "dragon":
if enemyD == 6 or enemyD == 7:
print"The dragon deals",enemyD,"damage points. A critical hit!"
playerH = playerH - enemyD
testDeath(playerH, enemyH, enemy)
if slay == False: break
#sleep(1.5)
#print"You have",playerH,"health points remaining."
sleep(2)
elif enemyD == 0:
print"The dragon's attack missed. He deals 0 damage points."
sleep(2)
else:
print"The dragon deals",enemyD,"damage points."
playerH = playerH - enemyD
testDeath(playerH, enemyH, enemy)
if slay == False: break
#sleep(1.5)
#print"You have",playerH,"health points remaining."
sleep(2)
我知道这是很多代码,但我认为不够少就足够了。它可能是,但由于我看不出问题,我不知道在这里发布什么部分。
一切正常,直到有人死亡。当某些事情消亡时,它会说"congratulations you win"
,但战斗将继续。有谁能看到这个问题?
答案 0 :(得分:1)
您的testDeath
函数定义了自己的本地slay
。这是不连接到while
循环正在测试的值。您需要return
:
def testDeath(pHealth, eHealth, enemy): #defining a function to test if player/enemy is dead
if pHealth <= 0:
slay = False
print"You have lost the fight, better luck next time."
elif eHealth <= 0:
slay = False
print"You have slain the",enemy,". You win! Congratulations!"
else:
slay = True
return slay
并指定它:
slay = testDeath(playerH, enemyH, enemy)
您可以重新排列功能以使其更整洁:
def testDeath(pHealth, eHealth, enemy):
"""Test whether the player or enemy is dead."""
if pHealth <= 0:
print "You have lost the fight, better luck next time."
return False
elif eHealth <= 0:
print "You have slain the {0}. You win! Congratulations!".format(enemy)
return False
return True
请注意str.format
和docstrings的使用以及更合理的结构。