Create and Display Enemies

Learn how to create and move enemies in the game.

Introduction

In this lesson, we will be building the enemies that will be shown on the canvas. Remember that each enemy object will be moving in a vertical direction and bouncing back when it reaches the boundary of the canvas. You have already written the logic to move an object vertically and then bounce it back in the Coding Challenge: HTML Canvas with Moving Object lesson. The only difference is that here, our object is an image.

Now, let's jump to the code.

Create the structure of the enemy

As a first step, we need to create the structure of the enemy, which will contain the position of the enemy and the speed at which the enemy will oscillate. Let's build the structure now.

Get hands-on with 1200+ tech skills courses.