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