-1

这是我在 stackoverflow 上的第一篇文章,我只是在问,因为我真的被卡住了。我和一个好朋友正在尝试制作一个简单的基于文本的游戏(让我们在 80 年代听到它)。这是我第一天基本上使用 python,我遇到了一个问题。玩家可以选择是否拿起匕首。稍后我希望这个决定发挥作用。除了我不知道如何查看玩家输入的内容!这是我的代码 注意:它在现在的状态下崩溃,主要是因为我在试图解决这个问题时磕磕绊绊。

def start():
        print("You wake up in a dank tunnel reeking of death and decomposition,")
        print("you have no weapons to defend yourself with.")
        time.sleep(1)
        print("\n")
        print("You head north and find yourself at a four way tunnel.")
        print("\n")
        print("Down the west tunnel, you hear sounds which could have only come")
        print("from hell itself. With every sound uttered, your fear grows.")
        time.sleep(1)
        print("\n")
        print("Down the north tunnel,  there is a room with a glimmering object")
        print("lying on the ground.")
        time.sleep(1)
        print("\n")
        print("Down the East tunnel, there appears to have been a cave in.")
        time.sleep(1)
        print("\n")
        print("The South tunnel is the way you just came. \n")
        time.sleep(1)
        while True:
            r3 = input("Which way would you like to go?\n")
            if r3 == "west" or r3 == "West":
                print("The sounds are becoming Louder, do you continue or head back?")
                while True:
                    w1 = input("")
                    if w1 == "continue" or w1 == "Continue" or w1 == "west":
                            print("You continue further on...")
                            time.sleep(2.5)
                            westtunnel()
                    elif w1 == "head back" or w1 == "Head Back" or w1 == "back" or w1 == "Back":
                        print("\n")
                        print("The voices congregate behind you. No way your going back that way!\n")
                        time.sleep(1)
                        westtunnel()
                    else:
                        print("\n")
                        print("You couldn't possibly do anything else.\n")
                        time.sleep(1)
                        print("Greater minds prevail and you continue westward...\n")
                        time.sleep(2)
                        westtunnel()
            elif r3 == "north" or r3 == "North":
                print("You find a rusty dagger on the floor, stained with blood.")
                print("Do you want to pick up the dagger?")
                print("1: Yes, that seems useful!")
                print("2: No, I'm a bit of a pacifist you see.")
                while True:
                    pd = input("")
                    if pd == "1":
                        print("You slowly picked up the dagger.")
                        number = int(pd)
                        break
                    else:
                        print("You left the dagger. All alone.")
                        number = int(pd)
                        break
                print("You can go only go back the way you came.\n")
                time.sleep(1)
                print("You head back to the four way tunnel.")
            elif r3 == "east" or r3 == "East":
                print("\n")
                print("You can not go that way, there are rocks there. We told you this.\n")
                time.sleep(1)
                print("You go back to the four way tunnel")
            elif r3 == "south" or r3 == "South":
                print("\n")
                print("This is the room you awoke in, there is nothing of interest.\n")
                time.sleep(1)
                print("You head back to the four way tunnel")
            else:
                print("\n")
                print("You run into a corner, you hurt yourself in confusion. \n")
                time.sleep(1)
                print("You stumble back to the four way.")

def ladder():
    print("Do you have a dagger?!")
    number = pd
    if number == "1":
        print("Good you have it!")
        start()
        input("")
    else:
        print("awh...no dagger for you...")
        start()
        input("")
if __name__ == '__main__':
    menu()
4

1 回答 1

3

查看 Python 类。

您可能想要创建一个 Game State 对象来保存游戏过程中做出的决定的结果。然后,稍后,当这些决定的结果很重要时,您检查状态。

您需要在主游戏循环期间保留对该游戏状态对象的引用。但是,将其保存在一个对象中可以使所有状态信息井井有条,而不是保留对几个不同变量的引用。

于 2013-09-22T19:04:29.073 回答