Search code examples
rubylibgosu

Gosu 2D Game Development Programming Example Error


I am learning Ruby at the moment so definitely a beginner and have been playing with the Gosu 2D game development and am having issues with the following code and unsure what I have missed/done incorrectly.

Code is:

require 'rubygems'
require 'gosu'

class MyGame < Gosu::Window
  def initialise
    super 300, 400, false
    self.caption = "Gosu Tutorial Game"
  end

  def update
  end

  def draw
  end
end

window = MyGame.new
window.show

The above code is stored within the file my_game.rb

When I attempt to execute the code on the command line typing:

> ruby my_game.rb

I get the following error:

my_game.rb:17:in `initialize': wrong # of arguments(0 for 3) (ArgumentError)
    from my_game.rb:17:in `new'
    from my_game.rb:17:in `<main>'

Solution

  • You have a typo in initialise.

    You meant to write initialize to provide a constructor for your game and call Gosu::Window constructor (super) with 3 parameters. But since you haven't really defined initialize (instead you've defined a distinct initialise method) - MyGame.new tries to call Gosu::Window#initialize which accepts 3 arguments, but you provide 0 to MyGame.new - that's what the error message tries to convey.

    If you'll fix the typo initialise -> initialize, MyGame.new will call your constructor with no arguments, and it will then provide the needed 3 arguments via super call.