基本上我正在学习编码并看到了这个漂亮的龙境界示例任务。一切都很好,直到我到达我想参与战斗的阶段。
def cave_2_fight():
def monster_hit():
m_hit = random.randint(1, 11)
return m_hit
def player_hit():
p_hit = random.randint(1, 11)
return p_hit
monster_hp = random.randint(1, 101)
player_hp = 100
print("You are being attacked by a goblin with {} hp".format(monster_hp))
print("Getting ready text")
while monster_hp != 0 or player_hp != 0:
attack_area = input("Where do you want to hit him[top or mid or bot]: ")
if attack_area == 'top':
p_dmg = player_hit()
m_dmg = monster_hit()
print('You hit goblin for {} and he hits you for {}'.format(p_dmg, m_dmg))
player_hp = player_hp - m_dmg
monster_hp = monster_hp - p_dmg
return player_hp, monster_hp
elif attack_area == "mid":
p_dmg = player_hit()
m_dmg = monster_hit()
print('You hit goblin for {} and he hits you for {}'.format(p_dmg, m_dmg))
player_hp = player_hp - m_dmg
monster_hp = monster_hp - p_dmg
我希望它有点随机,并且我在第一次疏忽后回到选择attack_area时非常困难。提前致谢
答案 0 :(得分:1)
return player_hp, monster_hp
退出该功能。你应该拿出这一行 - monster_hp和player_hp会在没有它的情况下得到更新。
另外,你应该改变
while monster_hp != 0 or player_hp != 0:
到
while monster_hp > 0 or player_hp > 0:
因为某人的HP可能会消极。如果您希望任何一个玩家死亡时游戏结束,请更改"或"到"和" (这将要求他们两个都活着。)
你可能会被投票,因为a)你的代码问题根本不清楚(我猜),b)你发布的代码不是{{ 3}}:第一行会出现语法错误(我认为这只是复制粘贴的缩进问题。)