Episode eleven of Making Games With Ruby: Options, has been written! This episode is short and sweet, and gives the players power over how our game plays. This episode also marks the end of the core development on our Pong game, everything after this will just be icing on the cake. You can read it here: http://devel.manwithcode.com/making-games-with-ruby.html#9
I hope enjoy this one! :)
- Tyler
(After posting this, I realized that the last episode came out just about a month ago, I'm really sorry for how long this took. In all honesty, I had the code for this written about 3 days after finishing Episode 10, but procrastination, school, and random stuff kept me away from writing the episode. Those reasons are no excuses, and I hope you'll accept my apologies!)
Episode ten of the Making Games With Ruby series: Sound, has been written! As you could probably tell by the title, we add sound to our game in this episode, more specifically one sound effect and some background music. You can read it here: http://devel.manwithcode.com/making-games-with-ruby.html#8
I'd like to say sorry for how long this episode took to come out. It's a short one, but I still had difficulty finding the time and motivation to produce it. For the most part this is because I'm back at school, so expect future delays, but I'll still try my best to get them out as fast as I can.
- Tyler
Episode nine of the Making Games With Ruby series, Game States (Title, About, and Pause), is finally written! This episode has us implementing game states, which lays the foundation for our Pause, Title, and About screens, which we also implement in this episode. You can read it here: http://devel.manwithcode.com/making-games-with-ruby.html#7
This one is probably the longest episode in the entire series (at least so far), which is why it's taken me a bit longer to finish. (I'm still not 100% sure I've caught every mistake, so if you see any code errors or script typos, don't hesitate to email me, or leave a comment here).
Enjoy! :)
- Tyler
Episode eight of Making Games With Ruby, Winning and Playing Again, is written! This one's a pretty short and easy episode, but it's still a good one! You can read it here: http://devel.manwithcode.com/making-games-with-ruby.html#6
Have fun! :)
- Tyler
Episode seven of Making Games With Ruby, Keeping Score, has been written. You can get to it from this link: http://devel.manwithcode.com/making-games-with-ruby.html#5
This episode has text drawing and players scoring on each other, I hope you like it :)
- Tyler
Episode six of Making Games With Ruby, The Ball, is written, online, and available at this link: http://devel.manwithcode.com/making-games-with-ruby.html#4
This episode has the ball boucing off the sides of the screen and the paddles, so it's pretty cool, I hope you like it! :)
- Tyler
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. http://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:
[source lang="ruby"]
require 'rubygems'
require 'rubygame'
[/source]
Then we're going to create the main Game class, and stub out all the methods in it:
[source lang="ruby"]
class Game
def initialize
end
def run!
end
def update
end
def draw
end
end
[/source]
And after that we're going to add the code to run the game:
[source lang="ruby"]
g = Game.new
g.run!
[/source]
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:
[source lang="ruby"]
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
[/source]
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:
[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!
Transcript:
Hello Everybody, and Welcome to the first episode of Making Games With Ruby! I'm Tyler, and this video is brought to you by manwithcode.com
In this episode I'll be covering what you will be learning in this series, what we're using to develop our games, what I'm assuming about you, and why I'm teaching you.
What you'll learn
If you couldn't tell by the title, you're going to learn how to make games using the Ruby programming language. More specifically I'm going to teach you how to make Pong. Though Pong may sound a little simplistic, and I'm not claiming it isn't, learning how to make Pong will teach you almost everything you'll need to know to make any game, with out getting bogged down in game specific details.
What we're using
Of course we're using the Ruby programming language. To create games, we'll have the help of the Rubygame library, which is many things including a wrapper around SDL, and a nice framework for developing games.
In the creation of this series I will be using Ruby 1.8.7-p174, and Rubygame 2.6.2. Theoretically The code in this series should run on Ruby 1.8.whatever and Rubygame 2.whatever, no promises though, since languages and libraries change.
And if you want to know about my environment, I'll be running Ubuntu 9.10 as my Operating System, and using Gedit as my text editor. You can write and run the code on any platform you wish, but this is what I prefer.
What I'm assuming about you
So I don't have to explain every line of code to you, I'm going to be assuming that you already know the Ruby programming language. If you don't there are many books available, as well as my own video series Programming With Ruby at http://manwithcode.com/ruby-programming-tutorials/
Just reading one book, or watching my video series probably isn't enough. I'll be easier if you have used Ruby for a while, and are comfortable with it. This isn't
a requirement, but it would help make things easier on yourself.
Why I'm teaching this
There are many reasons why, but I'll talk about the most important few:
1) I love making games.
2) I love teaching.
3) Teaching teaches me something. - When you actually sit down and think about what you do, things become more concrete, and you know why you do what you do, or even see bad habits that you need to correct
4) I'm trying to make money - Yep, I'm not going to try and hide this. Some of the videos in this series will be put up for sale, you'll be able to see them as I put out more videos.
Thank you very much for watching! I'll see you in the next video.