python – 文本冒险游戏布尔变量对我不起作用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 文本冒险游戏布尔变量对我不起作用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4383字,纯文字阅读大概需要7分钟。
内容图文
![python – 文本冒险游戏布尔变量对我不起作用](/upload/InfoBanner/zyjiaocheng/726/579127bdc8904e1aa72164e7fafa3b5f.jpg)
我的文字冒险游戏有点麻烦.我们的想法是从起居室开始,到地下室去抓钥匙,当你再次进入起居室时,你应该赢.当我执行我的代码时,它只是让我进入房间,布尔值应该告诉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()
解决方法:
必须告诉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()
典型的游戏代码看起来比你的更像.它有一些优点,首先是涉及的递归会更少,因此“堆栈”不会那么深.其次是它允许你在房间之间应用一些共同的逻辑,例如跟踪玩家的位置.
内容总结
以上是互联网集市为您收集整理的python – 文本冒险游戏布尔变量对我不起作用全部内容,希望文章能够帮你解决python – 文本冒险游戏布尔变量对我不起作用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。