The Rubygame Book Part 2

From KibaBase

Jump to: navigation, search

Welcome to Part 2. In this part, we will show you how to use the EventQueue and create a loop for Setup class so that the game can run continuously rather than just pop up and disappear. However, the setup class won't be used for playing the game, rather it will just be used as the game menu.

Contents

Before the Loop

Before we create the loop. We must first initialize the Rubygame::QueueEvent module. The Rubygame::QueueEvent class is used for detecting keyboard, mouse and other input device events such as a certain key pressed or if certain combination are pressed, allowing you to create code in response. Since every game requires input devices, this is a necessary module that you will use in almost every Rubygame application. Of course, since Rubygame is built on SDL, it naturally manages SDL_event.

Add @queue = Rubygame::EventQueue.new() to the initialize method.

Modify lib/setup.rb:

class Setup
  def initialize
    @screen = Screen.new([800,600],0,[Rubygame::HWSURFACE,Rubygame::DOUBLEBUF])
    @queue = Rubygame::EventQueue.new()
  end
end

Adding the Setup Loop

Now, we can add a loop. To do this, we'll add a method called run.

Modify lib/setup.rb:

class Setup
  def initialize
    @screen = Screen.new([800,600],0,[Rubygame::HWSURFACE,Rubygame::DOUBLEBUF])
    @queue = Rubygame::EventQueue.new()
  end
  def run
    loop do
    end
  end
end

Importance of the Loop

In game programming, the loop is where practically everything take places. Drawing, screen update, the game logic code, input event detection are somehow executed there. This is where you integrate everything.

Adding the EventQueue

Time for us to be using the Rubygame::EventQueue module. This is the last part of coding you have to do before you can execute the run method.

We're going with a rather lengthy block so bear with me.

Modify lib/setup.rb:

class Setup
  def initialize
    @screen = Screen.new([800,600],0,[Rubygame::HWSURFACE,Rubygame::DOUBLEBUF])
    @queue = Rubygame::EventQueue.new()
  end
  def run
    loop do
      @queue.each do |ev|
        case ev
        when Rubygame::QuitEvent
          Rubygame.quit()
          exit
        when Rubygame::KeyDownEvent
          case ev.key
          when Rubygame::K_ESCAPE
            Rubygame.quit()
            exit
          end
        end
      end
    end
  end
end

What we're doing is we're autofetching SDL_event simply by using each. Then we check if the event matched any of these event. When it matches Rubygame::QuitEvent, for example, it has detected that we have pressed the quit button in the window. Thus, it executed the conditional statement.

In this case, we're quiting the application. The Rubygame.quit() we're using, is a method that will clean up whatever mess the rubygame application has lying around. Make sure you use it, because it helps prevention confusions among your users because of problems like the desktop resolution not reverting to its previous, default setting. Then, we simply exit the program via exit.

When we're checking if the Rubygame::KeyDownEvent event has been triggered, we're not just looking to see if any of the key is pressed, but we're also looking to see if it pressed a certain button. In our case, we're checking if the user press the escape button on the user's keyboard.

One Last Part

Don't forget that we didn't execute the run method yet!

Now add setup.run() to rorgeball.rb at the end of the file.

Modify rbpong.rb:

setup.run()

Running It

Now, run the rbpong program. Notice that it doesn't exit immediately. However, we got one final task for you, please make sure that you can exit the application by the follow methods:

  1. Clicking on the close button on the window.
  2. Pressing the escape button.

If you successfully accomplish all these tasks without encountering an error, than you have completed part 2 of The Rubygame Book tutorial. You can now continue to Part 3.

Personal tools
TOOLBOX
LANGUAGES
Advertisement