我的文字冒险游戏有点麻烦。我们的想法是从起居室开始,到地下室去抓钥匙,当你再次进入起居室时,你应该赢。当我执行我的代码时,它只是让我进入房间,布尔值应该告诉if语句has_key = true
但它总是不起作用。有什么想法吗?
def welcomeMessage():
print "Welcome to my game!!"
def winnerMessage():
print "You're a winner!! Congratulations!!"
userQuit()
def userQuit():
print "Thanks for playing!"
def living_room():
# This part isn't executing (Boolean doesn't work here)
# I want the if statement to execute, not the else statement
if has_key == True:
winnerMessage()
userQuit()
else:
print ("\nYou are in the living room. The paint from the walls is tearing off."
+" There is a door near you, but it seems to be locked. To your west is the"
+" kitchen, where you can eat some tasty snacks and to your south is a bedroom. ")
direction = raw_input("Which direction would you like to go? (W)est or (S)outh? You also have the option to (Q)uit. ")
if direction == "W":
kitchen()
elif direction == "S":
bed_room()
elif direction == "N":
print "Sorry, you can't go north here."
living_room()
elif direction == "E":
print "Sorry, you can't go east here."
living_room()
elif direction == "Q":
userQuit()
else:
print "Sorry, that's not a valid direction."
living_room()
def kitchen():
print ("\nYou are in the kitchen. The water from the sink is slightly running. All of the"
+" cupboards in the kitchen have been left open, like someone has searched through them."
+" To your south is the dining room, and to your east is the living room. ")
direction = raw_input("Which direction would you like to go? (S)outh or (E)ast? You also have the option to (Q)uit. ")
if direction == "S":
dining_room()
elif direction == "E":
living_room()
elif direction == "N":
print "Sorry, you can't go north here."
kitchen()
elif direction == "W":
print "Sorry, you can't go west here."
kitchen()
elif direction == "Q":
userQuit()
else:
print "Sorry, that's not a valid direction."
kitchen()
def bed_room():
print ("\nYou are in the bedroom. One of the windows in the room is slightly ajar. The other window"
+" is shattered with a brick laying on the floor next to it. To your west is the dining room and"
+" to your north is the living room.")
direction = raw_input("Which direction would you like to go? (W)est or (N)orth? You also have the option to (Q)uit. ")
if direction == "W":
dining_room()
elif direction == "N":
living_room()
elif direction == "E":
print "Sorry, you can't go east here."
bed_room()
elif direction == "S":
print "Sorry, you can't go south here."
bed_room()
elif direction == "Q":
userQuit()
else:
print "Sorry, that's not a valid direction."
bed_room()
def dining_room():
print ("\nYou are in the dining room. It is very hard to see in here due to the dim lighting. You notice a staircase is the"
+" in the center of the room. To your north is the kitchen, and to your east is the bedroom.")
direction = raw_input("Which direction would you like to go? (N)orth or (E)ast or go (D)own the staircase? You also have the option to (Q)uit. ")
if direction == "N":
kitchen()
elif direction == "E":
bed_room()
elif direction == "D":
basement()
elif direction == "S":
print "Sorry, you can't go south here."
dining_room()
elif direction == "W":
print "Sorry, you can't go west here."
dining_room()
elif direction == "Q":
userQuit()
else:
print "Sorry, that's not a valid direction."
dining_room()
def basement():
print ("\nYou are now in the basement. A cloud of dust passes by you and makes it hard to breath. You move away from the area"
+" and you notice a key on the floor. You pick up the key and hold onto it. You may use it for later. ")
direction = raw_input("Press U to go upstairs. You also have the option to (Q)uit. ")
if direction == "U":
has_key = True
dining_room()
elif direction == "Q":
userQuit()
else:
print "Sorry, the only place you can go is back upstairs."
basement()
welcomeMessage()
has_key = False
living_room()
答案 0 :(得分:2)
虽然在 living_room()的调用之前定义了 has_key ,但它的范围不会渗透到从 living_room调用的例程中定义的任何同名变量()即可。这是因为在Python中,函数体是本地范围块。在函数体中声明或赋值的任何变量都是该函数的局部变量,除非明确声明为global。
特别是,基础()中定义的 has_key 属于基础()的本地范围。它是一个不同的新的局部变量。
livement()中 has_key 的状态更改未反映在最初定义的 has_key 中,然后才会调用 living_room()< / strong>即可。它们的范围不同。
虽然一个选项是在其状态将被更改的例程中声明 has_key 变量全局变量(如basement()),但全局变量通常不被视为最佳实践。更改函数中全局变量的状态可能导致很难找到错误。可能更好的做法是让地下室返回其状态并在其调用者中检查该状态。
我注意到你的一些函数是递归的,不确定在这种情况下它是最好的选择,但这是另一个问题。
将http://spartanideas.msu.edu/2014/05/12/a-beginners-guide-to-pythons-namespaces-scope-resolution-and-the-legb-rule/视为Python变量范围的一个教程。
答案 1 :(得分:2)
必须告诉Python您的has_key
变量属于global:
def basement():
global has_key
...
if direction == "U":
has_key = True
通过轻微的重写,您可以使用message passing来控制玩家当前所在的位置。
因此,不是调用living_room()
函数并让它通过从内部调用函数来控制玩家所在的位置,而是live_room函数可以返回你的“游戏循环”应该调用的函数:
def game_loop():
first_room = living_room
next_room = first_room()
while callable(next_room):
next_room = next_room()
典型的游戏代码看起来比你的更像。它有一些优点,首先是涉及的递归会更少,因此“堆栈”不会那么深。其次是它允许你在房间之间应用一些共同的逻辑,例如跟踪玩家的去向。
答案 2 :(得分:1)
在更改basement()
函数中global has_key
的值之前插入此行:
has_key
它将告诉Python将值赋给全局范围的has_key
,而不是在函数内部创建一个新的,本地范围的>>> x, y = ???
>>> min(x, y) == min(y, x)
False
。
答案 3 :(得分:0)
您还可以使整个程序更加面向对象,并将has_key设置为对象的属性。只需要添加一个类,一个 init 方法来初始化self.has_key = False并将所有函数都花在一堆self上。 为了防止你经常复制和粘贴,我已经为你做了。
class TextAdventure(object):
def __init__(self):
self.has_key = False
def welcomeMessage(self):
print "Welcome to my game!!"
def winnerMessage(self):
print "You're a winner!! Congratulations!!"
self.userQuit()
def userQuit(self):
print "Thanks for playing!"
def living_room(self):
# This part isn't executing (Boolean doesn't work here)
# I want the if statement to execute, not the else statement
if self.has_key == True:
self.winnerMessage()
self.userQuit()
else:
print ("\nYou are in the living room. The paint from the walls is tearing off."
+" There is a door near you, but it seems to be locked. To your west is the"
+" kitchen, where you can eat some tasty snacks and to your south is a bedroom. ")
direction = raw_input("Which direction would you like to go? (W)est or (S)outh? You also have the option to (Q)uit. ")
if direction == "W":
self.kitchen()
elif direction == "S":
self.bed_room()
elif direction == "N":
print "Sorry, you can't go north here."
self.living_room()
elif direction == "E":
print "Sorry, you can't go east here."
self.living_room()
elif direction == "Q":
self.userQuit()
else:
print "Sorry, that's not a valid direction."
self.living_room()
def kitchen(self):
print ("\nYou are in the kitchen. The water from the sink is slightly running. All of the"
+" cupboards in the kitchen have been left open, like someone has searched through them."
+" To your south is the dining room, and to your east is the living room. ")
direction = raw_input("Which direction would you like to go? (S)outh or (E)ast? You also have the option to (Q)uit. ")
if direction == "S":
self.dining_room()
elif direction == "E":
self.living_room()
elif direction == "N":
print "Sorry, you can't go north here."
self.kitchen()
elif direction == "W":
print "Sorry, you can't go west here."
self.kitchen()
elif direction == "Q":
self.userQuit()
else:
print "Sorry, that's not a valid direction."
self.kitchen()
def bed_room(self):
print ("\nYou are in the bedroom. One of the windows in the room is slightly ajar. The other window"
+" is shattered with a brick laying on the floor next to it. To your west is the dining room and"
+" to your north is the living room.")
direction = raw_input("Which direction would you like to go? (W)est or (N)orth? You also have the option to (Q)uit. ")
if direction == "W":
self.dining_room()
elif direction == "N":
self.living_room()
elif direction == "E":
print "Sorry, you can't go east here."
self.bed_room()
elif direction == "S":
print "Sorry, you can't go south here."
self.bed_room()
elif direction == "Q":
self.userQuit()
else:
print "Sorry, that's not a valid direction."
self.bed_room()
def dining_room(self):
print ("\nYou are in the dining room. It is very hard to see in here due to the dim lighting. You notice a staircase is the"
+" in the center of the room. To your north is the kitchen, and to your east is the bedroom.")
direction = raw_input("Which direction would you like to go? (N)orth or (E)ast or go (D)own the staircase? You also have the option to (Q)uit. ")
if direction == "N":
self.kitchen()
elif direction == "E":
self.bed_room()
elif direction == "D":
self.basement()
elif direction == "S":
print "Sorry, you can't go south here."
self.dining_room()
elif direction == "W":
print "Sorry, you can't go west here."
self.dining_room()
elif direction == "Q":
self.userQuit()
else:
print "Sorry, that's not a valid direction."
self.dining_room()
def basement(self):
print ("\nYou are now in the basement. A cloud of dust passes by you and makes it hard to breath. You move away from the area"
+" and you notice a key on the floor. You pick up the key and hold onto it. You may use it for later. ")
direction = raw_input("Press U to go upstairs. You also have the option to (Q)uit. ")
if direction == "U":
self.has_key = True
self.dining_room()
elif direction == "Q":
self.userQuit()
else:
print "Sorry, the only place you can go is back upstairs."
self.basement()
def main():
t = TextAdventure()
t.welcomeMessage()
t.living_room()
if __name__ == "__main__":
main()
精彩的比赛。玩得很开心。虽然它有点短,但感谢分享并保持良好的工作! 如果你多做一点OOP,你也可以提供一些用户操作。 :)
class TextAdventure(object):
def __init__(self):
self.has_key = False
def use(self):
print 'You used something'
def eat(self):
print 'You ate something'
def drink(self):
print 'You drank something'
def main():
t = TextAdventure()
actions = { 'use' : t.use, 'eat' : t.eat, 'drink' : t.drink }
a, b, c = actions.keys()
action = raw_input('Choose what to do [{0}, {1}, {2}]'.format(a,b,c))
if action in actions:
actions[action]()