当我尝试与玩家对抗敌人时,我一直循环不断。这是一个更大的基于文本的地牢爬虫,它使您可以得到物品,而敌人也可以随步而变。
import random
import sys
player = {
'name': 'unknown',
'class': 'unknown',
# 'location': r1,
'HP': 100,
'ap': 1,
'dp': 1,
'mp': 1,
'ap_item': 1,
'dp_item': 1,
'ms_itme': 1
}
enemy1 = {
'HP': 50,
'ap': .2,
'dp': .2,
'name': 'Rat'
}
enemy2 = {
'HP': 50,
'ap': .2,
'dp': .2,
'name': 'Skeleton'
}
enemy5 = {
'HP': 75,
'ap': .5,
'dp': .5,
'name': 'Skeleton pack'
}
def enemy_attack(oppo1, player):
while player['HP'] > 0:
if oppo1['ap'] == .2:
rand_damage = random.randint(8, 25)
attack_damage = rand_damage * player['dp']
final_damage = attack_damage * player['dp_item']
player['HP'] - final_damage
print(final_damage, "damage!")
break
elif oppo1['ap'] == .5:
rand_damage = random.randint(16, 50)
attack_damage = rand_damage * player['dp']
final_damage = attack_damage * player['dp_item']
player['HP'] - final_damage
print(final_damage, "damage!")
break
else:
print("enemy_attack is wrong")
break
def attack(player, oppo1):
while oppo1['HP'] > 0:
rand_damage = random.randint(8, 32)
attack = rand_damage / player['ap']
attack_damage = attack / player['ap_item']
final_damage = attack_damage * oppo1['dp']
player['HP'] - final_damage
print(final_damage, " damage!")
break
def fight(player, oppo1):
while (player['HP'] > 0) and (oppo1['HP'] > 0):
print(player['name'], "attacks", oppo1['name'])
attack(player, oppo1)
if oppo1['HP'] <= 0:
print(player['name'], "is winner")
oppo1['HP'] = 0
print(player['name'], "HP:", player['HP'])
print(oppo1['name'], "HP:", oppo1['HP'])
break
else:
print(oppo1['name'], "attacks", player['name'])
enemy_attack(oppo1, player)
if player['HP'] <= 0:
print(oppo1['name'], "is winner")
player['HP'] = 0
print(player['name'], "HP:", player['HP'])
print(oppo1['name'], "HP:", oppo1['HP'])
print("game_over", player['name'])
def encounter(player, oppo1):
print("You have come to a door, you open the door and slowly enter.")
fight(player, oppo1)
input('Press any key to continue')
def main():
print("encounter 1")
encounter(player, enemy1)
input("press enter to continue")
print("encounter 1")
encounter(player, enemy2)
input("press enter to continue")
print("encounter 1")
encounter(player, enemy5)
input("press enter to continue")
main()
真的可以使用一些帮助。我尝试了很多不同的想法,以便能够拥有乘数和所有战斗逻辑,但这无休止地循环了玩家和敌人的相互攻击。
答案 0 :(得分:1)
在几个地方都有这个:
player['HP'] - final_damage
此代码在player
内不存储任何值-只是减去HP,然后丢弃该值。要存储值,您需要在其中存储等号:
player['HP'] -= final_damage
似乎您也不会尝试降低对手的生命值。为此,您将需要
oppo1['HP'] -= final_damage
在代码中的某个时刻。
由于玩家的HP和对手的HP从不降低,因此while
函数的fight
循环中的条件始终为真,并且战斗将持续进行。
顺便说一句...在我评论时,我注意到您可以做一些其他事情来改善您的代码。
在attack
函数中,您需要执行以下操作:
while oppo1['HP'] > 0:
# ...
break
此break
语句将在第一次运行时退出while循环,因此您也可以只使用if
语句。
if oppo1['HP'] > 0:
# ...
希望这会有所帮助!