Joe Yates' Blog

Programming and DevOps

Hello Gosu!

I’m trying out Gosu, which is a 2D game library written in C++ with Ruby bindings (Wikipedia article).

The Ruby documentation is sparse but sufficient.

The gem is easy to install: $ gem install gosu

A ‘Hello World!’ app requires about 20 lines of code:

Hello World in Gosu - hello_world.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
require 'rubygems'
require 'gosu'

class HelloWindow < Gosu::Window

  def initialize
    super( 250, 100, false )
    @background_image = Gosu::Image.new( self, 'hello_world.png' )
  end

  def draw
    @background_image.draw( 0, 0, 1 )
  end

  def button_down(id)
    close
  end

end

HelloWindow.new.show

Run it like this: $ ruby hello_gosu.rb

The program creates the window shown at the start of this article.

The complete code is in a GitHub repository.