这是我关于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()
答案 0 :(得分:3)
查看Python类。
您可能想要创建一个游戏状态对象,其中包含在游戏过程中所做决策的结果。然后,稍后,当这些决定的结果很重要时,你会检查状态。
您需要在主游戏循环期间保留对该游戏状态对象的引用。但是,将它保存在一个对象中可以保持所有状态信息的有序组织,而不是保留对几个不同变量的引用。