Making Games with Ruby Ep. 3 – Basics

Making Games with Ruby Ep. 2 – Setup

Covered In This Episode:

  • Creating a Window
  • Basic Event Handling

Transcript:

Hello Everybody and welcome to making games with Ruby episode 3, Basics. I’m Tyler and these videos are brought to you by manwithcode.com.

Today we’re just going to be talking about the very basics of game creation, just getting a window on the screen. The code for this episode is on manwithcode.com, if you’re not there already. https://manwithcode.com/322/making-games-with-ruby-ep-3-basics

Lets get started!

We’re going to start by requiring in the libraries we need, which for now is only rubygems and rubygame:

require 'rubygems'
require 'rubygame'

Then we’re going to create the main Game class, and stub out all the methods in it:

class Game
	def initialize
	end

	def run!
	end

	def update
	end

	def draw
	end
end

And after that we’re going to add the code to run the game:

g = Game.new
g.run!

So far this is just some very basic structure, nothing really happens yet. I’m going to run the code just to double check we have no syntax errors…

The first thing we’re going to do is initialize everything we’ll need:

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

Some of this is self explaitory, but let’s break it down. The first line of 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:

def run!
	loop do
		update
		draw
		@clock.tick
	end
end

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:

def update
	@queue.each do |ev|
		case ev
			when Rubygame::QuitEvent
				Rubygame.quit
				exit
		end
	end
end

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:

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!

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 tyler@manwithcode.com.

Thank you very much for watching! Goodbye!

Making Games with Ruby Ep. 2 – Setup

15 thoughts on “Making Games with Ruby Ep. 3 – Basics”

  1. Æ!!

    Congrats Tyler! 🙂

    Rubygame is really interesting! I’m studying Gosu alone and learning rubygame using your screencasts!

    Keep the good work! 🙂

  2. hi tyler,

    so many thanks for your guide! I’m trying to make a port of ClanBomber (don’t know if you played on it, it’s a Bomberman-like game; it was released with RedHat, if memory serves me right), your guide is what I was searching for in order to begin!

    I want to report an error, too: in def initialize,
    @screen.title = "Pong"
    should be
    @screen.title = "Pong"
    , I guess… wrong html entities? 🙂

    Thanks again! Bye!

  3. Hey Tyler. I’m fairly new to Ruby, but obviously I’m hoping to improve. I really like your videos. They’re informative and a great resource for beginners like me.

    There were a few times going through both the game-making videos and the regular Ruby tutorials where I got pretty confused… Like, as someone who has no idea what RubyGems was until today, you said I could go to the website and search for something I wanted, but I had no idea what I’d even be searching for. I thought, are these programs? Blocks of code to copy into a project? I didn’t even know where to begin, and I’m still pretty confused. I ended up just experimenting a bit and I did manage to download RubyGems and Rubygame, but those other websites can be pretty dang confusing and unhelpful to newbies. To me, it feels like there’s a secret Programmers Club and I’m not a part of it, so sucks to be me. These videos are a great way for me to slowly understand, though. It just takes persistence, I guess.

    I hope you make more of these videos, since I’m not sure where else to turn to learn any of this. I’d be willing to pay a bit for them, though if the price is too high, I’ll have to turn them down. I’m a poor college student… can’t really afford to buy anything except ramen noodles, you know?

    Anyway, thank you for your time and effort.

    1. I’m glad you liked the videos! 🙂

      don’t feel bad about not understanding things, you’ll get it eventually! And I probably deserve a lot of the blame for your confusion, I’ve lost touch with my “inner newbie”, I’m sure I could’ve talked about things in a much more beginner friendly way. I’d like to try to fix this, not sure exactly how yet, but we’ll see.

  4. Hi…
    I found these errors while running the game in console.
    Pls help me to rectify ..

    Warning: Could not load SDL_gfx! Continuing anyway, but some Surface methods will be missing.
    Error message was: “Could not load SDL_gfx.”
    Warning: Could not load SDL_mixer! Continuing anyway, but audio features will be missing.
    Error message was: “Could not load SDL_mixer.”
    Warning: Could not load SDL_ttf! Continuing anyway, but the TTF class will be missing.
    Error message was: “Could not load SDL_ttf.”

      1. Hi, Tyler

        this is the nice tutorial…
        but i get the same error in this tutorial…

        error display like this “Could not load SDL”..

        can u some help me.
        thnx if u help..
        Thank You.

  5. Great work – I am trying to show my 9 yr old that programming can be cool (rather than just playing flash games)…. She has been helping me follow the screencasts.

    Keep up the work though – I don’t want to end up with a black screen 😉

    Seriously – I am trying to learn/teach programming – this sort of thing is a bit more cachy than dealing with active record….

  6. Hey Tyler,

    Good job ! I learnt alot from your videos. Are you making anymore ? Perhaps you should write an e-book or something I’d buy it.

    Thanks for your hard work – if you put a donate button up I’ll buy you a case of beer

  7. hello the fuckng window doesn’t apear & this apear wht i do??

    14: from ./pingpong.rb:2:in `’
    13: from C:/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:39:in `require’
    12: from C:/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:135:in `rescue in require’
    11: from C:/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:135:in `require’
    10: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/rubygame-2.6.4/lib/rubygame.rb:43:in `’
    9: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/rubygame-2.6.4/lib/rubygame.rb:43:in `each’
    8: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/rubygame-2.6.4/lib/rubygame.rb:44:in `block in ‘

    7: from C:/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require’
    6: from C:/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require’
    5: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/rubygame-2.6.4/lib/rubygame/main.rb:22:in `’
    4: from C:/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require’
    3: from C:/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require’
    2: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/ruby-sdl-ffi-0.4/lib/ruby-sdl-ffi/sdl.rb:34:in `’
    1: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/ruby-sdl-ffi-0.4/lib/ruby-sdl-ffi/sdl.rb:51:in `’
    C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/nice-ffi-0.4/lib/nice-ffi/library.rb:98:in `load_library’: Could not load SDL. (LoadError)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.