【问题标题】:Game text based on ruby language - OOP基于 ruby​​ 语言的游戏文本 - OOP
【发布时间】:2015-04-15 17:46:37
【问题描述】:

我正在尝试为用 Ruby 编写的游戏集成文本。为此,我想我可以将文本本身保存在哈希中。 我希望用户选择播放的语言。 (两种语言)。

我已经编写了游戏代码、字典,并且我已经分离了在另一个文件中打印文本的调用。这样我就可以保持代码干净了。

我打算保留散列,只包含不同语言的文本。

当我从控制台运行主文件(my_game.rb) 时出现以下错误。

my_game.rb:62:in `enter': undefined local variable or method `eng' for
#<CentralCorridor:0xb7d73d74> (NameError)
           from my_game.rb:35:in `play'
           from my_game.rb:257:in `<main>' code here

我修改了原代码,可以看成是注释块中的第一段。第一行注释掉块相当于文本,写在另一个文件中。

class CentralCorridor < Scene

def enter()
    puts "#{Language.get(eng, 'sc_enter')}"
=begin
    puts "The Gothons of Planet Percal #25 have invaded your ship and destroyed"
    puts "your entire crew. You are the last survivieng member and your last"
    puts "mission is to get the neutron destruct bomb from the Weapon Armory,"
    puts "put it in the bridge, and blow the ship up after getting into an "
    puts "escape pod."
    puts "\n"
=end

这似乎是返回错误的方法。我不确定是否应该修改它以接受来自模块的书面参数。 它在my_game.rb内实现

def play()
    current_scene = @scene_map.opening_scene()
    last_scene = @scene_map.next_scene('finished')

    while current_scene != last_scene
      next_scene_name = current_scene.enter()
      current_scene = @scene_map.next_scene(next_scene_name)
    end

    # be sure to print out the last scene
    current_scene.enter()
  end
end

我已经在模块内部编写了哈希代码,并在my_game.rb中引用了

require "./hash_compos"
require "./language.rb"

class Scene
  def enter()
    puts "This scene is not yet configured. Subclass it and implement enter."
    exit(1)
  end
end
...

以下代码是我在模块中实现的 Dictionary 的一部分。该文件名为language.rb。并且模块同名Language

...
      def Language.get(miDict, key, default=nil)
        # Gets the value in a container for the given key, or the default.
        i, k, v = Language.get_slot(miDict, key, default=default)
        return v
      end

      def Language.set(miDict, key, value)
        # Sets the key to the value, replacing any existing value.
        container = Language.get_container(miDict, key)
        i, k, v = Language.get_slot(miDict, key)

        if i >=0
          container[i] = [key, value]
        else
          container.push([key, value])
        end
      end
...

这是哈希。写在hash_compos.rb。通过Language.rb 中创建的函数,我实现了一个字典来尝试包含最少的代码。

require './language.rb'

# create a mapping of a scene to text
eng = Language.new()
Language.set(eng, 'The Gothons of Planet Percal #25 have invaded your ship
and destroyed\nyour entire crew. You are the last survivieng member and your
last\nmission is to get the neutron destruct bomb from the Weapon Armory,\nput 
it in the bridge, and blow the ship up after getting into an 
\nescape pod.\n', 'sc_enter')
Language.set(eng, 'txt', 'sc_cc')
...

【问题讨论】:

    标签: ruby oop


    【解决方案1】:

    问题在于您将eng 定义为hash_compos.rb 中的局部变量

    this answer 中的相关问题所述

    您无法访问所需文件中定义的局部变量

    此范围问题的快速解决方法是将eng 定义为hash_compos.rb 中的常量。为此,只需将变量重命名为 ENG 并相应地更新所有引用它的文件。

    这将使变量可以从需要hash_compos.rb 的文件中访问。

    【讨论】:

    • 问题是我需要输脑,但没有医生在。
    • eng 变量必须在模块内工作。我不确定我在说什么,但我可以发誓该变量必须是构造函数中的一个参数,该参数与打印哈希值的方法一起使用。我认为如果我构建一个能够打印作为参数传递的相反值的方法,问题就会得到解决。我将编辑查询,并从字典中添加整个代码。
    【解决方案2】:

    好的,解决方案比我最初想象的要简单得多。 我选择遵循两者都建议的迹象,所以首先要做的事情。谢谢你们俩。

    问题在于我正在使用旨在解决特定问题的严格方法。 我创建了一个新的散列,并将所需的文本放入其中。 然后我在“main”中注明了.rb 文件名。 我会写字典的命名法,以防万一有人关心。填充始终是可选的!!!

    #!/path/to/your_bin
    

    表明在文件中,不是打印文本的最佳方式。所以我推荐, 对文件进行编码,就好像我们想为帖子应用降价一样。 一个简单的:

    # Encoding: utf-8
    

    我实现了哈希,声明了一个常量变量,如擦除所示。

    VARIABLE = {"key" => "value"}
    

    然后你只需要按名称调用变量。直接地。 Ruby 负责在哈希键中提取指示值。

    VARIABLE["key"]
    

    文本格式,适合编码到文件中。转义序列不是必需的。

    问题解决了。

    【讨论】:

      【解决方案3】:

      问题是在下面一行你试图使用eng,但该变量不存在:

      puts "#{Language.get(eng, 'sc_enter')}"
      

      因此,根据您在其他地方使用的代码,您似乎可以在该行之前分配给eng

      eng = Language.new
      puts "#{Language.get(eng, 'sc_enter')}"
      

      【讨论】:

      • 您建议的解决方案根本不起作用。错误消失了,但没有打印散列中的书面文本。此外,该变量已经在模块中初始化。我不明白为什么要重新初始化¿?
      猜你喜欢
      • 2011-01-13
      • 2021-06-25
      • 1970-01-01
      • 2013-07-12
      • 1970-01-01
      • 2013-07-08
      • 2015-09-10
      • 2023-01-08
      • 1970-01-01
      相关资源
      最近更新 更多