Ruby
Writing Games with Ruby
Summarized using AI

Writing Games with Ruby

by Mike Moore

The video titled 'Writing Games with Ruby' features Mike Moore, who shares his enthusiasm for game development using the Ruby programming language and the Gosu library. The session is designed for beginners in game programming, and attendees are encouraged to follow along to create a working game.

Key points covered in the video include:

  • Introduction to Game Development: Mike introduces the concept of game development, highlighting the fun and creativity involved. He encourages interaction from the audience to gauge their interest and experience levels.
  • Gosu Library: This minimalistic game library is presented as a user-friendly tool for Ruby developers. Mike emphasizes its ease of use and the hands-on approach it fosters compared to traditional frameworks.
  • Demonstration of Examples: Mike showcases various games he has previously created using Gosu, such as 'Captain Ruby,' 'Little Brat,' and 'Escape to Ruby Comp.' He mentions the possibilities of creating games within a short timeframe, like during the Ludum Dare competition.
  • Game Loop and Basic Mechanics: He explains the importance of the game loop, which handles updating game logic and rendering visuals. The distinction between entities, such as the window entity for graphics and inputs, is also discussed.
  • Writing a Simple Game: Mike walks through coding a basic game, 'Hello Ruby on Ales,' discussing key elements like window creation, rendering text, handling user input, and maintaining the game loop.
  • Animation and Sound: Techniques for adding movement and sound effects to enhance the gaming experience are demonstrated, including the use of simple mathematical functions to create animations.
  • Creating Sprites and Player Objects: The session explains how to build a player class to handle player interactions and movements, emphasizing the management of various entities within the game.
  • Encouragement for New Developers: Mike urges viewers to try game development, suggesting that anyone familiar with web applications can easily get started with simple game programming. He proposes a fun challenge of creating a Ruby character that navigates toward beer, tying the idea back to the conference's theme.

In conclusion, Mike Moore aims to inspire his audience to explore game development with Ruby and the Gosu library, showcasing its accessibility and potential for creativity in making engaging games. The session ends with an invitation for attendees to connect with him during the conference for further discussions and social gaming experiences.

00:00:15.920 All right, well it's good to be back. My name is Mike Moore. If you had Wi-Fi, you could gem install Gosu and follow along, but you can do that afterwards as well.
00:00:21.520 First off, I've had the flu for about a week. I thought it would be over by now, and it's not. I wasn't feeling very good at all yesterday, and I took a late-night flight. I stopped at the store and got some NyQuil gel tabs.
00:00:34.559 This morning, as I broke them open to mask the effects of the cold, I noticed they were Alka-Seltzer effervescent tablets that you dissolve in water. I put one in a glass, waited, and thought they were dissolved, so I quickly drank it. However, they weren't completely broken up, and it went down my throat, which could be a problem. So, not only am I nervous, but I'm feeling a bit gassy from that.
00:01:16.080 Okay, I have a secret, and you guys are the first to hear it: writing games is really fun! So show of hands, how many here like to play games on their computer, phones, or any other device? That's pretty much everybody. Now, show of hands who has ever tried to write a game before? Awesome, so about half of you! That's good.
00:01:29.920 We’ll talk about what it takes to write games. If you're not familiar with writing games, this session is for you. And if you're already an expert, you might find it a bit boring. Writing games is fun, and it'll blow your mind! I think writing games in Ruby is particularly enjoyable because it allows me to focus on the goals rather than the technicalities of how to achieve them. So, I love Ruby, and that's why writing games is fun for me.
00:02:00.640 We will use the Gosu library, which is a minimalistic game library. It's important to note that this is not a framework with templates to build your game; it's much more hands-off. It's designed to print graphics to the screen and play sounds. It's a fantastic little gem with excellent Ruby support. Even though it's written in C++, it's meant to be used within Ruby, making it particularly beneficial for Ruby developers. I encourage you to check it out at libgosu.org.
00:02:37.920 Before we get into how to write games, let's talk about what is actually possible when writing games. If you go to your code directory and the Gosu project, you'll find an examples directory that includes several files you can run to understand how it works. I'm going to switch to my Gosu gem and demonstrate some examples.
00:03:10.640 This is one of the example programs called Captain Ruby. In this little platformer, you use your arrow keys to move left and right, and pressing the up key makes Captain Ruby jump. If you don't press up at the right moment, you'll fall. Let’s see if I can get to the top—ah, I can never reach it. But, this is all possible with Gosu, and it’s out of the box!
00:03:52.000 I have kids, and they used to always want to sit on my lap and type on the keyboard with me, which was really annoying. So, I got them a little nine-inch netbook running Linux. The first thing I created with Gosu for them was a game called 'Little Brat.' If you gem install 'Little Brat,' you'll be able to run it. It’s fun because even when my kids were two and three, they thought it was fantastic to type with me!
00:04:58.320 Control-Q ends this game. That was my first project. I loved it! Another game I wrote back in 2010 was called 'Escape to Ruby Comp,' shared during my earlier presentation at RubyConf. In this game, you're a code ninja trying to avoid evil Python snakes. You win by reaching the end, and every time you level up, you encounter more snakes. If you fail, well, you fail!
00:05:44.880 Recently, at LA RubyConf, Aaron was tweeting a lot about Flappy Bird, and I thought to create a sort of Flappy Bird clone called 'Ruby Hop.' In it, you hop through hoops as a full Ruby character. This shows what's possible with Gosu. There are many more opportunities if you check out the libgosu site and forums where many people share their game creations.
00:06:32.080 There's a competition called Ludum Dare, which is a 48-hour solo hackathon to create an entire game, as complete as you can, within 48 hours. Gosu is an excellent tool for this kind of project. So now that we’ve seen what’s possible with games like Captain Ruby, Little Brat, Escape to Ruby Comp, and Ruby Hop, let’s discuss how to write games.
00:07:20.000 All games share one common aspect: they run within a game loop. This loop runs continually, repeating over and over again. Every time it cycles, it calls an update method where all game logic occurs. After updating the game state, it calls the draw method to represent the new state visually.
00:07:59.360 In Gosu, which is minimalistic, there’s not a lot to it—only a handful of entities. The most important is the window entity, which is responsible for handling all your graphical output and input. It's essential to reference this window object in your code. You can load images either from your file system or by creating new images from other images or drawing text onto images. Additionally, you can play sound effects, which can be of two types: songs and samples. A song is typically a looping sound, while a sample is short-lived and is used frequently, like the bouncing sound of the Ruby character hopping.
00:09:53.920 Now, let’s take a look at the Gosu window on the libgosu site, particularly the Ruby reference. If you open the class list, you’ll see a limited number of classes, and the one we care about most is the window class. We will also explore the image, song, and sample classes for this demonstration.
00:11:19.760 When initializing a new window, we pass four values, with the fourth being a default: the width and height of the window, whether or not it runs in full-screen, and the update interval, typically set to 1/60th of a second. We can change that rate to any desired frame rate for our game. Let’s create something now. I’m going to open up my handy editor and create a new file called hello.rb. First, I’ll require Gosu and define a new class called Hello that inherits from Gosu::Window.
00:12:40.960 Inside my initialize method, I'm going to set the default values for the width, height, full screen, and update interval. We’ll set the width to 800, height to 600, and full screen to false. Next, I can call super without arguments to pass those values to the Gosu::Window class. Now, let’s set the window caption to 'Hello Ruby on Ales.' Now that we've written this code, let’s run it. Oops, I forgot to create an instance and call show on it! There we go, we see 'Hello Ruby on Ales'—yay!
00:14:06.960 However, I can't close it with escape or return, so I need to create a button down event. This method will check for the escape key, and if pressed, it'll close the window. Since the game loop runs 60 times a second, on each loop it'll call update and draw methods. When you click a button, it'll call button down or button up events. If we press the escape key, the game will shut down gracefully.
00:15:54.000 Next, I want the window to display 'Hello Ruby on Ales' instead of just the caption. To do this, I need to create an image object that says 'Hello Ruby on Ales.' I’ll use the Gosu method to get a new image from text, passing in the window object, the string to display, the default font, and the font size of 100. Then in the draw method, I’ll draw the image at coordinates zero, zero to show it at the top-left corner of the window.
00:16:45.760 We’ve made 'Hello Ruby on Ales' visible! Now, we discussed coordinates previously; in Gosu, the upper-left corner is (0, 0) and the bottom-right is (width, height) of the window. Since our window is 800 by 600, we can center the text by calculating based on the image’s width and height. Instead of drawing it at (400, 300), we should adjust those values by half the width and height of the image to properly center the text.
00:18:47.000 Mathematics is involved here—we’ll use simple arithmetic to find the center of the screen and make adjustments accordingly. Now that we have our centered text, we can experiment further. Instead of hardcoding to 400 and 300, I could define x and y as window.width and window.height divided by 2 for better readability. That should still keep the text centered. Perfect! Now the text is centered, and the code is cleaner.
00:20:33.280 Next, let's add some movement to our image. I’ll modify the y coordinate by adding a sine wave based on the current time to create a bouncing effect. We want to keep things lively! Let’s try adding a cosine wave for additional vertical movement, giving the illusion of hop animation. This is how we create the effect of animation in games—by drawing new frames quickly enough to trick the eye into seeing motion.
00:22:42.080 To enhance the experience further, we’ll incorporate sound! Let's create a sound object from the Gosu class and play it as background music. I can use an MP3 file like bubble.mp3 and control its playback through Gosu's music functionality. I’ll also add a sound effect for interaction using a sample like beep.wav, which will be played every time I press the spacebar. This makes the experience interactive and fun!
00:24:50.240 We’ve covered how to display images, update every game loop, and bring the user experience alive with sound. It’s about creating an engaging interaction where players can see things happening and hear their effects. If I change the background music to something else, like 'Kings of Rock,' the gaming atmosphere transforms. You can also explore creating sprites to represent multiple entities within the game, allowing for varied interactions and experiences!
00:27:50.000 As we continue, I’ll craft a new file called sprite.rb and start defining classes for a game. We’ll continue building by incorporating gameplay mechanics, where players can move left and right and make use of sprites for characters. When developing a game, it's key to understand how to create a player object that can react and interact within the game environment, ensuring an engaging experience.
00:29:17.760 Now, I'll implement the new game structure. We’ll create a player class to manage instances with direction and images for movement and actions. The sprite will move according to player input, whether it's left or right. As sprites handle specific functionalities, we can easily manage multiple sprites (like players and enemies) within the game while keeping the system organized and maintainable.
00:31:00.000 Do you know what parallax scrolling is? It creates an illusion of depth by moving different images at different speeds; it's mind-blowing how simple mechanics can lead to rich gameplay visuals! At this point, I encourage anyone who hasn't tried game development to take the plunge! If you can write web applications, you can definitely create a simple game. Let’s think about what a fun Ruby on Ales game might look like—how about getting the Ruby character to the beer? That sounds like a fun challenge!
00:31:52.240 Thank you all for your attention. My name is Mike Moore, and I hope you are inspired to start writing games using Ruby! If you’d like to join me for a quake three LAN party during the conference, please come find me!
Explore all talks recorded at Ruby on Ales 2014
+3