【发布时间】:2021-06-02 12:46:03
【问题描述】:
所以对于一个任务,我正在构建一个简单的选择你自己的冒险终端游戏。
我有一个无法解决的未定义局部变量或方法错误。
prompt.select("Do you stop to listen? Do you leave the path to investigate? or do you spur your horse on faster?") do |menu|
menu.choice "Stop to listen", -> {paragraph_4}
menu.choice "Leave path and investigate", -> {paragraph_5}
menu.choice "Spur horse on faster", -> {paragraph_7}
end
prompt = TTY::Prompt.new
$paragraph_4 = def paragraph_4
prompt = TTY::Prompt.new
puts ""
puts ""
puts " Stopping to listen it seems whatever is moving in the brush is doing the same. Your horse stamps a foot impatiently."
prompt.select("Will you dismount? Do you call out? or do you continue on your way?") do |menu|
menu.choice "Dismount", -> {paragraph_7}
menu.choice "Call out", -> {paragraph_8}
menu.choice "Continue", -> {paragraph_7}
end
$paragraph_5 = def paragraph_5
uts ""
puts ""
puts ""
puts " Suddenly a giant spider leaps from the brush and burys its huge fangs in your neck."
puts ""
puts " YOU HAVE DIED
这仅适用于每个文件的第一段。就像我在提示符下进行第一个选择一样,但是第二个或第三个-
game.rb:77:in block (2 levels) in <main>': undefined local variable or method paragraph_2' for main:Object (NameError)
我不明白为什么?
prompt = TTY::Prompt.new
$paragraph_1 = def paragraph_1
prompt = TTY::Prompt.new
puts ""
puts ""
puts " 'Ah the north road, a wise choice friend as your way will meander towards Ered Mithrin and Durins folk of the Grey Mountains'
'Perhaps like Gandalf and his companions who whence this way you will find Erebor the lonely mountain, whereby Smaug the Terrible met his end.'
'Now friend the hour grows late and you best away lest you not find a clearing within the woods to light a fire for camp at night.
'Bidding the kind old man well and flicking a silver coin his way you nudge your horse with your knees and head towards the woods.'
'As you approach the branchs of the dark and twisted trees seem to reach out and their trunks rear up and lean forward to engulf you. Soon the space'
'between the boughs becomes thinner and thinner and although what seemed like moments ago late afternoon now seems like a twighlight under the weight of the ancient forest."
"As you walk deeper into the dark woods you hear a rustling and from the corner of your eye you see movement to your left"
prompt.select("Do you stop to listen? Do you leave the path to investigate? or do you spur your horse on faster?") do |menu|
menu.choice "Stop to listen", -> {paragraph_4}
menu.choice "Leave path and investigate", -> {paragraph_5}
menu.choice "Spur horse on faster", -> {paragraph_7}
end
$paragraph_2 = def paragraph_2
prompt = TTY::Prompt.new
puts ""
puts ""
puts " 'Ah the middle way, a sensible choice friend as your way will take you through the lands of the Sindarin or Silvan Elves and Thranduil's realm'
'Now friend the hour grows late and you best away lest you not find a clearing within the woods to light a fire for camp at night.
'Bidding the kind old man well and flicking a silver coin his way you nudge your horse with your knees and head towards the woods.'
'As you approach the branchs of the dark and twisted trees seem to reach out and their trunks rear up and lean forward to engulf you. Soon the space'
'between the boughs becomes thinner and thinner and although what seemed like moments ago late afternoon now seems like a twighlight under the weight of the ancient forest."
""
"As you walk deeper into the dark woods you hear a rustling and from the corner of your eye you see movement to your left"
prompt.select("Do you stop to listen? Do you leave the path to investigate? or do you spur your horse on faster?") do |menu|
menu.choice "Stop to listen", -> {paragraph_4}
menu.choice "Leave path and investigate", -> {paragraph_5}
menu.choice "Spur horse on faster", -> {paragraph_7}
end
【问题讨论】:
-
Lambda 是闭包。 #paragraph_2 方法在哪里定义?
-
我已经添加了另一个代码 sn-p 如果你能看一下 Todd 将不胜感激。
-
你在 lambdas 中调用方法。方法不在范围内。
-
您也将方法分配给全局变量,但它们不是 lambda,您永远不会尝试调用它们。目前尚不清楚您为什么认为这会奏效,而且您的示例并不能真正充分解释您的思维过程以提供进一步帮助。
-
我是 Ruby 新手,学习编码才 3 个月,请原谅我的无知。我想要做的是通过 tty 提示从每个段落分支出来,然后再次分支,直到故事结束。如果您能建议这将如何工作,将不胜感激。