Gosu Makes Writing Games Easy

Gosu is a fantastic little library for making basic 2D games using Ruby. There are extensions to add 3D graphics, common game logic, and a physics engine. You install Gosu the normal ruby way:

sudo gem install gosu

The basics of Gosu are simple:

  1. Write a class that inherits from Gosu::Window
  2. In the initializer call super with the width, height, and whether you want your app to be fullscreen
  3. define a draw method that uses the Gosu library functions to render graphics on the screen
  4. define an update method that makes changes to your game
  5. Write Class.new.show
  6. That’s it

Gosu automatically calls the draw method followed by the update method followed by draw again. It does this as often as it can which means the framerate varies depending on how complicated your draw and update methods are.

With this basic framework you can make simple animations and 2D games. Here is a simple animation of a square going diagonally across the screen.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
require 'rubygems'
require 'gosu'

class Basic < Gosu::Window
  def initialize
    super(800, 800, false)
    @pos = 10
  end

  def draw
    color = Gosu::Color::FUCHSIA
    self.draw_quad(@pos, @pos, color,
                   @pos + 10, @pos, color,
                   @pos + 10, @pos + 10, color,
                   @pos, @pos + 10, color)
  end

  def update
    @pos = @pos + 1
  end
end

Basic.new.show