 Okay, so in this section, we're going to begin by checking for user input. So we're going to just check if the user clicked down, up, left or right. So to do that, we're going to check if type is equal to SDL key down. And if that's the case, we're going to check which key in particular to the user press. Now what we have to do is we have to assign some value to a direction. So let's say if the user clicked left, we want to specify that the snake is moving to the left. If the user clicked right, we're specifying that the snake is moving to the right. And to do that, I think we are going to want to create a direction variable. So up here, I'm just going to implement a direction variable. And then I'm going to create an enum with the different directions. If the down key is pressed, what we're going to do is we're going to set the direction to be down. So we say the IR is equal to down. Then we are just going to check the other cases. Okay, good. So now what's going to happen is if the user clicks down, up, left or right, we are going to set the direction to the respective value of the enum. So next what we have to do is we actually have to move the head. So in which direction is the head of the snake going to move? Now to create the head, or to create the data structure for the head, I'm just going to use SdlRect because SdlRect has an x, y, width and height values. So that can work pretty fine for our purposes for now. So I'm going to declare a new SdlRect and this rect is going to be called head. And we're going to initialize it to be at position 500, 500. Now it's at position 500, 500 because the size of this window is 1,000, 1,000 as you can see up here. So we're going to create a new rectangle with the size 500, 500. And the size of this is going to be 10 by 10. Now this is actually fairly important so make sure that you have a 10 by 10 size for your rect because later on I'm going to do some stuff that is actually going to require you to have a 10 by 10 size. Okay, so now that we have a head, we are actually going to move this around the screen. So still down here, actually now outside of this, what we're going to do is we are going to, okay, so what we're going to do here is we are going to check in which direction we are moving. So we're going to do a switch statement and then we're going to switch on the direction. Now we're going to have a couple of cases. So the first case is if we are moving down, we are going to increase Y by 10. Okay, so now that we have basic movement implemented in this game. Whoops, I should have put this here. Okay, so now that we have this basic stuff in the game, what we're going to do is we actually are going to draw the body. And to draw the body, as you would know in STL, you first have to set the color that you want to draw with. Then you're going to just draw your structure. So in this case, we're going to draw like this. Okay, so now that we have that finished, what we're going to do is we're going to compile this and see what we get. Okay, as you can see, I have a square on the window, the square is moving around. When I click up down left or right, it moves in the direction that I specified. Okay, so now that we have the basics out of the way, let's actually begin writing our program. Now we're going to need a couple of libraries and I'm going to include three of them. I'm going to include Vector, we're going to include Algorithm and I'm going to include DQ. So I'm going to explain why we're going to need these later, but for now, just make sure you have them included. Okay, so what we need now is we need a container that's going to store our snake. So our snake, let's kind of think about it. What does a snake game entail? What is a snake game? So a snake game, let's say, let me just like draw it kind of crudely here. Okay, so let's say we have snake, right? And we have a little snake, let's just say this is a snake. So in snake what we do is we constantly remove something from the end and then add it to the front. So let's say at the end we remove one of these symbols and then in the front we add it here. So that's like a single instance of the snake moving forward. So let's say another instance is it moves forward again, right? And then occasionally it runs into an apple and then when it runs into an apple, what it's going to do is it's going to grow. So we just need to add another one and then leave this as it is. So to do this kind of thing, I think the best container to hold a data structure like that would be a DQ. The reason why DQ is really good for this is because a DQ you are able to push from the front, pop from the front, push from the end and pop from the end. So I think this is really good because doubly ended Q, it lets you delete from the front, delete from the end, it lets you add from the end and add from the front. And that's why we are going to make, that's why we're going to make the entire body of the snake. We're going to make this doubly ended Q container. So this container is going to be doubly ended Q for the snake body. And we're going to keep the head for now, but what we have to do here is we have to add the snake body container. And we're going to call this, I'm going to call this, I'm not sure what to call it. Just say STD and we're going to say DQ. And this DQ is going to be, it's going to be composed of STL rects. So we're going to have a bunch of STL rects and these rects are going to be called RQ, rect Q. I think that's a good idea. Let's just call this RQ. And I think we should also include, so the game world is also going to need apples. So because it's going to need apples, I think we should create a new container and this container is going to be used for apples. And I'm fresh out of ideas. So you can basically use anything you want. I'm just going to use Vector because I always use Vector for just the general containers. And this is also going to be an STL rect container. So we're going to contain STL rects in this one. Okay, so now that we have that, we actually want to populate the game world with a bunch of apples. And here we're going to just generate a whole bunch of apples at random positions on the screen. And yeah, so we're just going to make a whole bunch of apples here outside of the main loop. Make sure it's outside of the main loop. You don't want to keep adding apples to the screen every single iteration. And here we are going to decide how many apples do we want. So let's just keep it simple. We're going to have 100 apples at one time. And then we are going to increase i by 1. We don't really use this variable. So all we are doing here is just iterating 100 times. And for every time we iterate, we're going to add a new apple to the screen at a random position. And what I'm going to do here is I'm going to do a modulus 100 and then I'm going to multiply it by 10. I want every apple to be in a location that's divisible by 10 and I'm going to explain why a little bit later. Then we have to set the size of these apples and we're just going to set the size to be 10 and at the same as the head. So the head and the apples can be the same size. And I think that should be enough. So we drew all of our apples. Or should I say we added all the apples to the game world. So all the apples are now in the apples vector. So next what we want to do is we want to specify the size of our snake. So how big is our snake right now? And we specify the size like this. So it's going to be pretty simple. I should actually define it here. So the size of our snake is going to be, not SDD. So the size of our snake is going to be just called size. So int size is equal to one. So I'm going to say the size of the snake is one. And yeah, when you start the game your size is going to be one usually. Okay, so that's enough for the setup. So basically what we're doing for the setup is we're creating a DQ. We're creating an apples vector. And then we're creating a bunch of apples for the snake to move around and eat. Now what we have to do is we have to do some collision detection. So there are two stages for collision detection is one, we want to check the collision between apple and the snake. And another collision detection we want to do is we want to do the collision detection between the snake and itself. So is the snake, is the head of the snake touching its own body? And if it is, we're just going to set the size to one again. So kind of like indicating that you have to start over. And we're going to do our collision detection here. So we're going to say collision detection. And the first thing we're going to do is we are going to check the collision between the apple and the head of the snake. So actually, before I do that, let me see if I include it. Yeah, I include algorithms. So I'm going to use a couple of algorithms in this example. So what we're going to do is we're going to say STD for each. And then we are going to pass in a begin. We're going to pass in apples.begin. And what I'm doing here is I'm just going to specify the iterator. That's the beginning of the apple. And then the iterator, that's the end of the apples container. And then what we have to do here is we have to pass in a lambda expression. So this lambda expression is just going to, it's basically, what it's going to do is it's going to, how should I put it? It's going to do the collision detection. So it's going to check if the collision happened. And if it happened, we're going to do something. Okay, so what we're going to do here is we are going to check if the head is touching any of the apples. So what's going to happen is this E variable, it's going to be each of the apples on the screen. So it's going to go for each apple, or maybe I should just call this apples, or apple. That should be a little bit easier to understand. So what's going to happen is we're going to check the collision between the apple and the head of the snake. We're going to check every single apple against the snake. So let's do that. Okay, so here we are going to check the collision and we're going to say if head.x is equal to apple.x and then we are going to say and head.y is equal to apple.y. Okay, so what happens when the snake eats the apple? Well, what happens when the snake eats the apple is the apple gets removed from the scene and what we do is we increase the size of the snake. So because of that, we're going to say size plus equals 10. Don't forget we define size up here and the original size is one. So what happens is every time the snake eats an apple, we're going to increase its size by 10. You can adjust this however you want. And to remove the apple from the scene, we're not going to do anything fancy here. I'm just going to move it off the screen so that the snake can't see it. Okay, so I'm going to say apple.x is equal to negative 10 and apple.y is equal to negative 10. Okay, that's good. So here this is our collision. Collision detection with apple. Okay, next, we have to do our collision detection with itself. So did the snake touch itself? And if it touched itself, what do we do? How do we handle that? And here we're going to say... Okay, so here what we're going to do is we are going to do another four each, so STD or each. And then we're going to pass in the beginning and the end of our queue. And here we are going to pass in another lambo expression and this lambo expression is going to be specific for the body of the snake. Okay, now we're going to do head.x is equal to e.snakesegment.x and head.y is equal to snakesegment.y. Okay, so what do we do if the snake head touches the snake body? Well, I think the easiest thing to do here is just say, okay, so if the snake touches its own body, we're just going to set the size to one. So kind of like reset the snake. We're just going to reset the snake to its original state. And here what we're going to do is we are going to push... we're going to push the head because as you can see, every single iteration of this loop, every single iteration of the game loop, what we're doing is we're adding 10 to the head or like we're either adding 10 to the head or subtracting 10 from the head. And what we're going to do here is we're going to put the newest version of the head to the front of this queue. So to do that, we're just going to say rq.pushfront and then we're going to push the head. So we're going to push to the front of the snake a new head. And then to make sure that the snake is shrinking at the end, what we're going to do is we are going to pop the last item from this queue. So we're going to do it like this. So here what we're going to do is we're going to check if rq, the size of rq is exceeding the size that's specified by the game. So for example, let's see. So this size here in size, if this size is smaller than rq, then we have to remove items from the end of the snake until it gets to the size that's appropriate for the rules of the game. So for example, here we added a new head to this snake. So because we added a new head to the snake, we also have to pop the tail from the snake. And to do that, we're going to use a while loop because we can also set the size to 1 here. So for example, if the snake's size was 99, then it touched its own tail and its tail got set to 1. What we have to do is we have to remove all of the segments so that it goes from 99 to 1. And to do that, we're just going to do while rq.size is greater than size. So if the size of this q is larger than the size that's specified by the game, what we're going to do is we're going to say rq.pop back. So we're going to remove the last element from the snake until it complies with the size that we specify. So what's next? What's next is we're going to draw the body. So we're going to draw the body of the snake. So to draw the body, I'm just going to modify this here. So here all I'm doing is I'm drawing the head. But what we want to do now is we want to draw the whole body. And to draw the whole body, I'm just going to do another 4 each. So I'm going to say std or each. rq.begin. Then I'm going to say rq.end. And then we're going to do another lambda expression here. And this lambda expression, I don't think you need to trap anything here. So I'm just going to leave it as it is. Auto. And then we're going to say snake segment. Okay. So here what we're going to do is we are going to fill a rect for each snake's act. And the fill a rect is just the STL's way of drawing to the screen. So fill a rect, basically just draw a rectangle specified that you're going to pass in. So what we're going to do is we're going to pass in a renderer. Actually, I do need to trap something. I need the renderer. So we're going to say renderer. And then we are going to pass in the snake segment, which is an STL of rect, I believe. Okay. And we don't need this anymore. It's going to delete, actually. Okay. I think that's, I think that's, that's pretty much it. So this is about 130 lines of code. And let's see if it works. I hope this works. Okay. So g plus plus. And then we're going to specify the snake. Actually, I'm on STL2. I think the STD, C plus plus 20. I think I need that. Oh, snake. Oh, okay. Okay. We forgot to draw the apples. So let's go back in here. And we have to draw the apples because if you don't draw the apples, you're going to have a bad time. Okay. Okay. So to draw the apples, we're basically going to do the same thing, except this time, I think we should just set the color to be red because apples are red. Okay. Let's compile that again and let's run it. Okay. So as you can see, I'm grabbing the apples. Every time I collide with the apple, it disappears. And I grow by 10, by 10, not pixels, but 10 times 10 pixels. So, and if I collide with myself, I become, I just reset the game. I'll not reset the game, but I reset the size of the snake. There you go. That's a snake game.