In this tutorial, I am going to show you how to use Gosu 2D Ruby Game development library. I assume you have some experience with coding in ruby programming language. If you are not comfortable with loops, classes and functions then I suggest you to take a few hours to learn ruby programming language. As ruby is very easy to learn, you will pick up the concepts pretty quickly and it won’t take much time to continue with this tutorial.
What You Can Do With Gosu
Gosu is a 2D game development library for the Ruby and C++. It is not a complete gaming engine but It gets the job done with most of it’s features. There are few other extension libraries that you can use with Gosu like chipmunk, chingu, texplay and ashton for extended features.
There are few other libraries which are useful for the deployment of the game and for other use.
Installing Gosu and Ruby
Before we start building games, you need to install Gosu and Ruby on your computer. Depending on the type of the operating system you have, the instructions will vary. I am going to quickly point you towards the installation instructions of three popular operating systems.
Windows Instructions
In order to build ruby based games on Windows, you need to use Visual Studio C++ Express (or Professional) edition IDE on your computer. By installing visual studio, compiling the C++ and Ruby source files becomes easy.
Start by installing Ruby on your computer. Go to ruby lang and download Windows installer. After the installation, you need to install ‘rubygems‘ which makes it easier for you to download other development files. Once you install rubygems then installing gosu is very easy.
gem install gosu
If all goes well, You’re ready to program.
Mac OS X Instructions
Install latest version of ruby on your Mac. After installing ruby, you need to download the rubygems package. After that just run the following command.
gem install gosu
This will install gosu on your system and you’re ready to go.
Linux Instructions
Gosu depends on multiple development libraries to work on Linux. Your system needs the following files before installing gosu:
build-essential
freeglut3-dev
libfreeimage-dev
libgl1-mesa-dev
libopenal-dev
libpango1.0-dev
libsdl-mixer1.2-dev
libsdl-ttf2.0-dev
libsndfile-dev
libxinerama-dev
Once you install these files you can Install ruby 1.9.x and ruby gems packages.
ruby1.9.1-dev
rubygems
As there are many distros out there, each with their own installation method. You can download these files from the respective repository using Software Center or download via command line by invoking command line installation tool like apt-get or yum.
e.g.
sudo apt-get install build-essential freeglut3-dev libfreeimage-dev libgl1-mesa-dev libopenal-dev libpango1.0-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev libsndfile-dev libxinerama-dev
sudo apt-get install ruby1.9.1-dev rubygems
After installation of these deveopment packages you can install gosu.
gem install gosu
Please check the official site of Gosu If there are any updated set of dependencies. After installation, you are ready to execute gosu based ruby programs on your linux system.
Additional Libraries
Releasy : If you wish to distribute game on windows and Mac as native executable then you need to install releasy. Install the gem using following command:
gem install releasy
Texplay : Texplay lets you manipulate images in gosu. It is a C extension that is perfect for fast pixel-perfect collision detection in games, and fast image manipulation. By using this library you can get the pixel and draw the shapes during the runtime (like circle, line boxes etc). Install releasy gem using following command:
gem install texplay
Chingu: Once you get comfortable with the gosu library then you can use chingu for additional game logic and other core framework features. You can install chingu gem using this command:
gem install chingu
There is also another altarnative library rmagick, easy to use, but much harder to distribute compared to texplay. You can download rmagick using following command:
gem install rmagick
Chipmunk Physics Library: You can extend your gosu game physics using the chipmunk library. This gem can be installed using the command:
gem install chipmunk
ruby-opengl : You can use the standard opengl for the 3D effects and few other effects with gosu. You can install the gem using following command:
gem install ruby-opengl
There are few other libraries that you can install that will help extend the game you design using gosu. Keep tab on gosu forums for additional updates on the new or existing libraries.
Executing Ruby Programs
Open command prompt (or terminal for linux users), navigate to the directory where you have stored the source files and then type the following command.
ruby filename.rb
filename.rb is the source file that you want to execute. You can replace the name of the file with your own file name. Make sure you are typing this command in the directory is stored, otherwise ruby will flag an error. Also do note the file name correctly while typing because wrong file name also returns the error.
We are going to cover some of the basics of Gosu library. At the end of this chapter, you will be able to write graphical programs using gosu library.
If you want quick Video tutorial, please check out the video below.
Let’s first start by creating an empty window with Gosu.
Code:
require 'rubygems'
require 'gosu'
class DemoWindow < Gosu::Window
def initialize
super(640, 400, false)
end
end
DemoWindow.new.show
Code Explanation:
First two lines initialize rubygems and gosu library in order. In third line, we have a class called DemoWindow class that inherits from the Gosu::Window class. It creates a new window of size 640 x 400, and runs the main loop. That is all it does for now.
Once you run this code, It should show you the window of 640×400 without title.
We are going to add the caption to the window so that you can see the name of the game in window title.
Let’s add the caption in our program.
require 'rubygems'
require 'gosu'
class DemoWindow < Gosu::Window
def initialize
super(640, 400, false)
self.caption = "Gosu Demo Window"
end
def update
end
def draw
end
end
DemoWindow.new.show
Check the output. It should show you Window caption of “Gosu Demo Window”.
Code explanation:
In this program we have added update() and draw() methods. These two methods are overrides of Gosu::Window’s member functions. These two methods are very important because update() method should contain your game logic and movement. The update() method is called 60 times per second in order to process the game logic.
In draw() method, you should keep the code that redraws the objects and background. It should not contain the logic of your game. Whenever the program requires to redraw the background, it calls for draw() method. So the draw() method is always in the loop to either fetch the input or to redraw the game background, unless program calls for close().
When the program is executed, it invokes the show() method which triggers the input events and then moves to update() method and processes the game logic. After that it goes for the draw() method and then exits through close() method if the call is made for that.
Make changes to this program. Write your own caption. You can also change the window size. Make sure you check display size combination, If you want to experiment the size of the game window.
Basic Shapes
Now that we have managed to create a simple window, let’s see how to draw some basic shapes. We have three methods to draw the basic shapes.
draw_quad()
draw_triangle()
draw_line()
Let’s take a look at these methods one by one. But first a small note on the co-ordinate values. We are creating shapes with the help of X, Y and Z co-ordinate values. For example, when it comes to drawing the line, we fill the color of line starting from X to X1 and Y to Y1, which makes up a line on our screen. In case of triangle, we need three X values for the position of triangle and three values for Y. And similarly we have to take four values of X and Y for quadrangle.
In order to make this more clear, we will write a simple code for each shape.
draw_quad() method
Code:
require 'rubygems'
require 'gosu'
class DemoWindow < Gosu::Window
def initialize
super(640, 400, false)
self.caption = "Demo of draw_quad()"
end
def draw
x = 300
y = 200
size = 120
draw_quad(x-size, y-size, 0xffff8888, x+size, y-size, 0xffffffff, x-size, y+size, 0xffffffff, x+size, y+size, 0xffffffff, 0)
end
end
DemoWindow.new.show
Code Explanation: As you can see, we have written the draw_quad() method inside the draw() method. We have specified the X and Y co-ordinate values and substracted and added it with the size. This way we are giving the method the values it needs to draw the quadrangle. We have also specified the color values to fill the quadrangle. You can specify the Z=0 or leave that part out. I have kept it purposefully in order to explain you about the Z value.
draw_triangle() Method
require 'rubygems'
require 'gosu'
class DemoWindow < Gosu::Window
def initialize
super(640, 400, false)
end
def draw
x = 300
y = 200
size = 120
draw_triangle(x-size, y-size, 0xff00ff00, x+size, y-size, 0xff00ff00, x-size, y+size, 0xff00ff00)
end
end
DemoWindow.new.show
Code Explanation: The draw_triangle() method is fetching the values for X and Y, using the x-size and y-size and x+size. These three values makes up the triangle. Just pass the specific values for X and Y and you can get the triangle as per your values.
draw_line() Method:
require 'rubygems'
require 'gosu'
class DemoWindow < Gosu::Window
def initialize
super(640, 400, false)
end
def draw
x = 300
y = 200
size = 120
draw_line(x-size, y-size, 0xff00ffff, x+size, y+size, 0xff00ffff)
end
end
DemoWindow.new.show
Code Explanation: In this code we have passed two X and two Y co-ordinate values, along with color code. That is all this method needs to draw the line.
Background Color
As per the current update of the libgosu, there seems to be no built-in method for the background and you have the only two options in this case.
Use image to fill the screen (This applies to most of the games which relies on graphic images for the background).
Use quadrangle to fill the screen and pass -Float::INFINITY for z to make sure it is drawn below everything.
That’s all for now. From here you can go ahead and learn more about making games with Gosu. I have tried to cover some basic concepts, I hope you have some good foundation from which you can develop further games. If you have any questions or suggestions, feel free to let me know about it in comments. You can also shout @maheshkale on twitter.