My First Game Programing Experience with Ruby

This is in no way a game programming article. I am just narrating my experience of writing a simple game for first time in life. All those who are interested in writing a computer game but have absolutely no idea of how to go about might find some information here.

Screenshot from my game.

Whats/Why Ruby ?

Ruby is an interpreted pure object oriented language. It is certainly not the best language to write games in. But you can still use it to develop some very simple games. Actually, I am learning Ruby these days to try out Rails. The idea of learning a new language by writing a game thrilled me. This article is in no way Ruby specific.

Doing Graphics ?

This is one important question for all new comers to game programing. Even I had no idea about it. In the past I tried writing a game or two with entry level C++ libraries, (and once even with javascript!), but I was not impressed. Then I understood that any library that could do graphics is not really sufficient. So I had to learn some library that was specifically meant for game programing. Now, I am an average programmer, so I am always on lookout for easy alternatives. This is when I came across SDL. SDL (Simple DirectMedia Layer) is a cross-platform library for multimedia programing. It provides low level access to keyboard, joystick, mouse,audio and 3D hardware. SDL is written in C, but it has bindings to several languages (including Ruby). This makes it really useful. The advantage of using SDL with Ruby is you can exploit cool OOP features of Ruby and not worry about things like garbage collection,compilation,OS. The biggest disadvantage is ‘speed’. But then I am not writing a ‘3D first person shooter’ game. If you still wonder (like I used to) ‘how can I play two audio files simultaneously’ check out the SDL API for playing sound. SDL makes it really easy.

Another screenshot from my game.

Heard of Sprites ?

Well, I had not, till recently. This is a very important concept of game programing. You probably cant write any good 2D game without knowing about ‘sprites’. So what are they? Have you notice the way charactors or objects in games are animated . When a charactor walks you can actually see his legs moving. Even stationary object exhibit some form of animated behavior. This all is achieved through ‘sprites’. A sprite is collection of images, which are shown one after another to animate an object. Generally these images are stored sequentially (frame wise) in a bigger image. This has following advantages:

  • Each image need not be loaded separately from disk.
  • Once you load single image containing the entire collection into memory, this speeds up retrieval and display of each frame, since they lie in adjacent memory locations. This can be very useful for certain types of computers\consoles.

UFO sprite. (Notice the slight difference in height of two images which animates the UFO)

My truck ! (Left facing image when user clicks left arrow and another for right arrow).

Spaceship. Four images for movement in four directions.

Sprites are generally designed by specialized people. Generating sprites requires lot of creativity and experience. You should be able to create cool animation with minimum frames. Most good game programmers will find this part tricky. If you are making a commercial game then you better hire some artist for sprites. However for my simple game, I used GIMP to create some basic sprites. These are png files with transparent background.

Collision Detection

Another important element of any game. As the name suggests it refers to detecting when boundaries of two objects overlapped or touched each other. For example, a missile hitting a target. The missile and target both could be sprites. A game can contain many sprites and every sprites may require to be checked for collision. Collision detection involves lot of processing. In my game , I need to check for collisions between every missile and UFO on screen. This off course can be optimized, but that is where complexity is involved.

Game Loop

A game look is similar to windows message loop. Simply put, its a while loop which looks something like this:

while(! IsGameOver)
{
// Scan user input
// Move all sprites by 1 step
// Detect collisions
// Update game status (IsGameOver =true or false)
// Optional delay
}

I think it will require lot of skill and practice for person like me to write efficient game loops. Also, it seems if processing time for one step in the loop is greater than the delay, your game speed becomes heavily dependent on CPU speed. Well, that was all I learned from writing a very simple game with Ruby. I hope after going thorough this article newcomers will atleast know what to search for on search engies before writing their own game. And if you do write one, do share with me !

[All trademarks and registered trademarks appearing on ashishware.com are the property of their respecive owners.]