如何在基于文本的程序中添加输入

时间:2019-06-26 15:59:36

标签: python python-3.x

我使用Python创建了一个基于文本的RPG。在执行程序的那一刻,它为您提供了一个简介,最后,我让用户决定走1.左2.右3.中。每个地方都有完成游戏所需的唯一物品,这意味着如果您向右走,它将看到您的包中是否附加了特定物品。如果没有,您将返回主要部分,决定再次前往何处。话虽这么说,中间是我希望用户能够立即攻击龙的主要部分,以便他们可以输,或者如果准备了必要的物品,赢了!现在您没有选择进攻的权利,您只需到达巨龙并赢,就不会输。如何在整个游戏中整合输入的任何技巧都将有所帮助。如果需要更多信息,我可以很乐意分享:)。

我在攻击巨龙之前尝试实现输入,但是它陷入了循环之中,因此即使您获得了所有物品,您也将返回到主地牢。这是最后一个想法的片段代码。

def valid_input(prompt, option1, option2):
    while True:
        response = input(prompt).lower()
        if option1 in response:
            print_pause("You use the " + str(Weapon) + " against the dragon")
            print_pause("But it is not strong enough "
                        "to defeat the dragon, he uses Fire Breath"
                        " and, he incinerates you! ")
            print_pause("You lose!")
            play_again()
            break
        elif option2 in response:
            print_pause("Smart Choice! You head back to the main dungeon")
            dungeon_game()
            break
        else:
            print("Sorry, try again")
    return response


def middle_dungeon():
    print_pause("You go to the middle dungeon.")
    print_pause("After a few moments,"
                " you find yourself in front of a " + Dragon + "!")
    print_pause("This is why you need these magical powers.")

    if "MagicRune" in bag:
        print_pause("Luckily the Wizard trained you well, you now obtain "
                    " the power of the " + str(MagicRune) + "!")
        print_pause("You attack the dragon! ")
    if "MagicRune" not in bag:
        print_pause("You do not obtain the necessary magical powers.")
        print_pause("It looks like you need a scroll or more power!.")
        print_pause("You head back to the main dungeon.")   
        dungeon_game()

    dragon_health = 100
    count = 0
    while dragon_health > 0:
        damage_by_player = random.randint(0, 60)
        print_pause(f"You hit the dragon and caused {damage_by_player} damage")
        dragon_health = dragon_health - damage_by_player
        print_pause(f"dragon health is now {dragon_health}")
        count = count + 1

    print_pause(f"You successfully defeated the dragon in {count} attempts, you win!")
    play_again()

def dungeon_game():
    passage = ''
    if 'started' not in Dungeon:
        display_intro()
        Dungeon.append('started')
        while passage != '1' and passage != '2' and passage != '3':
            passage = input("1. Left\n"
                            "2. Right\n"
                            "3. Middle\n")
            if passage == '1':
                left_dungeon()
            elif passage == '2':
                right_dungeon()
            elif passage == '3':
                middle_dungeon()
                dungeon_game()

因此,从本质上讲,此输出将拒绝您,直到您进入左地牢和右地牢,然后在其中看到MagicRune:这将使您在循环时进入龙并赢得游戏。

1 个答案:

答案 0 :(得分:0)

您需要重新排列一下代码。更改输入功能的方法如下:

def valid_input(prompt, option1, option2):
    while True:
        response = input(prompt).lower()
        if option1 in response:
            return option1
        elif option2 in response:
            return option2
        else:
            print("Sorry, try again")

现在,它返回用户选择的选项,并让调用代码确定如何处理该信息。这使得它实际上可重用。您可以这样称呼它:

chosen = valid_input("Wanna attack the dragon?", "yes", "no")
if chosen == "yes":
    # do that stuff
else:
    # do other stuff

您还有另一个确实需要解决的问题:您正在像goto语句那样对待函数调用。当您尝试调试代码时,这将使您感到痛苦,并且还会导致难以跟踪的错误。

例如,您不应调用play_again()重新启动代码。而是设置您的代码结构,以使您不必这样做。例如

def dungeon_game():  
  while True:
    # set up initial game state here, e.g.
    bag = ["sword", "potion"]
    # call main game logic function
    dungeon()    
    # that function returned, so the game is over.
    want_to_play = input("play again?")
    if want_to_play == "no":
      # if we don't break, the While loop will start the game again
      break

def dungeon():
  # blah blah dragon attack
  if (whatever):
    print("You got killed. Game over.")
    return # goes back to dungeon_game()
  else:
    print("You won the fight")
  # code for the next thing that happens...

if __name__ == "__main__":
  dungeon_game()