【发布时间】:2014-10-02 19:34:49
【问题描述】:
我刚刚开始使用 Python 2 和一般编程,并决定进行一次文本冒险以进行一些练习,但我完全被 title 所困。
这是目前为止的代码。我几乎完全猜测了整个真/假的东西,但我想要解决的是:当你进入 "viewing_room" 时,你会遇到一扇锁着的门,但如果你去 "lab_room" 并得到钥匙卡从身上就可以打开了。
我试图让锁着的门是假的,但是当你收集钥匙卡时,它会变成真,门就会解锁。感谢您的帮助,谢谢!
prompt = "> "
decision = "What do you do?"
not_assigned = "Say wa?"
def engine_room():
print "You are in a dark room with the sound of moaning engines."
print "You see a corridor to your left and one to your right and an elevator straight ahead of you."
print decision
choice = raw_input(prompt)
if choice == "go left":
viewing_room()
elif choice == "go right":
right_corridor_dead_end()
elif choice == "use elevator":
print "you get in the elevator and go up."
main_hallway()
else:
print not_assigned
engine_room()
def right_corridor_dead_end():
print "You walk down the corridor only to be blocked by a collapsed ceiling."
print decision
choice = raw_input(prompt)
if choice == "go back":
engine_room()
else:
print not_assigned
def viewing_room():
print "You walk down the corridor and enter and a large room with a window covering the size of the wall."
print "Straight ahead is another door"
print decision
choice = raw_input(prompt)
if search_body() == False:
if choice == "open door":
print "The door is locked"
viewing_room()
elif choice == "go back":
engine_room()
else:
print not_assigned
if search_body() == True:
if choice == "open door":
print "The door opens you walk through"
storage_room()
else:
print not_assigned
viewing_room()
def main_hallway():
print "You enter a large brightly lit room with 3 rooms connected to it and another elevator straight ahead."
print "The rooms are named, the two on the left are the armoury and lab rooms and to the right are the cabins."
print decision
choice = raw_input(prompt)
if choice == "go to lab room":
lab_room()
elif choice == "go back":
engine_room()
def lab_room():
print "You enter the lab room which is cluttered with unexplainable machines."
print "To the back of the room you see the dead body of a man with no obvious cause"
print "He might have something useful on him"
print decision
choice = raw_input(prompt)
if choice == "search body":
search_body()
elif choice == "go back":
main_hallway()
def search_body():
print "You find a keycard that says 'storage' on it."
return True
lab_room()
engine_room()
【问题讨论】:
-
lab_room()在您的退货声明后无法在search_body中访问 -
你应该看看类而不是函数。
-
您需要能够以某种方式存储状态。通常这是用对象完成的,这确实是何时使用面向对象编程的主要示例。在 Python 中研究 OOP。
-
classic:在破坏self之前检查您的self.....oops -
问题是你需要随时保持游戏的状态。使用类很常见,但您也可以创建一个包含键/值对的字典,用于跟踪启用/禁用哪些功能。该 dict 可以是全局变量,或者更好的是,它可以传递给函数。这与持有状态的类非常相似,但更加解耦。
标签: python python-2.7