There are multiple game libraries for Ruby, Gosu (website, Github, RubyGems) being the most well known. Gosu is 2D game development and features easy to use and game-friendly interfaces to 2D graphics and text (accelerated by 3D hardware), sound samples and music as well as keyboard, mouse and gamepad/joystick input.
Potentially also using Gamebox (website, Github), a framework for building and distributing games using Gosu. Or Chipmunk.
Maybe we can take some from http://guides.railsgirls.com/ruby-game/ and the gosu showcase as inspiration: http://www.libgosu.org/cgi-bin/mwf/board_show.pl?bid=2
Basic setup:
- ingredients (Ruby version, Gem version, os) > infobox
- What does 'game library mean', is there a difference between 'library' and 'gem'? > infobox
- what game are we making and why? (Valentine's game with Cupid shooting arrows at broken hearts?)
- what is a sprite? > infobox
- actual development / tutorial
- how to 'release' your game so your friends can use it too?
Getting Started
Just install the gem via gem install gosu
or add gem "gosu", "~> 0.8"
to your Gemfile and run bundle
.
To test whether everything works as expected, you can use this Hello World script:
require 'rubygems' # only necessary in Ruby 1.8
require 'gosu'
class MyWindow < Gosu::Window
def initialize
super(640, 480, false)
self.caption = 'Hello World!'
end
end
window = MyWindow.new
window.show
Play around with the example games:
cd $GEM_HOME/gems/gosu-0.7.48/examples
and then: ruby CptnRuby.rb
or: ruby RMagickIntegration.rb
or: ruby Tutorial.rb
Gosu 0.8.x+ relies on the SDL 2 library when working with OS X. We recommend installing Homebrew and then running brew install sdl2 libogg libvorbis
To install Gosu 0.8.x on Linux, you will need the following packages, even though the names will be different in every distribution: libsdl2-dev, libsdl2-ttf-dev, libpango1.0-dev, libgl1-mesa-dev, libfreeimage-dev, libopenal-dev, libsndfile-dev. https://github.com/jlnr/gosu/wiki/Getting-Started-on-Linux
So how does this work?
Let’s inspect some code, shall we? Open game.rb in your texteditor. See the
!/usr/bin/env ruby -w
require 'rubygems'
require 'gosu'
include Gosu
… right at the top of your file? Here we make sure we ‘call’ the necessary gem, so we can move on to our class (or multiple classes). So we have our
class Game < Window
end
… thing going on. The def’s you see within this Game class, are methods. Here we define which instructions the program should follow. Just take a look at the following snippet:
def draw
draw_quad 0, 400, Color::WHITE, 640, 400, Color::WHITE, 640, 500, Color::WHITE, 0, 500, Color::WHITE
if @dir == :left then
offs_x = -25
factor = 1.0
else
offs_x = 25
factor = -1.0
end
@cur_image.draw(@x + offs_x, @y - 49, 0, factor, 1.0)
end
Want to play around a bit? Copy the contents of game.rb
in a new .rb file. Save it and name it as you’d like. Now try and change some stuff in the game and run it in your terminal (ruby mynewgame.rb
) to see the changes.
You can try and create a new sprites.png! Don’t forget to call it here:
def initialize
super(640, 480, false)
self.caption = "Jump 'n Run"
@standing, @walk1, @walk2, @jump = *Image.load_tiles(self, "sprites.png", 100, 160, false)
@x, @y = 400, 0
@vy = 0
@dir = :left
@cur_image = @standing
end
And see the Game.new.show
? That creates a new instance. It has no memory, so when you get stuck in the game, you can just start a new game. Have fun!
Drawing in Gosu https://github.com/jlnr/gosu/wiki/Basic-Concepts
Have others play your game Do you want to share your game with other Mac users? Simply follow these steps:
Find the most recent Mac app wrapper on [github](https://github.com/jlnr/ruby_app/releases/)
Show the app’s package contents via the right-click menu in Finder
Edit the `Info.plist` file, and change the bundle identifier to match your game (`org.libgosu.UntitledGame` by default)
Copy your game files into the `Contents/Resources` subfolder
Rename your game’s main source file to `Main.rb`
And you’re done! You should now have a fully functional .app bundle.
The .app is a self-contained Ruby 2.1 installation with most of the standard library plus a few libraries that are often used together with Gosu.
To compile your Ruby game into an executable on Windows, you can manually use Ocra on the command line.
In theory, ocra should be as simple as these two lines in a command prompt:
gem install ocra
ocra my_ruby_game.rb
If you are using sound, you have to ship fmod.dll too. Other gems like ruby-opengl might require their own DLLs too. You can put them in a lib subdirectory and then include them on the command line:
ocra my_ruby_game.rb lib\*
To let your game find the DLLs you have to change the PATH environment like this somewhere early in your game:
ENV['PATH'] = File.join(GAMEROOT,"lib") + ";" + ENV['PATH']
or: https://github.com/Spooner/releasy/ another gem for both Windows and Mac, follow the readme of Github step by step!
For this tutorial, we will be using a language called ruby. Ruby is an open-source language created by Yukihoro "Matz" Matsumoto in Japan, and is an ideal language to start with because it's quite easy to understand!
You'll need access to a computer with:
If you don't own a computer, ask your parents or try and find a community center in your town that offers the possibility to use a computer connected to the internet.
Throughout the tutorial, we will use
this special font
for all the code that you need to type, both in your terminal and text editor.Info box: troubleshooting
These instructions will be different depending on whether you are using a Mac (OSX), a Windows, or a Linux machine. If something doesn't work or if you get stuck, you can try to search on the internet for a solution using the error message or what you are trying to do (eg. "open terminal") as well as your operating system (Windows, Linux, OSX) to find a solution.
Info box: the terminal
the terminal is a very powerful, but at first scary, tool. It allows you to "talk" to your computer by typing in commands. With it, you can do a lot of things: create/edit/delete/move files, make changes to your computer settings, make your computer "say" things, start and stop programs.. and even check for how many hours your computer has been turned on! we will be using the terminal mainly to run the little programs we create, and to move around in our folders.
Find your terminal by using a search on your computer, look for "Terminal" (or "Command Prompt"). Open it: you'll get a black screen that looks a bit intimidating! typing one of the words below (we call them "commands") and pressing enter in your terminal will do the following:
dir
- list the contents of the folder you are in (Windows)ls
- list the contents of the folder you are in (OSX/Linux)mkdir foldername
- create a folder with the name foldername (you can replace foldername with anything you want, like your name.)cd foldername
- move into the folder with name foldername (the folder with that name must already exist; you can use for example the folder you created above)ruby -v
- if you've installed ruby, you can check which version you have installed by using this command.info box: ruby
ruby comes bundled with an "interactive shell"; you can test some simple ruby code in there. to open the interactive shell, type
irb
into your terminal. If at any point you need to exit the irb, just typeexit
.ruby is pretty good at math; you can type in any sort of valid calculation and ruby will give you the answer. from you interactive shell, type something like
2 + 2
and press enter.amazing, right? it just gave you back the result of your calculation! Feel free to play some more with integers (this is what we call full numbers)
Next we can take a look at strings, variables, and simple methods.
Strings are, to put it simply, text. anything can be a string, even numbers, as long as you enter them in quotes, like this: "my string".
Variables are a way for us to save data (like strings, or integers). In order for you to save a variable, you have to "define" it. Let's try it! type the following into interactive shell and press enter:
name = "Anna"
now, type
name
and press enter. See what just happened? you asked ruby to give you the variable name and it gave you back the string you had saved in that variable.Methods: methods are "actions" on data. that's really all they are! for each data-type (integers and strings are both data types) there are different methods available. for example, ruby gives you the possibility to take a string and turn all the letters into capital letters! let's try it. Type in the following and press enter:
"Anna".upcase
that's right! everything uppercase. It also works if you use that method (upcase) directly on your name variable, like this:
name.upcase
try some of the following methods on your string:
downcase
,revert
,length
,empty?
,swapcase
and this is just the beginning - there is much, much more to be done with ruby...