Introduction to Ruby Game Programming
I always wonder if one can write game using Ruby language. I do some research and find out that it is possible. I will take this opportunity to introduce to you game programming in Ruby. 1. Model a deck of card Sure this is simple enough, but it can give us a basic idea of a deck of card which ...
I always wonder if one can write game using Ruby language. I do some research and find out that it is possible. I will take this opportunity to introduce to you game programming in Ruby.
1. Model a deck of card
Sure this is simple enough, but it can give us a basic idea of a deck of card which can be used to develop a more advanced card game.
deck=[] suits = %w[ Heart Diamond Club Spade ] counts = %w[ Ace 2 3 4 5 6 7 8 9 10 Jack Queen King ] suits.each do |suit| counts.each do |count| deck << "#{count} of #{suit}" end end deck.shuffle puts deck
This simple code make a deck of card which consists of four suits and count from Ace to King. Then we use the shuffle to make a deck of random cards.
2. 2D games
We will introduce Gosu game library which is the most important, I have to say. I is a great libray for building 2D game.
i. Requirements and Installations
sudo apt-get update sudo apt-get install build-essential libsdl2-dev libsdl2-ttf-dev libpango1.0-dev libgl1-mesa-dev libfreeimage-dev libopenal-dev libsndfile-dev # To install Ruby - if you are using rvm or rbenv, please skip this step sudo apt-get install ruby-dev # If you are using rvm or rbenv, do not use 'sudo' sudo gem install gosu
I myself use both the linux and windows and later I also find out that one can even run in Visual C++ if you prefer develop in the latter. There are very good document related to this.
One should have a good hardware capacity to run game at least with RAM 512MB or more.
ii.hello world program
We will start first with our hello world program using gosu library.
require 'gosu' class GameWindow < Gosu::Window def initialize(awidth=320, height=240, fullscreen=false) super self.caption = 'Hello' @message = Gosu::Image.from_text( self, 'Hello World!', Gosu.default_font_name, 40) end def draw @message.draw(15, 15, 0) end end window = GameWindow.new window.show
One can run with:
ruby hello_world.rb if you name it hello_world.rb
Here we try to understand what is going on in the code above: We have extended Gosu::Window with our own GameWindow class, initializing it as 320x240 window. super passed awidth, height and fullscreen initialization parameters from GameWindow to Gosu::Window.
Then we defined our window’s caption, and created @message instance variable with an image generated from text "Hello, World!" using Gosu::Image.from_text.
We have overridden Gosu::Window#draw instance method that gets called every time Gosu wants to redraw our game window. In that method we call draw on our @message variable, providing x and y screen coordinates both equal to 10, and z (depth) value equal to 0.
We will cover more on ruby game development. This is just the first step of our journey