We've reached the last lecture in module 11, so as a final reminder, here are the module 11 learning objectives. So, last time we finished implementing our rubber chicken class. And it's time to actually put that class into a game. And so, we're now going to build a complete game that includes the rubber chicken as one of the classes in the game. I've already written all the code, but I've also commented all out again, and captured the sequence that I followed to develop the code. So I'm going to bring you through the process from start to finish, and I won't actually be typing code, I'll just be uncommenting pieces I already typed in. And talking about sort of interesting things as we get to those pieces. So let's go right to the IDE, I've already got it opened up, and zoomed, thank goodness for both of those. So the initial stuff, and I'm going to try not to get dizzy, I'm going to pop back and forth between the Power Point, and the code so that we can look at each step. But before we start doing that, I want to show you the core things that I've already included in the game. So we already have the standard constants for window width and window height, I have some information about chickens, how many chickens should be in the game, and how much damage each chicken inflicts. I'm saving the three sprites that we're using in the game for efficiency in this texture 2Ds, because we're going to be creating bears and chickens and explosions all over the place. So just loading this content once and then saving it and using it as we need it is a good idea. We have some game entities. We have a list of rubber chickens, a list of bears, and a list of explosions. And you will notice that in this constructor, for the list of rubber chickens, I've actually provided an argument, whereas, all the lists we've created so far I haven't done that. This just tells the system how large that list should be. So it's the capacity of the list, and I actually know I will always have exactly num chickens, chickens so if I provide the capacity first, then I don't have to grow the list dynamically. We have teddy bear spawn support, I have speed for the teddy bears, they'll spawn every two seconds. We've seen this timer stuff before, and I will need a random number generator to generate the random x location for teddy bears that I spawn. I've set the resolution and made the mouse visible because we click on rubber chickens to launch them. And we want to make sure the player can see where the mouse is to do that. In the load content I haven't done too much exciting here, I've loaded these three sprites, so you've seen that before. But I'm also calling this spawn initial rubber chickens method. That doesn't come free in the game one class, I wrote this, so let's go to the definition, I right-clicked and about halfway down, so this is just writing a method like the methods we've already written to roll the die. To tell the rubber chicken to draw and update themselves, this is just another method that I've written. Now, it turns out that this one is slightly different, because I've made it private. Nobody outside this class should ever be able to spawn the initial rubber chickens and so I made that private. And it can only be used from within the game one class, and specifically, it's only used from the load content method. So I'm actually going to do some math here, where I figure out what the spacing between the rubber chickens should be based on the window width and how many rubber chickens I'm going to have. And I then set the current x, so this is the x for the rubber chicken I am about to create to x spacing over 2. So this is going to be way over near the left side of the window though not all the way up against left side of the window. I then spawn the chickens. I'll talk about this get chicken start y method right now. Again, another private method. And really, it just returns window height, so the very bottom, minus chicken.height over two. So this is going to put the chicken standing on the bottom of the window, basically. I wrote this method because I discovered that numerous times I needed to do this exact same calculation. So instead of doing the calculation at three different places in my code, I did it in this one place instead. So, I get the chicken start Y, and now I do a FOR loop. This is much like the FOR loop when we had the initial num teddies a long time ago when we were learning about FOR loops. And so I go through the FOR loop, I add a new rubber chicken, with a sprite, a location and how much damage it inflicts. And then, I change the current x by adding x spacing to it, so that the next time around the loop, when I'm on chicken number one, our second chicken, it will be at x spacing plus x spacing over 2. So I'm, you know, putting them in a row across the bottom of the screen. And I do that with that for loop and now if we go back to the Power Point slide, we see that I have done this step, I've spawned the initial chickens. You can't see and by the way, this is exactly the order in which I did the steps as I was creating this game. I got to you know, step three and said hey, I should keep track of this so I can go in the same order when I do the lecture. So we spawn those chickens but you can't see 'em. So, we should draw all the game entities. So, let's go up to the draw. And I'll just uncomment this code. And you might think reasonably, that I should just be drawing the rubber chickens at this point, why am I drawing teddy bears and explosions, because those lists are empty right now and I know it. To be honest, when I came here I said, I might as well draw everything right now while I'm thinking about it, rather than waiting until later and realizing I forgot to draw the explosion and then coming down and, you know, spending debugging time. So I did it when I thought of it. And now, now that I've uncommented this I can run it. And we see a row of rubber, beautiful, beautiful rubber chickens along the bottom of the screen. Okay. So now, I have drawn all the game entities. So the next thing says, comments for all the updates that need to happen. So at this point, I started thinking about how the game would really work and I went up to the update method and I put up a set of comments. I said, you know, make sure you update all the game entities, check for collisions between chickens and bears, clear out dead teddies. The sequence of steps that I knew would need to happen on every update once the full game was running. We'll just look at those comments as we go along because it's hard to see them with all the code interspersed between them. But this was stopping and thinking and not writing any code, just sort of, capturing my ideas about the sequence of events that had to happen in that update method. So I did that and now we should update all the game entities. So I'll scroll up to the update method, as you can see, there's lots of code in here, but here's the upfate the game entities, and so I'll uncomment all of this. And really I wanted to update the chicken because, you know, once I drew chickens I wanted to click on them and I clicked on them and nothing happened. And so I was upset, and so I went and implemented this. And because I was updating the chickens, I knew everything would need to be updated. So I just added updating all of them now I made a comment here, by the way, that I changed the rubber chicken speed for finer granularity. So let's go to the rubber chicken. So in this field that is rubber chicken speed, I made it a float, and I made it 0.8 f. the f is required because it's a float; otherwise, the compiler will think 0.8 is a double. So I've made that a float, and I realized, when I was doing that, that I regularly make the speed a float because we usually want to have finer control over the speed of our game entities than integers provides to us. So now if I F5 it, I can click on a rubber chicken and he flies up, and I can, you know, click on all the ru-, well not all, I'll get bored, but I can click on rubber chickens to launch them. And so, now, we've noticed that we are, well, we've noticed, because I just said we're updating all the game entities. And the next thing is, when a chicken flies out of the top of the window, we should do something. We should either remove that chicken from the game, make it inactive, so that you know, we clean out the dead rubber chickens later, and it's reclaimed. And, and that's a perfectly reasonable thing to do. And in fact in the optional project, you will be, if you choose to do it, you'll be removing inactive projectiles, from the game. Now for chickens, the way I wanted this to work is, I pretty much always wanted the bottom row of chickens to be full. Let's see, that's not exactly correct. For each, think of it as 20 columns. Here, why don't I run it and then we can look at it. So, I wanted there always to be a chicken in this zeroth column. So either he's sitting down here, or he's flying his way up. But once he's gotten to the top and outside the window, I wanted a new chicken to be put here, so that you always had each column have a chicken in it. So, the problem, or one of the alternatives was to destroy the chicken, and spawn a new one. However, what I decided to do instead was this. Let's go to the check for chicken leaving the window. Right here. Instead of destroying it, I decided to move that chicken back down to the bottom of the column. And so you'll notice I added a reset method to Rubber Chicken so we can go to the definition of that reset method here. And so it puts the chicken back at the bottom of the window It sets its velocity back to zero, and it makes moving false. And just so you know, [LAUGH] I originally had this as a set y rather than a reset. So, I just moved the chicken back to the bottom of the window. And because the velocity was still positive, and because it was still moving, it just kept scrolling up, so it would, you know, pop down to the bottom and go up, and pop down to the bottom and go up, and while that was very hypnotic, I didn't like it cause it wasn't how the game was supposed to work. So I had to change this to reset. So notice. Right, that it's okay, we develop, we design and implement our classes, and then when we start using them, we may well discover that there were things that we didn't know we needed and we discover we need em as we start using the code. It's a perfectly natural part of developing games. And so, I felt perfectly free to add methods as I needed them throughout the code. Okay, so why did I move the chicken down to the bottom again instead of just destroying it and creating a new one? Because this is better for memory. There's no need to throw away the chicken to the garbage collector has to go pick it up, just be...just so I can create a new chicken in the same column. I can just move this chicken right back down, and so that saves us a little bit on memory. So, we've done the replacing chickens part. And at that point, I could shoot chickens, I could launch chickens up toward the top of the window, and then when they exited the window, they came back to the bottom. So, let me run that to show you. So, I can send a chicken, and when he's out, I get a new chicken, really the same chicken, back in that column. This has turned out to be another long lecture, so, I'm breaking this one into two parts as well. This is the end of part one.