我正在制作一个简单的基于文本的游戏,它使用while
循环进行战斗。它询问玩家他们想做什么,并提供使用武器或库存(药水等)的选项。如果他们选择库存来查看他们拥有的东西,我怎样才能让他们选择回去选择武器并进行攻击?我试图创建一个调用back
它的唯一代码的函数pass
,如果用户键入back
它会将它们发送到菜单的开头。但它只会无休止地循环敌人的攻击,而不让用户做任何事情。有任何想法吗?这是我的战斗代码:
我希望能够在print "what will you do?"
线路之后将它们送回
def combat():
hero_hp = 1000000
enemy_hp = randint(5, 10)
enemy_name_test = randint(1,5)
enemy_weapon_test = randint(1,5)
enemy_name = list_of_enemys[enemy_name_test]
enemy_weapon = list_of_weapons[enemy_weapon_test]
while hero_hp > 0 and enemy_hp > 0:
print "The %s swings at you with a %s" % (enemy_name, enemy_weapon)
damage = randint(0, 10)
if damage == 0 :
print "the %s misses" %enemy_name
else :
hero_hp -= damage
print "The %s hits you with a %s for %d hit points you have %d hit points left" % (enemy_name, enemy_weapon, damage, hero_hp)
hero_hp -= damage
if hero_hp <= 0 :
print "you have been slain!"
close()
else :
print "What will you do?"
print inventory
player_choice = raw_input("> ")
if player_choice == '1' :
print hero_weapons
player_choice2 = raw_input("> ")
weapon = hero_weapons[player_choice2]
damage = randint(0, 10)
enemy_hp -= damage
print "You attack with %s for %d hit points" % (weapon, damage)
print "%s has %d hit points left" % (enemy_name, enemy_hp)
if enemy_hp <= 0 :
print "You have slain the %s" % enemy_name
else :
pass
else :
print hero_equipment
player_choice2 = raw_input("> ")
heal = randint(1, 10)
hero_equipment.pop(player_choice2)
print "you drink a potion and heal %s hit points" % heal