我正在为艰难的方式学习Ruby的练习36编写文本冒险游戏:http://ruby.learncodethehardway.org/book/ex36.html
我想要包含'说明'作为一个选项玩家可以随时使用,但一旦玩家退出Room()函数并进入intructions()函数,我不确定如何将它们返回到适当的房间。我可以让说明()始终将玩家返回到开头,但有没有办法让他们回到同一个位置?
这是一个简单的例子,抱歉它不完整......我还在构建它:
puts
puts <<INTRO
"Welcome to the Cave of Indifference.
It doesn't much care for you. Beware!
There are deadly areas of this cave, but if you seek
it you may find the secret treasure and escape with your life."
INTRO
puts
puts "Type \'Instructions\' at any time for direction"
puts
sword = false
monster = true
treasure = false
def Command()
puts ">>> "
end
def dead(how)
puts how.to_s
puts "PLAYER DEAD!"
Process.exit(0)
end
def instruction()
puts "Rooms will have individual instructions"
puts "but here are some general items."
puts "west, east, north, south: goes that direction"
puts "look: look around the room"
puts "take: to take item or object"
end
def Room1()
puts "You are now at the cave entrance."
puts "You may go west or east. OR exit with your life!"
Command(); choice = gets.chomp()
if choice.downcase == "exit" && treasure = true
puts "Congratulations! You win!"
Process.break
elsif choice.downcase == "exit" && treasure = false
puts "Seriously?! Giving up already?"
puts "Fine. Here is what happens:"
dead("You stumble on your exit from the cave and trip
on a rock. The fall cracks your skull and you bleed
to death. Bye bye!")
elsif choice.downcase.include? "right"
#INPUT
elsif choice.downcase.include? "left"
#INPUT
elsif choice.downcase.include? "instructions"
instructions()
else
"That command makes no sense, try again."
end
end
Room1()
我还假设代码存在许多问题,并且非常感谢您的帮助,但不用担心我会继续努力并让它变得非常有趣:)
答案 0 :(得分:1)
您可以将指令方法指定为最后一个位置。
def instruction(last_room)
#do stuff
last_room.call()
end
您可以像这样调用该函数:
instructions(method(:Room1))
,其中Room1
是您要返回的方法的名称。
答案 1 :(得分:1)
您遇到的问题是没有说明返回Room1
(或RoomX
)。没有你做任何特别的事情,它会做到这一点。你需要的是:
@room = :Room1
while true
send(@room)
end
然后设置变量@room
以控制您所在的房间。
这不是世界上最好的方式,但它会让你开始。