The game is getting pretty playable, but like all good things it needs to come to an end.
If you remember from our little design exercise, we have a goal that will end the game. When we defeat the Big Bad Boss.
In this step, we will add the Boss into the game and create a Game Won state for when the player defeats the Boss.
Getting to the Big Bad Boss
The boss is going to be another character that we add to the game. We want him to be tougher than any of our enemies so far and we want him to shoot at the player to make it challenging.
Let’s start by creating the Boss character.
So now we have a field where we can put our Boss and we have a method that we can call to create him, but we don’t want to call it right away because we only want to have our Boss show up when we get far enough into the game.
So we will need 2 more things. First we need something to track whether or not it is time for the Boss to show up, and once this tracker is satisfied we don’t want to create another Boss.
Ok, at this point when you run your game, if you survive for 15 seconds or so you should get to a point where the small enemies stop getting created and you see a large purple shaded box representing the boss.
Boss Behavior
Now that we can get to the Boss, we want an EPIC BOSS FIGHT! Ok, maybe it won’t exactly be epic at first, but we can start by giving him some basic behavior. We also need to add the ability to shoot the Boss.
To start with we want to add some simple AI to the Boss’ movement. Nothing fancy, just moving the Boss up and down.
Now your Boss should go up and down the screen. Really, really simple pattern for now. We can get fancy and create more complicated logic for the movement later.
Damaging the Boss
Now that the Boss is moving, we need to be able to destroy it so we can win the game. This means we need to wire up the collision detection for our player’s projectiles and the Boss hit box. We also need to make sure the Boss takes damage when it gets hit.
Now projectiles will collide with the Boss but our current Character code won’t handle this correctly after the Boss gets hit. So we need to make a few tweaks to the Character class.
Now you will need to go into your main Game class and update the constructors for your Player and the Final Boss. The regular enemy constructor is fine since it just uses the default. There is also a check when firing projectiles that needs updating.
Now your projectiles should hit the Boss and damage it. However you should notice that once the Boss’ health drops to 0, the Boss is still there and the projectiles just fly past.
We still need to make the OnUpdate method a little smarter about what happens when the Final Boss is destroyed.
Destroying the Boss and Next Level
Destroying the Final Boss is simple enough. In the OnUpdate code, simply add a check to remove the Boss from the game when his health reaches 0. But what we really want to do is change scenes to a show that we beat the boss.
In order to give us some room to give the game more depth, we are not actually going to have the player win the game after beating the first boss. Instead we are going to have the game transition to a Next Level scene.
Now we have a scene we can transition to, lets transition to it when our Boss dies.
And we don’t want to get stuck there so lets add a method to allow the player to continue.
Now when you play the game, after you destroy the Final Boss, you should get to the Next Level scene. Then if you hit enter it should start play again but your score will remain the same.
Actually Winning
What we really want to do in this chapter is give the player the ability to beat the game. In order to do that we need to add 1 more scene, the Game Won scene.
If you have been following along, you have already gotten pretty familiar with adding scenes to the game by now, and could probably do this yourself. Just in case here is the code for the Game Won scene.
Now we need to be able to actually get to this scene. Defeating the Boss doesn’t get us there, it only gets us to the next level, and right now we have infinite levels.
First let’s put a way to track what level we are on and set a maximum number of level. Then we can use those 2 values to tell us when to end the game.
Now whenever we get to the Next Level, lets update our current level and see if it is the final level.
Now when the player beats the boss on the last level, they have beat the game.
In the next section we will make the Boss a little tougher and let him shoot back.