Ruby works for games. Seriously. It is not C++ or Unity, but for 2D games and prototypes, it gets the job done fast.
Why Ruby?
You already know Ruby. That is the main reason. No need to learn a new language just to make a simple game. Ruby's syntax is clean, so your game code stays readable.
The Tools
Three libraries worth knowing:
Gosu - The go-to for 2D games. Handles windows, graphics, input, and audio. Simple API. Works on Mac, Windows, and Linux.
Chingu - Built on Gosu. Adds game states, animations, and collision detection. Good for bigger projects.
DragonRuby - Commercial toolkit. Fast, cross-platform, exports to consoles. Costs money but saves time.
We will use Gosu here. It is free and simple.
Installation
gem install gosuThat is it. On Mac you might need SDL2:
brew install sdl2Your First Game Window
require 'gosu'
class Game < Gosu::Window
def initialize
super(640, 480)
self.caption = "My Game"
end
end
Game.new.showRun this. You get a black window. That is your canvas.
The Game Loop
Gosu calls two methods every frame:
update- runs 60 times per second. Handle logic here.draw- runs after update. Render your sprites here.
class Game < Gosu::Window
def initialize
super(640, 480)
self.caption = "My Game"
@x = 0
@y = 0
end
def update
# Game logic goes here
end
def draw
# Rendering goes here
end
endLoading and Drawing Sprites
def initialize
super(640, 480)
@player = Gosu::Image.new("player.png")
@x = 100
@y = 100
end
def draw
@player.draw(@x, @y, 0)
endThe third argument is the Z-order. Higher numbers draw on top.
Handling Input
Check for key presses in update:
def update
@x -= 5 if Gosu.button_down?(Gosu::KB_LEFT)
@x += 5 if Gosu.button_down?(Gosu::KB_RIGHT)
@y -= 5 if Gosu.button_down?(Gosu::KB_UP)
@y += 5 if Gosu.button_down?(Gosu::KB_DOWN)
endArrow keys now move your sprite. 5 pixels per frame at 60 FPS = 300 pixels per second.
Complete Example
Here is a working game. Player moves with arrow keys:
require 'gosu'
class Game < Gosu::Window
def initialize
super(640, 480)
self.caption = "Ruby Game"
@player = Gosu::Image.new("player.png")
@x = 320
@y = 240
@speed = 5
end
def update
@x -= @speed if Gosu.button_down?(Gosu::KB_LEFT)
@x += @speed if Gosu.button_down?(Gosu::KB_RIGHT)
@y -= @speed if Gosu.button_down?(Gosu::KB_UP)
@y += @speed if Gosu.button_down?(Gosu::KB_DOWN)
# Keep player on screen
@x = @x.clamp(0, 640 - @player.width)
@y = @y.clamp(0, 480 - @player.height)
end
def draw
@player.draw(@x, @y, 1)
end
end
Game.new.showSave as game.rb. Drop a player.png in the same folder. Run with ruby game.rb.
No Image? Draw a Rectangle
Do not have art yet? Use draw_rect:
def draw
Gosu.draw_rect(@x, @y, 32, 32, Gosu::Color::RED)
endRed square. Works for prototyping.
Adding Sound
def initialize
super(640, 480)
@jump_sound = Gosu::Sample.new("jump.wav")
end
def button_down(id)
if id == Gosu::KB_SPACE
@jump_sound.play
end
endUse button_down for one-shot actions like jumping. Use button_down? in update for continuous actions like movement.
What Next
Start small. Make Pong. Then make a simple platformer. Then add enemies.
Gosu has good docs at libgosu.org. The wiki has tutorials for animations, tilemaps, and more.
Ruby games will not run on phones or consoles. For that, look at DragonRuby. But for desktop games and game jams, Gosu is solid.
Build something. Ship it. That is how you learn.