I’ve always found that, for me at least, one of the best ways to learn a new programming language is to make a game. It’s how I learned ActionScript 2 and then ActionScript 3 and it’s really helped me learn JavaScript. I’ve become a bit obsessed with the canvas tag over the last couple of weeks and I really wanted to see how easy or difficult to create a game with it compared to Flash. To see how difficult it is, we’ll build a vertical scrolling shooter game.

We’re going to start off by creating a ship that we can move around and then five enemy ships that move from the top to the bottom of the canvas. We won’t worry about game graphics in this part, we just want to get the functionality down and then build on that. I’m not going to worry about compatability with browsers like IE8, if the canvas doesn’t work in a browser, that’s it’s problem. This is just an exercise in learning how to make something that will work with a modern browser.

I’m not going to go over the basics of the canvas tag. If you don’t know how it works, you can learn more here. The canvas and ctx variables are for find in the canvas and then drawing on it, width and height are the canvas dimensions. The ship variables are for both the initial positioning and dimensions of the square that’s going to represent the ship for now. Next let’s write the code that will actually draw the ship block on the canvas.

So this code will get us this. A green block near the bottom of the canvas. Let’s go through the functions and see what’s happening here. The clearCanvas function is the something that we have to have in order for this to work. We have to redraw the canvas every time we we run the game loop and we need to clear everything on the canvas first or else we’ll get multiple copies of the ship on the canvas. So we clear it using the clearRect function and we set it to clear the entire canvas. Next we have the drawShip function where we set the fillStyle to green and then use fillRect to set the ship’s x and y positions and it’s width and height. The init function is what we use with the window.onload function at the bottom to get everything started. We get the canvas element, then get it’s context and finally, we use setInterval to call the gameLoop function every 25 milliseconds. With the gameLoop function we call clearCanvas and drawShip. This is the heart of our game, the init function sets everything up and then gameLoop makes it run.

Here, we are checking to see which key is being pressed and if it’s one of the arrow keys, then we change the corresponding variable to true and when that arrow key is released, we change the variable back to false. This will all set up the movement of the ship. Now, change the drawShip function to this:

Now we are checking to see if the left or right arrow key is pressed and if so, move the ship 5 pixels in that direction, the same with the up and down arrows. At this point, we’ll have this. Ok, now let’s add some enemies to our game. First add these variables to the ones at the top of the page:

We are setting the total number of enemies, the array we’ll store our enemies in, the positioning of the first enemy, the width and height and finally, the speed our enemies will move at. Now, we’ll use a for loop to add our enemies to our enemies array:

We just run another for loop and draw an enemy for each enemy that’s in the enemies array. And as it runs through the entire array, it uses the individual x and y positions of each enemy in the array. This is important now for the x position of each enemy and will be import down the road when we want to replace dead enemies. Next, let’s get the enemies to move.

With this function, we’re running a for loop to move each enemy and checking to see if they’ve passed beyond the bottom of the canvas. If they have, then we move them back to above the top of the canvas, so it seems like an new enemy has entered. We still have one more thing to do, we need to change the gameLoop function to actually call these functions.

Now, you should have this. We’ve got the basics of our game and next time, we’ll change the colored boxes to actual ship graphics and give our ship the ability to shoot lasers. Here’s the complete code:

3 Responses to Build a vertical scrolling shooter game with HTML5 canvas – Part 1

This is a very informative tutorial, thank you for it! I have a quick question that might be kind of obvious. I’m not quite sure how the event listeners work and would like to know if you could clarify a bit. From what I understand you call addEventLIstener() to the page to determine what is being done by the user regarding any type of event, but I’m not quite sure I understand how the code is getting the actual key codes for the events.

In the code you pass an ‘e’ parameter to the keyDown and keyUp function and I don’t quite understand how the code knows what to do with that parameter or how it actually goes about getting the keyCode for that event and was wondering if you could clarify a bit. I have tried to research it on the web but haven’t really found a good answer as to why that e parameter has a keyCode attribute.

“In the code you pass an ‘e’ parameter to the keyDown and keyUp function and I don’t quite understand how the code knows what to do with that parameter or how it actually goes about getting the keyCode for that event”

When an event is fired in the browser window (for example a mouse click or a key is pressed) and you have set up an event listener to ‘wait’ for this event to happen, the browser will fire the function that was told to wait for that event when the listener was set up.

Usually the event type (in this example a key is pressed) will carry with it a parameter that identifies the source of the event. For event types involving key presses; the event will carry with it the ‘code’ for the key that was pressed.

It’s common practice in web programming to label this event parameter as ‘e’ or ‘event’, and by using the identifier ‘e.keyCode’ you can retrieve the value stored in the keyCode parameter – then it’s a simple matter of comparing this value against a list of known codes for certain keys. If you know the key being pressed is an arrow key, you can run the code that updates the player’s ship position in that key direction.

Your email address will not be published. Required fields are marked *

Comment

Name *

Email *

Website

I'm Mike Thomas and I spend most of my time making things using HTML/CSS/JS. I'm currently obsessed with HTML5, JavaScript and jQuery. Sometimes I say something interesting on Twitter, I put code up on github and I post interesting stuff on Google+. And check out my RSS feed so you don't miss anything.