Code Warrior 7

Step 1
Making a Boss Enemy!

In this lesson, we will make a Boss Enemy that will have extra health and will shoot other enemy bombers!

Step 2
Let's start by making our enemies unable to spawn for a better testing environment

Comment out the checkLevel function call from the draw loop:

Step 3
We currently have one enemy class that has two different types within it.

This is NOT a good way of doing different types of objects!

Step 4
We should have made a general enemy class and then specific classes for each type

The extends code will make the new class inherit from another class, meaning it can use variables and function from the inherited class.


Since this will take a lot of time to rework the code, we will skip doing this for the strafer and bomber enemies. However, we will be implementing a new subclass for the enemy boss.

Step 5
Add a new class named Boss that extends the Enemy class

Add this to the bottom of the enemy.js file:

Step 6
To prove that the Boss class can use the variables and functions of the Enemy class, add a Boss to the enemies array

Add this line of code to the setup function inside index.js:

Check Your Work
Step 7
Save your code

We should see a regular bomber enemy.

This was just a test to prove that extends will give the Boss class all the stuff inside the Enemy class.

Step 8
Change the code to make the new Boss object not pass in a type.

We mentioned this earlier, we will not be using a "boss" type inside the enemy class but instead have sub class called Boss.

Step 9
Let's go back to the Boss class and add a new constructor

The super code is calling the Enemy's constructor function and adding all those variables to the Boss.

Step 10
Just to clarify, the "super" code is running the constructor function from the inherited class.

Do not add any code in this step!

Step 11
Now that we have some variables, we need to overwrite some of them for the boss enemy.

The boss enemy will be bigger, slower, and always Doug.

We also need to add a health variable so it does not instantly die.

Check Your Work
Step 12
Save your code

The game screen should show our new Boss enemy!

Step 13
Add a draw function to our Boss class to draw the health bar and image

We need to overwrite the Enemy draw function because the Boss will be drawing a health bar as well.

Check Your Work
Step 14
Save your code

We should see a green bar on the bottom representing the health bar.

You may notice that the Boss still dies to one shot. We will fix that in the next few steps.

Step 15
FIND the code that checks when the enemy is hit with a player projectile

This code is inside the index.js file:

Step 16
Let's explain what the collision code is saying using pseudocode to clarify
Step 17
We will need to change the way we deactivate the enemy so it takes health into account
Step 18
To check if the enemy is a Boss type we will need to learn some new coding concepts

The code instanceof will check if something is a class object of a certain class

Step 19
Change the collision code so the Boss will take damage instead of immediately dying

The pseudocode looks like this:


The code will be placed here:

Check Your Work
Step 20
Save your code

We should now be able to hit the enemy ten times before it disappears.

Step 21
Let's add a move function to our Boss class

The Boss will move only one time from off screen to the starting position

Step 22
Let's also start the Boss above the screen so it can start moving

Change this code inside index.js:

Check Your Work
Step 23
Run your code

We should see the enemy Boss move down until it fully enters the scene

Step 24
We will be shooting 3 bomber enemies from the sides and bottom of the Boss enemy

The locations will look like this:

Step 25
We will need to know 3 different X positions for each enemy

The leftX, midX, and rightX are as follows:

Step 26
There will be only 2 Y positions we will need to know because the side enemies will be the same

The midY and bottomY are as follows:

Step 27
Make a new shoot function inside the Boss class

The previous two lessons should help you fill in the blanks!

Check Your Work
Step 28
Save your code

The boss should now shoot enemies every 3 seconds

Step 29
Lets now update our game to spawn the Boss every ten levels

First copy and delete (or cut) the code that added the Boss enemy inside index.js:

Step 30
Add some logic to the checkLevel function to spawn a Boss

Every ten levels and starting at level ten, we only want a Boss to spawn

Step 31
Let's also uncomment the checkLevel function to test the game
Step 32
If you want, comment out drawColliders() in the update function from the player and projectile class
39 40
Check Your Work
Step 33
Save your code

If your game looks like this, then this project is officially over!

Code Warrior 7 Info


MVCode Clubs

Created By

Missing tiny aaronjau101


Code Warrior

Access Level


For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.

Learn more about MVCode Teach