Making Games with Ruby Ep. 3 - Basics
Series: Making Games With Ruby
Covered In This Episode:
Keep reading...
- Creating a Window
- Basic Event Handling
initialize
creates the window for our game, Rubygame calls this a Screen. The first argument [640, 480]
is the size of the screen, the second is the screen depth (you don't need to worry about this), the last one is a list of flags we pass to rubygame. HWSURFACE means we want it to be accelerated on the graphics card if available, and DOUBLEBUF means we want the screen to be double buffered. Double buffering is a way of drawing to the screen. I won't go into too much detail right now to explain double buffering, but I will in a later episode.
The second line has an obvious purpose, it sets the title at the top of the screen to Pong
The fifth line is setup so we can handle events, I'll talk more about this later.
The last two lines are the setup so we can limit the framerate to 60 frames per second. The frame rate is how many times the screen is drawn every second (we'll be doing our drawing in the Game#draw
method). If we let this go unchecked, our game will run as fast as possible which isn't desireable since top speed will be different on different computers, and depending on what the game is doing at a particular moment in time. I think 60 is a good number, but we can easily change it later if we want.
Next we're going to setup the main game loop:
[source lang="ruby"]
def run!
loop do
update
draw
@clock.tick
end
end
[/source]
This loops indefinately until we decide in a different part of the code to end the game. @clock.tick
is what allows us to limit our framerate. You can now run the game, it should just be a black screen. You can see the title at the top, like we set in the initialize function.
But if you try to close the window... Nothing happens!!! To close it try pressing CTRL+C in the command prompt window, or you may have to go to the task manager and kill it from there. To fix this problem, we need to talk about the event queue.
We're going to start by defining the update
method:
[source lang="ruby"]
def update
@queue.each do |ev|
case ev
when Rubygame::QuitEvent
Rubygame.quit
exit
end
end
end
[/source]
Whenever the user gives us some input, presses a key, moves the mouse, etc. the Rubygame adds an event for this input onto the queue. We handle these events in our update method.
@queue.each
loops over each event, and we have a case statement that handles the event depending on the type. For closing the window, the event type is Rubygame::QuitEvent
, which is generated whenever the user presses the close window button or ALT+F4.
Now if you run the game, the window should appear and you should be able to close it now! Yay!
This is the final source listing for this episode:
[source lang="ruby"]
require 'rubygems'
require 'rubygame'
class Game
def initialize
@screen = Rubygame::Screen.new [640, 480], 0, [Rubygame::HWSURFACE, Rubygame::DOUBLEBUF]
@screen.title = "Pong"
@queue = Rubygame::EventQueue.new
@clock = Rubygame::Clock.new
@clock.target_framerate = 60
end
def run!
loop do
update
draw
@clock.tick
end
end
def update
@queue.each do |ev|
case ev
when Rubygame::QuitEvent
Rubygame.quit
exit
end
end
end
def draw
end
end
g = Game.new
g.run!
[/source]
This brings us to the end of the episode.
If you have any questions, comments, or suggestions, leave a comment on this page or email me at [email protected].
Thank you very much for watching! Goodbye!