 I'm Rishi Jain and I'm here to talk about game development, the Ruby way and I'm all the way from India. I know it's a long way from home. It has taken me one bus ride, two change of flights, two change of trains and a tram to finally reach Ghent and it has been awesome. And I work for this company called Josh Software and Josh is a Hindi word which means enthusiasm. A lot of people read it as Josh Software but it's Josh Software and we believe programming is an art. And we are not just about work since a lot of our work depends on open source community. We believe in contributing back to the community in whatever ways possible by doing open source contributions, writing gems, JS libraries or organizing Ruby Confindia. We have been doing that for past five years now. And personally I love conferences not only because I get to meet new people but it takes me to different places and I get to know about different cultures and it's very important. So when I got that wonderful email from the Arcamp organizers that your talk has been selected, I asked myself, what do I know of Belgium? What are some of the things that I could relate with? And this is the first thing that came to my mind. Belgium football players, right? I love football. I've been playing football. I've been watching football for so long. And this is one of the things that I could relate to. So I ask, any football fans in the house right now? Already some of you. Cool. So one of the things that is clear is not everybody in Belgium loves football because that is one of the things that I've heard about Belgium that people love football. But not really. Then the second thing that almost everyone in the world could relate to with Belgium is the chocolates, right? So when I was coming over here, I told my friends that I'll be going to Belgium and they were like this all the time. Chocolates, chocolates, chocolates. I was like, okay, I'll get some chocolates for you guys. So moving on to games. How many of you are like passionate gamers? Like every night you can play games all night long. Yeah, some hands there. Cool. How many of you like to, you know, don't mind playing games when you're traveling or when you're working in a meeting or something. Yeah, cool, cool. Some hands there. Not the most interesting one. How many of you have built a game like from scratch? Alrighty, some of you have. That's good. That's good for me. Not all of you have built a game. So when it comes to gaming, I consider myself a full stack senior game programmer, right? I'll break this down for you. Full stack because every job opening that I see these days are all about full stack developers, right? Everyone wants a developer who could come in the first day and start solving the greatest problems computer science have ever faced, right? Naming. So I'm a full stack developer. I also consider myself senior because most of you have not written a game and I've written like two, three small games. So I'm a senior. So I'm a full stack senior game programmer. So it's only fair that we start off with the history of gaming and I think it started this way, right? So I have a few games here which I thought really impacted the history of gaming and I believe this was the first game. It's called Pinball. I was very fortunate to have a computer at a very young age and I used to play this game on the Windows machine. And there was not a lot to do in this game except that every time the ball hits a different object, it produces a new sound and I love that. So I love this game. I've played a lot of it. Any of you have played Pinball, like on a machine, Windows or actual board or something? Some of you have. Okay. Cool. The next game, which I think is really important in the history of gaming, it's the Pong game. Four engineers build this game and as you can see, it's an arcade box with a couple of joysticks on it. So it was a multiplayer game and it was great vision back then to realize that people will love playing games with each other. But it didn't hit it off for them commercially because it was really difficult to find another player to play a game with. So gaming was considered dull kind of. So it didn't work out commercially for them, but it was great vision nevertheless. So to solve this problem, Steve Wozniak and his friends wrote this game. It's called the breakout game. It's a single-player game and all you have to do is break the bricks all day long. And it was a great game because you don't need another player to play with. Now these are the few games that I think are really important in the history of gaming. But what about my history of games? I have been playing games all my life. If you ask me, I can divide my lifespan so far into three different games that I've played at every age. So this is what we're going to do. I'm going to play the soundtrack of each game and you guys have to identify what game it is. So this is the first soundtrack coming up. There's more. Alright, somebody just stop this. Yes, you're right my friend, it's Contra. I love this game. I've played it like all my childhood I've been playing Contra. It's a great game. The original plan was to give away t-shirts when somebody answers. But I somehow managed to leave my bag of t-shirts at the first transit airport. So I'm really sorry about that. Then this is the second game. It's very easy. I'm sure you'll get it. I know and you know the answer. Most of you have played this game. Personally, I didn't like this game at all. But the problem was wherever you go, you go to birthday parties, you go to school, you go to playground. Kids were talking about this game and I was kind of left out of conversations there. So I thought, you know what, it's summer holidays. I need to beat the crap out of this Mario guy and play this game. So I played this game in one of the summer holidays and eventually, I mean, I'm not bragging or anything, but I kind of crossed all the levels. I mean, not all, but yeah, I was very close to the end. So I can't lie because my sister will watch this and she'll be like, you lied on stage. So I would have said all. But then that's okay. Moving on to the last game. I absolutely love this game. And it's really hard to identify. Only the true fans would get this. Anyone with any guesses? All right. So it's FIFA. I've been playing FIFA since FIFA 7 and I still play it. I've even hooked up a PS3 in my office and whenever I need a break or somebody needs a break, they go and play FIFA. All righty. Among all of this, I had this thought that I want to build a game. But why you would ask? I mean, playing games are fun. Why do you need to build a game? So for first reason, I think games are like ever expanding universe. And now consider this, that you're playing a game and you're the good guy with a gun and a bad guy comes up and you shoot the bad guy. So you would think that it's your decision to shoot the bad guy. But I would like to think that it's the game programmer's decision to make you shoot that bad guy. Right? So it's like a puppet show. And for once, I would like to be the guy who's controlling all the strings. So I wanted to build a game. Basically, you can say that I wanted to be God for a couple of hours. And the second reason is because building games are fun and you will see it. All righty. So let's start and code a game, right? In your favorite languages. C++, object to C. Right? No. Not happening. Not today. In Ruby. Right? But before we do that, let's see what kind of games we can write in Ruby. Right? So I'm here to talk about Gosu. So it's only fair we see the kind of games we can build using Gosu. I'm going to do this thing. Come here. All righty. Yes. You could see. So I've been playing this game for so long now. And I've kind of got bored with it. So do I get a new volunteer who would come on stage and play this game for us? Anyone? First row. Anyone? Cool. Cool. Cool. Come on up. Somebody. Great. Thank you. So I'll tell you the rules of the game. This is if some of you have played the eggs game on flash. This is one of that. So your egg is, I mean this particular dragon egg is at the top and you can move it while you're at the top. And the aim is to drop the egg into the basket. And to drop the egg, you have to press space bar. So, and once you've pressed space bar, for example, right here, you can't move the egg on left or right directions already. And I'll also give you this, that you go for like 10 eggs, you drop 10 eggs. And the best score that has been done is six. So beat that. All righty. Cool. And oh, we have sound for that. All right. You have taken a lot more than that. But thank you. Thank you so much. All righty. This was the first game that I wrote now moving on to the next game. No, not this one. This one. Right. So do I get any volunteers for this one? Anyone? Come on. Come on, people. Great. Come on up. Cool. Thank you for coming. So the purpose of this game is pretty simple. You have to type in the word that you see. And as you keep typing in the correct words, the level of difficulty of the words keep increasing. And also the way a word is coming up, the pattern might change. Like if you see, sorry for that. That was a symbolic performance. So one of the rules is you can't press the delete key. Once you've typed in, you've typed in. Right. And to submit a word, you have to press enter. Or to get the next word, you have to press enter. So I'll start off from scratch for you. There you go. Go. Oh, you press, you press. All righty. That's first word. Pressing the key is so big. Yes. I'm sorry, that was because I'm drunk. Why? Cool. Now the speed I've increased the words and your time is up. And it was a really bad performance. But thank you. Thank you. Moving on to the next game, which one? Let me show you this game. But before I do that, let me just turn it off. So this was about dragon eggs. And this is one of the process that I follow because I like drawing things. And so I like to, I like to draw these little things that this is what my game could look like. And it's a really bad drawing. I know the egg does not look like an egg. The baskets look weird, but it works for me. This is the second game. This game, which I'm about to show, I've not written this game. Bell and Albeza have written this game. But while I was looking around, what are the various options? I found this a pretty good game. And so I'm going to play this game for you guys. So the purpose is pretty simple. You have to shoot these aliens and you have like four lives. And you have to shoot them all. And it's kind of, they're getting to me. Oh man. Oh, I'm pretty good at it. I didn't knew that. Oh, okay. Cool. So this is one of the games. And last one is another game which I have not written. When you install Gosu, it comes up with Gosu. It's called Captain Ruby. And basically it's Dave, if you guys have played Dave. Anyone have played Dave over here? Some of you have. And I'm not really good at all. That's easy. That's close to the same before it starts wearing. So I'm going to do this again. And we are here. Great. So now that we have seen what kind of games we can build with Gosu, it's only fair that we start hearing something about Gosu, right? It's a 2D game development library for the Ruby and C++ programming languages. It's available for Mac OS, Windows, and Linux. And it works great with MRI Ruby, Mac Ruby, and Rubinius, but not so much with JRuby. So please don't try this at home. Then if you're working on a Mac, I would suggest that switch to System Ruby instead of RBM-backed Ruby because Gosu doesn't really work well with RBM. I have spent hours trying to make them work together. And that was not fun at all. Because the first reason I was looking at Gosu was because I wanted to have fun and write a game, not trying to make them to work together. So I happily switched to System Ruby. And this is the official website. You can find more information over here. And this is the official game board. So any game that you write, you can provide the link to the code ways of the game or a YouTube link where you can show off what the game is all about. So check this out. Now, I have always thought that game development is like you need to know big, you need to read big books and know the game theory and the maths and everything. But not really. You just need to know one concept. And that's the simple flow chart. You initialize all the things. You draw all the things. And you keep updating the state of all those things. And you draw again and you keep updating. Draw, update, draw, update. It's a simple cycle, right? That's all you need to know. So Gosu expects us to write these three methods. Initialize, draw, and update. So if I need a background image to my game window, I'm going to put that in initialize. For soundtracks, initialize. Even for starting off a game, you need to initialize. So all my code will come in initialize method. Then if I want to tell Gosu that go on and draw this particular image at this particular coordinate, you put that code in draw method. And whenever I want to change the state of the game or state of the objects or any interaction that I want to add, you add that in update method. And Gosu will call this update method 60 times in a second. So it's pretty fast. So to understand this a little bit more better, let's break down dragon eggs, right? What it had. It had a game window which you guys saw. It had images and sounds. I call them aesthetics. It had eggs, right? It had baskets. And lastly, it had collisions. Collision between an egg and a basket. Have the egg fallen into the basket or not. You need a way to find that out. So let's start off with game window. So you open a file, empty file in any of your favorite editor. I use Wim all the time. And you require Gosu. And you open up a new class called game and inherit from the Gosu window class because we want the object of that. And you add an initialized method and you call super over it. In that super, we pass in three parameters. First parameter tells you the width of the game window, which is 800 pixels. Second, no guessing needed, right of the game window. Third parameter, it's a Boolean value. And I have passed in false because it tells you that it asks me, do you want the users to be able to full screen your game window? And I don't want that. So I say false. And then I add a caption. And it's our game. So let's see this thing in action. This is going to be... You're doing this every time. That's okay. So you guys can probably read this, but I don't like it. So it's the same code. And at the end, to run a game, I initialize... I create a new object of the game class and I call show on it. And now to run a game, I'm going to do this thing so that you can see very clearly what's the game window. Oh, that's not good. Now this black window is our game window. And at the top you can see this is the caption that we wrote. My game. I thought we wrote our campaign. I'll close this thing. And I'm going to look it again. Oh, sorry. We wrote my game over here, but our can game on the slide. So let's change that because it's not my game. And yes, it works. So to close this, I do this. All right. So... Moving on, consider this blue part of the screen, your game window. So as you can see, the top left-most corner is the origin of an XY graph. It's 00. So as you move towards right, you're increasing in the X coordinate value. And that's obvious. There's nothing to tell you about that. But when you move down from that 00 origin, you're increasing in the Y axis value, Y coordinate value. And that's different because in our XY graph, as you move down, you're increasing in the negative side of the Y value. And as you move up, you're increasing in the positive side of the Y value. But here it's upside down. So that's one of the things that you should remember while writing your game. Now as you might have seen that to close a game, I have to take my mouse pointer to that red button and close it. And it's really annoying. So one of the things that I do is I add a close action to my game. Whenever I press escape, just close this game for me. It's easy. This is one of the practice that I have not picked up myself. I was watching this talk by Mike and he mentioned that he does it. And I realized, yeah, it's pretty stupid to take the mouse pointer every time. So I've started doing it myself. So this is our earlier code, which is kind of blurred. You don't need to see that. So we add an update method because now we want to update the state of the game. So as we have seen, we need to add that code in update method. So I'm going to say to go through if you see a button being pressed, I ask it by button down method. And if that button being escape key on the keyboard, just close the game window for me. Right? So let's see that in action. So it's the same code. We have just added an update method. And at the end call show on our new game object. And every time I type in wrong. So I don't have to actually take the mouse and click on it. I can just press escape. It's quick and it's easy. Right? So Gosu also provides you all these events. Just like escape key, you can look for KBA, KBB, KB123, whatever. For every key on the keyboard, there's an event in Gosu. Now moving on to images and sounds. Now a lot of people that I've talked to who write games in Gosu, what they do is they like to add the sounds to the game at the end. They would like to create objects which will be part of the gameplay. They would like to add the interaction between them. And when everything is done, they would add the background images. They would add the background soundtracks. But I do it opposite. I like to add them at the beginning because that's how it keeps me interested in writing that game. Also it helps me to see the whole picture building up. I need my player over here and this is how they interact and this is how it will look. So I add the images and sounds first. So now no guessing needed to add an image and a shout. We're going to open up our initialize method again. Call a super, add it caption and I'm going to tell Gosu that add and Gosu image for me and I pass in two parameters. Self, game window, object and the path of the file and I save it in an instance variable. Similarly, the same thing for sound. I tell Gosu, initialize a new sound object for me and I pass in the same two parameters. The file path and the game window object. And if you have noticed, I've just initialized these objects. I have nowhere to go and draw these objects for me and I play this object for me. So because that is something that we're supposed to do in the draw method. In the draw method I'll tell Gosu that please draw that background image for me at 00 position, which are the first and the second parameter and third parameter is the Z index value. Since it's a background image I say keep it at Z index 0. Similarly, I tell Gosu go and play this soundtrack for me and the boolean value which you see is telling Gosu that when the soundtrack gets over, restart it. Because you don't know how long the game will continue. So you don't want to have a really long sound file. You can keep just a small file and keep playing it again and again. So this is the same program as you can see. In the draw method I've set shell to draw these objects. I have initialized these objects over here. It's the same program that I've showed you. Let's just every single time. Let's just run this thing. And we have the sound for it. If you notice that since the origin of the graph is at the top leftmost corner it starts placing the image from that particular point. So it is your duty to make sure that you have an image which has the same width and height as the game window. Now since I didn't take care of that you can see a little bit of small block over here in black because the image ends. So it is something that you need to take care of. Moving on to edge. I needed one background image so I put that in directly in our initialized method. I needed one soundtrack in our initialized method. But I'll definitely need more than one edge, right? One will not surface. So I open up a new class called egg and in that class I add a few attribute accesses which will see why we need them. You can guess the x and y obviously. Then we add an initialized method where we are accepting three parameters. One, the window parameter that means I'll be accepting that please tell me what window you want to draw this object on. So and the second and third are self-explanatory x and y. So I do this, do this and finally add an image for our egg object, right? And it's just in the initialized method. Then in the draw method I tell this class to draw that particular image for me at x and y coordinates z index being one because I want that particular egg to be over the background image. And then in my original go-to game window class, this is the old code and I just add this particular code in our initialized method. So I'm initializing an object for the egg class that we just wrote. I'm passing in the window, game window object accessed by self and the x and y positions of the egg object I want to be. So let's see this thing in action. The same thing. We have just require the egg class. I add components and add an egg class in it. And there are so many other parameters which I will explain you later. So in our initialized method I just called egg.new and initialize them at 300, 200 and in my draw method I just say egg.draw and there you have it and it could even move which we have not seen the code for but yeah. Now to add the movement to an egg it's like you want to update the state of an egg, right of the current egg if I may say. So this is what you will do. You have already seen the button down method and when user presses escape we know how to find that particular key. So now I want users to be able to move the egg using the left and the right arrow key. So I am going to say if the key is left decrease its x coordinate value. Because when you move right of the origin you increase the value. So if you are at a particular point let's say at 300x value and if you want to move left you decrease it. You go towards 0. So I decrease the value by 5 and if I want to move right I increase the value by 5. Now similarly I want the eggs to be able to free fall because when the user presses space the egg should be able to move down without any breaks. So this is what I do. I add up a free fall method in my egg class and in that the attribute axis of which I just used I set that as true and similarly in my original game window class I open up the update method again and I just say that keep increasing the y value by 5 if that particular egg has been set true for free falling and it will keep increasing it. It will keep increasing the value of the y coordinate of egg because it keeps calling the update method 60 times in a second so you will not see any breaks in it you will see a smooth transition happening and let's see this in code. There you have your egg and you can move it and it falls. Now keep notice that when I press spacebar and I press the left arrow key in the right direction so this is something that you have to take care of so you have to pass in conditions that if the egg has been set for free falling do not call the left and the right arrow key movement or something like that now we know how to do a free fall but we have also written a method called free fall bang and I will say just set that particular egg true for free falling now what about multiple eggs right because you don't need just one egg in it you need multiple eggs on the screen but you also want to be able to use it to just see one egg at a time so there are couple of ways you can do it one every time the egg has completely fallen you initialize a new egg object and draw that object on the screen this is one way people do it the other way is by placing the eggs like if you have decided that you are going to have 20 eggs in the game so instead of initializing at runtime you can place all your eggs on the screen at such coordinates that user only sees one egg at a time so just see this code I say I initialize an empty array and let's say I have decided I want 20 eggs in the game so I add a new egg object every time and in the X coordinate I have passed in something like 400 into D plus 400 where D is the iterator count so for the first time the loop runs it will place an egg at 400 position and I will be able to see it but for the second time it will place an egg at 400 plus 400, 800th position and since my game window is 800 pixels long I don't see what's happening after that so I initialize all the eggs such a way they are difference of 400 so the second egg will be at 800, the third egg 1200, 1600 and so on and in the draw method I say something like this to draw each egg it's simple so when the egg have completely fallen for example I just press space and the egg have fallen into the basket and it's moving with the basket I just I just decrease the coordinate by 400 X coordinate by 400 of every egg which is subsequent to that egg so the next egg which was at 800 will now come down to 400 and after that which was at 1200 will now come down to 800 so I don't really have to manage the eggs initialization dynamically and I like doing this because it's easy now baskets same thing really right we don't have to talk about baskets separately except that you want these baskets to be able to move keep moving so now we have seen how egg can keep free falling so now we can guess how the basket can keep moving towards the left no biggie now at last the most important thing collision GOSU provides us with this method to identify if the distance between the two passed in parameters the first XY coordinate and the second XY coordinate are the distance between them 0 it works for some cases but not for every game now for example I wrote a separate collision method okay look at this image now I want at when the distance between these two objects should not be 0 and then I can say it's under collision but how do we come up with this method I've said baskets X coordinate should be less than X coordinate of egg which we can see in this image the baskets X coordinate which is right here less than the X coordinate of the egg which makes sense but the second line the difference between the X coordinate of the egg and the basket should be between 0 and 35 now how do I come up with such values so to understand that let me now I have printed the XY coordinate of the egg and the ring so now our egg is right here I'll move this thing so as I move towards left the X coordinate keeps decreasing as I move towards right the X coordinate keeps increasing and you are seeing so many log outputs because I have added this in the update method and Gosu is calling that 60 times so that's why I keep printing it now as I move down the Y coordinate is increasing now I know when the egg is here here it's into the basket but when it's like this it's not really into the basket so now look at the difference between the X coordinates it should be 0 so something like that and the end range of that particular coordinate should be somewhere here so I say that the X coordinate of egg is at 625 and the Y coordinate of ring is at 600 so the difference should be somewhere between 0 and 30 so that's how I come up with such values same thing for Y coordinate I know that this is not the correct state of egg being fallen into the basket the Y value should be something you can guess around by printing such values so I can safely say that the Y coordinate of the egg should always be less than Y coordinate of the basket because then I'll have that effect that egg is actually just over the basket and you can come up with such values using the same process now moving on to the building blocks these are the four building blocks of the game you need to come up with an idea of what it is, you need sounds and you need fonts these are the four things that take a lot of time writing a game is you have seen it super easy but you need the correct idea so what's my inspiration for it I've built games that I've played in my childhood so you can do that any games that you've played you can try to replicate them images, you can use these links you can download free images from these two sites but especially on Flickr if you're downloading images from Flickr just make sure that you're downloading images which provide you creative license to download otherwise the owner of the image can sue you for inappropriate use so just beware of that and there are licenses like you can't use it for commercial purposes, you can use it for art for some site projects or something so use that license for sounds this is a great site, you can download free sounds from it similarly for fonts these are the few resources that you have this is the official GOSU Github repo and the GOSU library and then third link is my Github account, you can look at the other games that I've written, I've written couple of more games which were not worthy enough to show here so and then this YouTube link it's a talk by Mike More which I just mentioned in the previous slides it's a great talk, if you find this thing remotely interesting, you should definitely look at his talk also that's a thank you