Step 5

Taking damage from the boss


Next, let’s work on coding how our player and the boss interact.

Create an if statement that checks if we are attacking when we collide with the boss. We will code this next.

First, let’s code what happens when we collide and are not attacking.

We first lose a life. Then we animate both the player and the boss. The player animation is a simple blinking animation. The boss animation is a pre-made one.

Lastly, we push the boss away from us a bit by multiplying its x and y velocities by -4.

Checking for a successful attack


Next, let’s work on coding our player attack.

Because our player can attack in different directions, we need to check if they are attacking in the direction that the boss is.

Since we have four directions, we need to have four checks. We also leave one else statement in case we attacked in the wrong direction (if ALL of these checks fail)

Each check works buy comparing the coordinates of the player and the boss. If the check is successful, we destroy the boss!

Checking for an unsuccessful attack


Next, let’s work on our last else statement, if the player attacked in the wrong direction.

The code here is the same as if we didn’t attack, feel free to copy and paste it here.

Winning the game


Lastly, we need to be able to win the game.

Add the code as shown to win if we defeat the boss!

Testing the code


Now let’s go ahead and test out the game. If all the code is correct it should be complete!

Now that the core mechanics are complete, think of some other things you can add to this game. There is a lot of potential with this one!