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:
- Write a class that inherits from Gosu::Window
- In the initializer call super with the width, height, and whether you want your app to be fullscreen
- define a draw method that uses the Gosu library functions to render graphics on the screen
- define an update method that makes changes to your game
- Write Class.new.show
- 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.
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