为 uni 编写一个非常基本的基于文本的游戏...尝试拥有一个全局变量或一个“键”,如果已“找到”即允许访问某些区域。当找到键= 1。尝试使用以下代码访问房间时尝试确定结果的所有权(或其他方式):(我将仅包括相关的代码部分)
def playGame():
key = 0
location = "Porch"
showIntroduction()
while not (location == "Exit") :
showRoom(location)
direction = requestString("Which direction?")
location = pickRoom(direction , location)
def showRoom(location):
if location == "Porch":
showPorch()
if location == "Entryway":
showEntryway()
if location == "Kitchen":
if direction == "north":
return "DiningRoom"
if direction == "west":
if key == 1:
return "Stairs"
else:
printNow("You do not possess the Skeleton Key!")
return "Kitchen"
if direction == "south":
return "Entryway"
def showLR():
if key == 0:
printNow("...")
printNow("You enter the living room; dust covers the fireplace.")
printNow("Cobwebs shield a great armchair and it's contents from view.")
printNow("You spot an object glinting on the mantle, the dim light casting flickering shadows all around.")
printNow("The only exit lies behind, to the west")
printNow("...")
if key == 1:
printNow("...")
printNow("The living room shimmers in the dappled light.")
printNow("Cobwebs shield a great armchair and it's contents from view.")
printNow("The key in your pocket feels warm, or is it just your imagination?")
printNow("The only exit lies behind, to the west")
printNow("...")
希望以上内容有意义,大多数函数 (showEntryway() showPorch()) 已被省略,但它们的作用与 showLR() 类似。
每次我进入 LivingRoom 并运行时,我都试图让“密钥”作为变量工作:
if key == 0:
我收到一条错误消息,提示未找到变量,初始值不是从 playGame()...\
希望这是可以理解的,任何帮助将不胜感激!!!!谢谢 TG