This tutorial series is a crash course in writing video games using the Ruby programming languages and Rubygame, a game development library for Ruby. It assumes that you have basic knowledge of programming along with knowledge of Ruby's syntax. If you did not have Rubygame and/or the Ruby intrepreter not installed, it is suggested that you do so before continuing this tutorial. This tutorial will assume you're using 2.0.0 version of Rubygame. In this tutorial, we will be creating a dodgeball game using just Rubygame and Ruby as we write this tutorial out. No other libraries will be used. The final game will be licensed under the GPL. This game will be called Rubies Dodgeball! In Part 1 of this tutorial, we'll be creating a screen and than setting up a basic game loop. We'll start by assuming that everything will be in one directory named rubies_dodgeball. The first thing you will do is of course require Rubygame and initializing it. Than after that, you created a screen, the display window for the game. Create a file in the rubies_dodgeball directory: dodgeball.rb require"rubygame" require"lib/loop.rb" include Rubygame Setting up Rubies Dodgeball to use rubygame. Create a file in the rubies_dodgeball/lib directory: loop.rb class GameLoop def initialize @screen = Screen.new([800,600],0,[Rubygame::HWSURFACE, Rubygame::DOUBLEBUF]) end endhis is very boring basic stuff, but we will get into more exciting stuffs later! We set up the dispay screen with the size of 800 by 600 resolution in the first agurement, next one with no depth, and the third one with flags Rubygame::HWSURFACE(It makes a video surface in video memory) and the Rubygame::DOUBLEBUF will enable hardware double buffering. There are other flags like the one that enable fullscreen mode. Of course, we need to initialize the GameLoop in order to take effects. This should be placed at the end of file dodgeball.rb game = GameLoop.new Once you run the program, it should display a brief display window. Since it have no loop, it doesn't stay long. Next, we add an EventQueue with a loop into the game. We modify loop.rb's GameLoop class in the following fashion: class GameLoop def initialize @screen = Screen.new([800,600],0,[Rubygame::HWSURFACE, Rubygame::DOUBLEBUF]) @q = Rubygame::EventQueue.new() end def run loop do @q.each do |ev| case ev when Rubygame::QuitEvent Rubygame.quit return end end end end end Rubygame::EventQueue is used to detect keyboard presses, mouseclicks, movements of mouse, and other input devices.There are hundreds of constants that correspond to a particular keyboard keys, mouse buttons, and others. For example Rubygame:K_ESCAPE is for the escape button. Rubygame.quit() is a method that should be used when Rubygame applications are about to be terminated. Notes that it isn't used for quiting the application, rather it clean up the messes it might have. For example, if you exit while in Fullscreen mode, users' desktop resolution will become that application's dispaly screen size, which is very annoying. Rubygame.quit() help prevent such annoyance and confusions from happening. Don't forget to add game.run() at the end of the file dodgeball.rb! game.run() When you run it, the application should go on forever, sucking all the available CPU resources that it can get, until you click that X button in the window. The end result of part 1 should look like this(linkies): dodgeball.rb lib/loop.rb Well, that is it for today, folks! This isn't so hard at all! What's coming next in part 2!: How to reduce the CPU resource usage to a saner level... Addition of a sprite system. Until next time! Kiba, A fang with no bite. |
