在Ruby中构建事件循环时,如何避免无限递归?(How do I avoid infinite recursion when building an event loop in Ruby?)
我想在Ruby中实现一个游戏循环,但是我当前的实现收到的“堆栈级别太深”(SystemStackError)。
这是我在我的俄罗斯方块般的下滑块游戏中取得的成就:
# falling block game class Tetris class EndGame < StandardError; end def start @canvas = Canvas.new @canvas.banner = "New game" @canvas.draw update end def update step update rescue EndGame puts "Game over!" end def step puts "Take one step..." # TODO: do stuff here end # draws our game board class Canvas SIZE = 10 attr_accessor :banner, :board def initialize @board = SIZE.times.map { Array.new(SIZE) } end def update(point, marker) x, y = point @board[x, y] = marker end alias_method :draw, :to_s def draw [banner, separator, body, separator].join("\n") end private def separator "=" * SIZE end def body @board.map do |row| row.map { |e| e || " " }.join end.join("\n") end end end game = Tetris.new game.start产生此错误:
Take one step... Take one step... Take one step... Take one step... Take one step... Take one step... Take one step... Take one step... Take one step... Take one step... gameloop.rb:20:in `puts': stack level too deep (SystemStackError) from gameloop.rb:20:in `puts' from gameloop.rb:20:in `step' from gameloop.rb:13:in `update' from gameloop.rb:14:in `update' from gameloop.rb:14:in `update' from gameloop.rb:14:in `update' from gameloop.rb:14:in `update' from gameloop.rb:14:in `update' ... 10067 levels... from gameloop.rb:14:in `update' from gameloop.rb:14:in `update' from gameloop.rb:9:in `start' from gameloop.rb:59:in `<main>'UPDATE
Ruby默认情况下不启用Tail Call Optimizations,但可以在Ruby VM中启用它。
RubyVM::InstructionSequence.compile_option = { tailcall_optimization: true, trace_instruction: false }I want to implement a game loop in Ruby, but my current implementation receives a 'stack level too deep' (SystemStackError).
This is how far I got on my Tetris-like falling block game:
# falling block game class Tetris class EndGame < StandardError; end def start @canvas = Canvas.new @canvas.banner = "New game" @canvas.draw update end def update step update rescue EndGame puts "Game over!" end def step puts "Take one step..." # TODO: do stuff here end # draws our game board class Canvas SIZE = 10 attr_accessor :banner, :board def initialize @board = SIZE.times.map { Array.new(SIZE) } end def update(point, marker) x, y = point @board[x, y] = marker end alias_method :draw, :to_s def draw [banner, separator, body, separator].join("\n") end private def separator "=" * SIZE end def body @board.map do |row| row.map { |e| e || " " }.join end.join("\n") end end end game = Tetris.new game.startProduces this error:
Take one step... Take one step... Take one step... Take one step... Take one step... Take one step... Take one step... Take one step... Take one step... Take one step... gameloop.rb:20:in `puts': stack level too deep (SystemStackError) from gameloop.rb:20:in `puts' from gameloop.rb:20:in `step' from gameloop.rb:13:in `update' from gameloop.rb:14:in `update' from gameloop.rb:14:in `update' from gameloop.rb:14:in `update' from gameloop.rb:14:in `update' from gameloop.rb:14:in `update' ... 10067 levels... from gameloop.rb:14:in `update' from gameloop.rb:14:in `update' from gameloop.rb:9:in `start' from gameloop.rb:59:in `<main>'UPDATE
Ruby does not enable Tail Call Optimizations by default, but it can be enabled in the Ruby VM.
RubyVM::InstructionSequence.compile_option = { tailcall_optimization: true, trace_instruction: false }最满意答案
使用while true ...循环而不是递归。 您还可以添加一个短暂的sleep以避免更多的更新。
Use a while true ... loop instead of recursion. You can also add a short sleep to avoid more updates than necessary.
更多推荐
发布评论