<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Atomic Robot Design &#187; HTML5</title>
	<atom:link href="http://atomicrobotdesign.com/blog/tag/html5/feed/" rel="self" type="application/rss+xml" />
	<link>http://atomicrobotdesign.com/blog</link>
	<description>Design From The Future &#124; Blog</description>
	<lastBuildDate>Mon, 06 Feb 2012 03:13:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Quick overview of using addEventListener in HTML5 games</title>
		<link>http://atomicrobotdesign.com/blog/web-development/quick-overview-of-using-addeventlistener-in-html5-games/</link>
		<comments>http://atomicrobotdesign.com/blog/web-development/quick-overview-of-using-addeventlistener-in-html5-games/#comments</comments>
		<pubDate>Mon, 10 Oct 2011 02:03:31 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[web development]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://atomicrobotdesign.com/blog/?p=1172</guid>
		<description><![CDATA[Coming from ActionScript, there are a few things that I forget people who are learning JavaScript who have programming backgrounds with languages like PHP or even straight up HTML/CSS might not be familiar with some of the functions or methods &#8230; <a href="http://atomicrobotdesign.com/blog/web-development/quick-overview-of-using-addeventlistener-in-html5-games/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Coming from ActionScript, there are a few things that I forget people who are learning JavaScript who have programming backgrounds with languages like PHP or even straight up HTML/CSS might not be familiar with some of the functions or methods I use in my tutorials. One thing that I’ve had a few questions about was addEventListener, which is something that you learn about right away when working with AS3. It’s a little different with JavaScript but the basics are the same. This isn’t going to be anything indepth, I’m going to explain enough about it so that when you’re building a game or something, you’ll know what’s going on.</p>
<p>First off, the basic explanation of addEventListener is pretty simple. Let’s take this line of code as an example:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">window.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'keydown'</span><span style="color: #339933;">,</span> keyDown<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>This line does this, add an event listener to the window object, the event to listen for is the keydown event. When that event happens, run a function called keyDown. Finally, there’s a Boolean for if you want to useCapture or not. If useCapture is set to true, then the window object in our example would get notified of the event before the keyDown function would. In the context of a game, I think you’ll have this set to false nearly every, if not all the time.</p>
<p>Another question I’ve been asked has been about what is set to, in our example, the keyDown function. Take a look at this code:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> keyDown<span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp;<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">keyCode</span> <span style="color: #339933;">===</span> <span style="color: #CC0000;">39</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">//39 is the keyCode # for the right arrow key</span><br />
&nbsp; &nbsp;<span style="color: #006600; font-style: italic;">//do something awesome</span><br />
&nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>So when the key is pressed and the keyDown function is called, it’s passed the parameter e. What is this e? In this case, e is short for event. Some people use event, other evt, but I like e because it’s nice and short and you still know what it means. In the case of a game and in the above code example, we check to see if the key being pressed is the right arrow key, which has the keyCode number of 39. How do we know this? Well, if you were to console.log out e, this is what you would get:</p>
<p><img src="http://atomicrobotdesign.com/blog/wp-content/uploads/2011/10/event_example.jpg" alt="" title="event_example" width="645" height="513" class="aligncenter size-full wp-image-1174" /></p>
<p>You’ll see in the picture all the things that the browser gives us access to. In the case of a game, we’re interested in the keyCode which you’ll find about halfway down. To access that, we just write e.keyCode and we’ll get the number of whatever key is pressed. Then we can check and see with arrow key has been pressed and then, say, move a character in the right direction.</p>
<p>For the most part, the three event types that you’ll be listening for when making games are keydown, keyup, mousemove, mouseover and click. Keydown and keyup will usually be used together, for example, you’re moving a ship around in a shooter game, keydown will move the ship when the key is pressed and keyup will stop it when the player stops pressing the key. The other three, of course, are used with the mouse and are pretty self explanatory.</p>
<p>Remember, console.log is your best friend when it comes to learning JavaScript. If you don’t understand something, like say, a parameter that’s being passed to a function, just log it out. Most of the time it will show you exactly what you were wanting to know.</p>
]]></content:encoded>
			<wfw:commentRss>http://atomicrobotdesign.com/blog/web-development/quick-overview-of-using-addeventlistener-in-html5-games/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The less loops the better</title>
		<link>http://atomicrobotdesign.com/blog/web-development/the-less-loops-the-better/</link>
		<comments>http://atomicrobotdesign.com/blog/web-development/the-less-loops-the-better/#comments</comments>
		<pubDate>Wed, 27 Jul 2011 00:43:45 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[web development]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://atomicrobotdesign.com/blog/?p=1130</guid>
		<description><![CDATA[If you’ve ever built a game, whether it’s a Flash game, a game for iOS or a game built using HTML5 canvas, you’ll know that the thing that makes the game run is loops. When I built my first Flash &#8230; <a href="http://atomicrobotdesign.com/blog/web-development/the-less-loops-the-better/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>If you’ve ever built a game, whether it’s a Flash game, a game for iOS or a game built using HTML5 canvas, you’ll know that the thing that makes the game run is loops. When I built my first Flash game while in school, I just wanted to get the game to work, so I didn’t care how many loops I used but my games were never that complicated, so I don’t think it mattered all that much. But when I started to mess around, building games using HTML5’s canvas, I realized, not matter what anyone says, the browser isn’t as good at running JavaScript and rendering graphics as well as Flash is right now. So, if you want to make good games, you need to optimize your code as much as possible.</p>
<p>Over the last few weeks, I’ve been really optimizing the vertical space shooter game that I built for the tutorial I did. One thing I realized as I was working on it, was that I should be using only as many loops as I need to run the code. Right around the time I was figuring this out, I watch a video of a presentation done by Mozilla Evangelist <a href="https://twitter.com/#!/robhawkes">Rob Hawkes</a> where he said the exact same thing, that you need to be careful about how many loops you have running in your game.</p>
<p>Whats the big deal about having too many loops? Well, think about it this way. If you have five loops in your game and you’re running it at something like 30 frames a second, you need to run all five loops thirty times each second. Think about a situation where you have a for loop inside another for loop. This is a pretty common situation and, let’s say the outer loop runs six times and the inner loop runs 9 times, now you’re going to run that interior for loop 1620 times a second. If it’s doing any kind of hard work, that’s going to slow things down.</p>
<p>One of the things we as developers need to remember is that, if you’re like me, you’ve got a quad core processor and you’re probably using a browser like Mozilla’s Aurora or Google Dev version, so you’re code is going to be a lot faster. I bet most of the people out there aren’t going to be running a crazy fast browser on a crazy fast machine. So, and I’m sure you’ve heard this before, but optimizing your code is always a good idea, which is surprising how many developers out there don’t do it.</p>
<p>The moral of the story is this: use as few loops are you need so your code runs faster. I’m sure for experienced programmers this seems like common sense, but I think it’s a good thing to bring up for beginners.</p>
]]></content:encoded>
			<wfw:commentRss>http://atomicrobotdesign.com/blog/web-development/the-less-loops-the-better/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to use sprite sheets with HTML5 canvas</title>
		<link>http://atomicrobotdesign.com/blog/web-development/how-to-use-sprite-sheets-with-html5-canvas/</link>
		<comments>http://atomicrobotdesign.com/blog/web-development/how-to-use-sprite-sheets-with-html5-canvas/#comments</comments>
		<pubDate>Mon, 13 Jun 2011 00:34:33 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[web development]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://atomicrobotdesign.com/blog/?p=1107</guid>
		<description><![CDATA[Sprite sheets have been a part of game development since the mid-70’s and there’s a reason they’re still being used today. It uses less memory and processor power because you can load all of a character’s positions and poses in &#8230; <a href="http://atomicrobotdesign.com/blog/web-development/how-to-use-sprite-sheets-with-html5-canvas/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Sprite sheets have been a part of game development since the mid-70’s and there’s a reason they’re still being used today. It uses less memory and processor power because you can load all of a character’s positions and poses in one image file instead of a different file for each one.Then you just move the sprite sheet to only show the part you want. And the great thing is, you can do this with the HTML5 canvas tag too, which makes building games more efficient.</p>
<p>To do this, you just need to add a few move parameters to the basic drawImage function. Let’s use the ship from my canvas shooter game as an example.</p>
<p><img src="http://atomicrobotdesign.com/blog/wp-content/uploads/2011/06/ships2.png" alt="" title="ships2" width="229" height="85" class="aligncenter size-full wp-image-1109" /></p>
<p>If we want to drive the ship on the canvas, then we would use this code:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">canvas <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'canvas'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
ctx <span style="color: #339933;">=</span> canvas.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'2d'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
ship <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Image<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
ship.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'ship.png'</span><span style="color: #339933;">;</span><br />
ctx.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>ship<span style="color: #339933;">,</span> x<span style="color: #339933;">,</span> y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>If you don’t understand this code, it’s probably best to learn more about it before you get into sprite sheets.</p>
<p>To use drawImage to display our sprite sheet, we’re going to need  add six more parameters. And they’re divided into two main groups, the source info and the destination info:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ctx.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>sprites<span style="color: #339933;">,</span>srcX<span style="color: #339933;">,</span>srcY<span style="color: #339933;">,</span>srcW<span style="color: #339933;">,</span>srcH<span style="color: #339933;">,</span>destX<span style="color: #339933;">,</span>destY<span style="color: #339933;">,</span>destW<span style="color: #339933;">,</span>destH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>The first one is obviously our sprite image. The next four are the position and width of our source image. Then last for are the position and width of the final image when it’s drawn on the canvas. Usually, the height and widths with be the same value because if srcW is bigger than destW, the image of our ship will get smushed over to the side and will be able to see the next image on the sprite sheet. If destW is the larger number, than our image ship will be stretch across the wider width.</p>
<p>In this example, when the user pushes either the left or right arrows keys, we’ll reposition the sprite sheet to show the ship turning in that direction. I’m not going to go line by line through the entire code, just the parts that you need to know to understand what’s going on. For our purposes, making the ship look like it’s turning, we can use the same basic set up as when you want the ship to move left and right:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>rightKey<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; ship_x <span style="color: #339933;">+=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span><br />
&nbsp; srcX <span style="color: #339933;">=</span> <span style="color: #CC0000;">83</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>leftKey<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; ship_x <span style="color: #339933;">-=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span><br />
&nbsp; srcX <span style="color: #339933;">=</span> <span style="color: #CC0000;">156</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
ctx.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>sprites<span style="color: #339933;">,</span>srcX<span style="color: #339933;">,</span>srcY<span style="color: #339933;">,</span>ship_w<span style="color: #339933;">,</span>ship_h<span style="color: #339933;">,</span>ship_x<span style="color: #339933;">,</span>ship_y<span style="color: #339933;">,</span>ship_w<span style="color: #339933;">,</span>ship_h<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>rightKey <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">false</span> <span style="color: #339933;">||</span> leftKey <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; srcX <span style="color: #339933;">=</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>The first part of the code is pretty simple and anyone with any experience building games will know what’s going on there, if the right arrow key is pressed do this or if the left arrow key is pressed do this. But when the right arrow key is pressed, we aren’t just moving the ship right by increasing ship_x by five, we’re also changing what part of the sprite sheet we see by changing the value of srcX. In this case, it’s to 83 pixels, which displays the second image on the sprite ship, which shows the ship turning right. The same happens when the left arrow key is pressed. But there’s still one more thing to remember, the sprite sheet has the be reset, as it will have to be in most cases. A simple if statement, that checks if neither of the left or right arrow keys are being pressed, then reset srcX back to 10.</p>
<p>You can check out a demo <a href="http://atomicrobotdesign.com/blog_media/sprite_sheet/spritesheet.html" target="_blank">here</a>.</p>
<p>It’s actually a lot more simple than you would think it would be. Using a technique like this can really add to your game. Another thing to remember is that you could put every single graphic you need for your game on one sprite sheet so that you only need to load the one element and that could improve loading times for your game.</p>
]]></content:encoded>
			<wfw:commentRss>http://atomicrobotdesign.com/blog/web-development/how-to-use-sprite-sheets-with-html5-canvas/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pause your HTML5 canvas game</title>
		<link>http://atomicrobotdesign.com/blog/web-development/pause-your-html5-canvas-game/</link>
		<comments>http://atomicrobotdesign.com/blog/web-development/pause-your-html5-canvas-game/#comments</comments>
		<pubDate>Wed, 04 May 2011 02:50:58 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[web development]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://atomicrobotdesign.com/blog/?p=1034</guid>
		<description><![CDATA[A feature that I think most games need is for the player to be able to pause the game. And it’s surprisingly easy to do this with a game built using the canvas tag and JavaScript. If you’ve been following &#8230; <a href="http://atomicrobotdesign.com/blog/web-development/pause-your-html5-canvas-game/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A feature that I think most games need is for the player to be able to pause the game. And it’s surprisingly easy to do this with a game built using the canvas tag and JavaScript. If you’ve been following my blog, you&#8217;ll have seen my six part tutorial series on <a href="http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-1/">building a shooter game</a> with the canvas tag and maybe even built you’re own version. I’m going to add a pause feature to the game in my example, but if you’re using setTimeout in any game, then this will work.</p>
<p>The key to making this work is understanding that what powers the game is the setInterval line of code:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">game <span style="color: #339933;">=</span> setTimeout<span style="color: #009900;">&#40;</span>gameLoop<span style="color: #339933;">,</span> <span style="color: #CC0000;">1000</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Basically, what’s going on here is that we’re running the gameLoop at about thirty frames per second, which is about standard for video games. You can learn more about <a href="https://developer.mozilla.org/en/window.setTimeout">setTimeout</a> at the amazing <a href="https://developer.mozilla.org/">Mozilla Developer Center</a>. In order to pause the game, we need to stop the loop and we can do this using the <a href="https://developer.mozilla.org/en/DOM/window.clearTimeout">clearTimeout</a> function. It does exactly what it says, it clears the timeout that’s running. We just need to tell the browser to listen for something like a key being pressed, I used the p key, and then run clearTimeout. First, you need a function like this:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> keyDown<span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">keyCode</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">80</span><span style="color: #009900;">&#41;</span> pauseGame<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>You can just add the if statement to the keyDown function in the shooter game if you’re just modifying that. This is a pretty simple line of code, if the p key is pressed, then run the pauseGame function. Now we need a pauseGame function:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> pauseGame<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; game <span style="color: #339933;">=</span> clearTimeout<span style="color: #009900;">&#40;</span>game<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>And that will pause the game. But, of course, we’ll have a problem if we leave it like this, there’s no way to start the game up again. All we need to do then, is set the timeout again. We still have another problem, how will the game know if it’s pause or not? We just need to add a Boolean variable, I called mine gamePaused and set it to false at the top of the code. Then I changed the pauseGame function to this:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> pauseGame<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>gamePaused<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; game <span style="color: #339933;">=</span> clearTimeout<span style="color: #009900;">&#40;</span>game<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; gamePaused <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>gamePaused<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; game <span style="color: #339933;">=</span> setTimeout<span style="color: #009900;">&#40;</span>gameLoop<span style="color: #339933;">,</span> <span style="color: #CC0000;">1000</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; gamePaused <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Now when the p key is pressed, the pauseGame function will check to see if gamePaused is true or not, if it’s false, it pauses the game. If it’s true, then it starts the game up, using the same code that’s in the gameLoop function. This is why we have the variable game equal to the setTimeout function, it allows us to reference it elsewhere in the code.</p>
<p>That’s it. Now you can pause any game you make when you use setTimeout to power the game loop. If you’re using setInterval, then you can use <a href="https://developer.mozilla.org/en/DOM/window.clearInterval">clearInterval</a> for the same effect.</p>
]]></content:encoded>
			<wfw:commentRss>http://atomicrobotdesign.com/blog/web-development/pause-your-html5-canvas-game/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An easy way to optimize the HTML5 canvas element’s clearRect function</title>
		<link>http://atomicrobotdesign.com/blog/web-development/an-easy-way-to-optimize-the-html5-canvas-elements-clearrect-function/</link>
		<comments>http://atomicrobotdesign.com/blog/web-development/an-easy-way-to-optimize-the-html5-canvas-elements-clearrect-function/#comments</comments>
		<pubDate>Sat, 30 Apr 2011 21:31:52 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[web development]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://atomicrobotdesign.com/blog/?p=1029</guid>
		<description><![CDATA[One of the main challenges in working with HTML5’s canvas element is really optimizing it because you’re dealing with two unknowns, the processing power of the user’s computer and the speed of their browser’s JavaScript engine. You can never know &#8230; <a href="http://atomicrobotdesign.com/blog/web-development/an-easy-way-to-optimize-the-html5-canvas-elements-clearrect-function/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>One of the main challenges in working with HTML5’s canvas element is really optimizing it because you’re dealing with two unknowns, the processing power of the user’s computer and the speed of their browser’s JavaScript engine. You can never know if they’ll be running an Intel Core i7 or a 6 year old single core. Are they using Chome or Firefox 3.6? One thing we can do quickly and easily, is to use clearRect to only clear the areas we need to instead of the entire canvas. I’ve talked about this before, but I’ve refined how I do it and I think it would be great for developers new to canvas to know this from the start instead of having to discover it on their own.</p>
<p>When I first started working with the canvas element, I would always just use clearRect to clear the entire canvas. That may be necessary in some circumstances, it’s a waste of resources if only a few things are moving around a 500&#215;500 canvas and we’re clearing 250,000 pixels. But if we’re moving 3 blocks around, we don’t want to clear all that and we also don’t want to have to set up individual clearRect functions for each block. The easiest way to do this is to create a function that we can just run for each block.</p>
<p>We would need to pass the function 4 things, the x and y co-ordinates and the width and height of the block we want to clear. So the function would look like this:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> clearCanvas<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span>y<span style="color: #339933;">,</span>w<span style="color: #339933;">,</span>h<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; ctx.<span style="color: #660066;">clearRect</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span>y<span style="color: #339933;">,</span>w<span style="color: #339933;">,</span>h<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>To run the function we would write some code like this:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">clearCanvas<span style="color: #009900;">&#40;</span>block.<span style="color: #660066;">x</span><span style="color: #339933;">,</span>block.<span style="color: #660066;">y</span><span style="color: #339933;">,</span>block.<span style="color: #660066;">w</span><span style="color: #339933;">,</span>block.<span style="color: #660066;">h</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
block.<span style="color: #660066;">x</span> <span style="color: #339933;">+=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span><br />
block.<span style="color: #660066;">y</span> <span style="color: #339933;">+=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span><br />
ctx.<span style="color: #660066;">fillRect</span><span style="color: #009900;">&#40;</span>block.<span style="color: #660066;">x</span><span style="color: #339933;">,</span>block.<span style="color: #660066;">y</span><span style="color: #339933;">,</span>block.<span style="color: #660066;">w</span><span style="color: #339933;">,</span>block.<span style="color: #660066;">h</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>If you’ve worked with canvas before then there’s nothing there that you haven’t seen before, but because we’ve created a function to clear the canvas, we can add another block and just pass it’s info to the clearCanvas function:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">clearCanvas<span style="color: #009900;">&#40;</span>block.<span style="color: #660066;">x</span><span style="color: #339933;">,</span>block.<span style="color: #660066;">y</span><span style="color: #339933;">,</span>block.<span style="color: #660066;">w</span><span style="color: #339933;">,</span>block.<span style="color: #660066;">h</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
block.<span style="color: #660066;">x</span> <span style="color: #339933;">+=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span><br />
block.<span style="color: #660066;">y</span> <span style="color: #339933;">+=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span><br />
ctx.<span style="color: #660066;">fillRect</span><span style="color: #009900;">&#40;</span>block.<span style="color: #660066;">x</span><span style="color: #339933;">,</span>block.<span style="color: #660066;">y</span><span style="color: #339933;">,</span>block.<span style="color: #660066;">w</span><span style="color: #339933;">,</span>block.<span style="color: #660066;">h</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
clearCanvas<span style="color: #009900;">&#40;</span>block2.<span style="color: #660066;">x</span><span style="color: #339933;">,</span>block2.<span style="color: #660066;">y</span><span style="color: #339933;">,</span>block2.<span style="color: #660066;">w</span><span style="color: #339933;">,</span>block2.<span style="color: #660066;">h</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
block2.<span style="color: #660066;">x</span> <span style="color: #339933;">+=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span><br />
ctx.<span style="color: #660066;">fillRect</span><span style="color: #009900;">&#40;</span>block2.<span style="color: #660066;">x</span><span style="color: #339933;">,</span>block2.<span style="color: #660066;">y</span><span style="color: #339933;">,</span>block2.<span style="color: #660066;">w</span><span style="color: #339933;">,</span>block2.<span style="color: #660066;">h</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>The browser only has to worry about clearing those two areas which will allow it to focus more resources on the other code you’re running. This will work with any moving object, whether it’s moving on it’s own or the user is controlling it. I’ve created an example on <a href="http://jsbin.com/ojulu6/3/edit">JS Bin</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://atomicrobotdesign.com/blog/web-development/an-easy-way-to-optimize-the-html5-canvas-elements-clearrect-function/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Build a vertical scrolling shooter game with HTML5 canvas &#8211; Part 6</title>
		<link>http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-6/</link>
		<comments>http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-6/#comments</comments>
		<pubDate>Wed, 13 Apr 2011 02:46:38 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[HTML/CSS]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://atomicrobotdesign.com/blog/?p=1003</guid>
		<description><![CDATA[Part 1 &#8211; Game Basics Part 2 &#8211; Graphics and Lasers Part 3 &#8211; Laser Hit Test Part 4 &#8211; Ship Hit Test and Score Part 5 &#8211; Player Lives and Continue Button Part 6 &#8211; Font fixes and Start &#8230; <a href="http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-6/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div style="width:350px; border:1px solid #333; margin-left:30px; margin-top:10px;">
<ul>
<li><a style="color:black; text-decoration:none;" href="http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-1/">Part 1 &#8211; Game Basics</a></li>
<li><a style="color:black; text-decoration:none;" href="http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-2/">Part 2 &#8211; Graphics and Lasers</a></li>
<li><a style="color:black; text-decoration:none;" href="http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-3/">Part 3 &#8211; Laser Hit Test</a></li>
<li><a style="color:black; text-decoration:none;" href="http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-4/">Part 4 &#8211; Ship Hit Test and Score</a></li>
<li><a style="color:black; text-decoration:none;" href="http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-5/">Part 5 &#8211; Player Lives and Continue Button</a></li>
<li><a style="color:black; text-decoration:none;" href="http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-6/">Part 6 &#8211; Font fixes and Start screen</a></li>
</ul>
</div>
<p>If you have been following this series of tutorials, you‘ll know that we’re almost down building the basics of a game using the HTML5 canvas element and Javascript. We only have a few things to do. The black background is pretty boring, so we’ll add a star field. Arial doesn’t really work with our 8-bit feel, so we’ll use Google web fonts to find something that’s more appropriate. And it’s not really practical for the game to just start when the page is loaded, so we’ll add a start screen with some game play instructions.</p>
<p>First, we’ll add the <a href="http://atomicrobotdesign.com/blog_media/canvas_game/Part_6/starfield.jpg">star field background</a>. To do this we’re going to use the exact same technique that we use for the enemy ships, it’ll just be slower and we don’t have to worry about anything affecting it. For the star field we’ll use an image and it will just scroll downwards continuously in the background. So, add this this to the variables at the top:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">starfield<span style="color: #339933;">,</span> starX <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> starY <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> starY2 <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">600</span></div></td></tr></tbody></table></div>
<p>The starfield  variable will hold our image and starX is our x position. And the reason we have starY and starY2 is because we’re going to make two instances of the star field and have it repeat over and over. We could dynamically generate all the stars in the background, but that would require a fair amount of processing power and I don’t think it’s worth it. We load the star field image the same way as the other two images, so add this to the init function:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">starfield <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Image<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
starfield.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'starfield.jpg'</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Next we need a function that will actually create the movement of the stars:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> drawStarfield<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; ctx.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>starfield<span style="color: #339933;">,</span>starX<span style="color: #339933;">,</span>starY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; ctx.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>starfield<span style="color: #339933;">,</span>starX<span style="color: #339933;">,</span>starY2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>starY <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">600</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; starY <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">599</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>starY2 <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">600</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; starY2 <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">599</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; starY <span style="color: #339933;">+=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span><br />
&nbsp; starY2 <span style="color: #339933;">+=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Pretty simple here, we’re drawing two instances of the star field, one is positioned on the y axis at 0 and the other one is positioned at -600. We then move both images downward and once the bottom image clears the bottom of the canvas, we then reposition it at the top of the canvas and it moves down again. With this cycling, it looks like the ship is slowly moving forward all the time. We need to just call the drawStarfield function, so add it to our gameLoop function but outside of the if statement so that the star field is moving even if they haven’t started to play yet, after we’ve added the start screen. It should look like this now:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> gameLoop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; clearCanvas<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; drawStarfield<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>alive <span style="color: #339933;">&amp;&amp;</span> lives <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; hitTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; shipCollision<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; moveLaser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; moveEnemies<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; drawEnemies<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; drawShip<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; drawLaser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; scoreTotal<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; game <span style="color: #339933;">=</span> setTimeout<span style="color: #009900;">&#40;</span>gameLoop<span style="color: #339933;">,</span> <span style="color: #CC0000;">1000</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Now, let’s give the game a more appropriate font. Lucky for us, <a href="http://www.google.com/webfonts/family?family=VT323&#038;subset=latin">Google’s web fonts</a> has exactly what we’re looking for, a font called VT323. If you’ve never used a Google web font before, it’s really easy, even if you want to use it with the canvas element. The very first thing we need to do is at this at the top of the web page, right after the title tag:</p>
<div class="codecolorer-container html4strict twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/link.html"><span style="color: #000000; font-weight: bold;">link</span></a> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'http://fonts.googleapis.com/css?family=VT323'</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'stylesheet'</span> <span style="color: #66cc66;">/</span>&gt;</span></div></td></tr></tbody></table></div>
<p>Now we have access the VT323 font, as long as we have Internet access. And, in the scoreTotal function, we change this:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ctx.<span style="color: #660066;">font</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'bold 18px Arial'</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>to this:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ctx.<span style="color: #660066;">font</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'bold 20px VT323'</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>If you test the web page now, the font will look more 8-bit and fit a lot better with the game’s graphics style. We’re going to change one other thing, the position of our score text, because, unlike with Flash, we can’t just set it to right align so that it will move when the school gets too large for it’s space. So change this:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Score: '</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">490</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span>score<span style="color: #339933;">,</span> <span style="color: #CC0000;">550</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>to this:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Score: '</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">55</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span>score<span style="color: #339933;">,</span> <span style="color: #CC0000;">70</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">55</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>This will position the score right below the text and then we don’t have to worry about space, no matter how high the score gets.</p>
<p>Our final step will be set up a start screen so that our game doesn’t just automatically play when the page is loaded. The first thing we need is a new variable, so add this at the top with the other variables:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">gameStarted <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span></div></td></tr></tbody></table></div>
<p>Our game hasn’t started yet so, of course, it’s going to be false. Next we need to set up the way for the player to start the game, so we’re going to add an event listener so that if they click anywhere on the canvas, the game will start. Add this to the init function with the other two event listeners:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">canvas.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> gameStart<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>When the player clicks the canvas, a function called gameStart will run, so let’s create that:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> gameStart<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; gameStarted <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; canvas.<span style="color: #660066;">removeEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> gameStart<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Wow, not much going on there, we’re just changing the gameStarted variable to true and removing the event listener. If you run the game now, you’ll see that absolutely nothing has changed. We need to make a few more changes before this will actually do anything. Our first change is to change this line in the gameLoop function:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>alive <span style="color: #339933;">&amp;&amp;</span> lives <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span></div></td></tr></tbody></table></div>
<p>to this:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>alive <span style="color: #339933;">&amp;&amp;</span> gameStarted <span style="color: #339933;">&amp;&amp;</span> lives <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span></div></td></tr></tbody></table></div>
<p>So now alive and gameStarted need to be true and lives needs to be greater than zero for the game to run. Now the rest of our changes are going to be in the scoreTotal function because it’s easier if we keep all the text in the same place in case changes need to be made. Now, add this to it:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>gameStarted<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; ctx.<span style="color: #660066;">font</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'bold 50px VT323'</span><span style="color: #339933;">;</span><br />
&nbsp; ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Canvas Shooter'</span><span style="color: #339933;">,</span> width <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">150</span><span style="color: #339933;">,</span> height <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; ctx.<span style="color: #660066;">font</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'bold 20px VT323'</span><span style="color: #339933;">;</span><br />
&nbsp; ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Click to Play'</span><span style="color: #339933;">,</span> width <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">56</span><span style="color: #339933;">,</span> height <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Use arrow keys to move'</span><span style="color: #339933;">,</span> width <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> height <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Use the x key to shoot'</span><span style="color: #339933;">,</span> width <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> height <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">90</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>With this if statement, we are checking to see if the game has started yet, and if it hasn’t then display this text, which includes instructions on how to play. When you click on the canvas, the game starts and all this text goes away. Because we changed the font, it’s moved the positioning of our continue text a bit, so update it to this:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Game Over!'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">245</span><span style="color: #339933;">,</span> height <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
ctx.<span style="color: #660066;">fillRect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>width <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">60</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span>height <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span><span style="color: #CC0000;">100</span><span style="color: #339933;">,</span><span style="color: #CC0000;">40</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
ctx.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#000'</span><span style="color: #339933;">;</span><br />
ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Continue?'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">250</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span>height <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">35</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
canvas.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> continueButton<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Alright, now if you test the game, there will be a start screen and you need to click on the canvas to start the game. You can check out the demo <a href="http://atomicrobotdesign.com/blog_media/canvas_game/Part_6/part_6.html">here</a>.</p>
<p>And that’s it. We’ve created a basic shooter game using the canvas element and it really wasn’t the difficult. The canvas  API is still pretty young, but I’m sure as it matures and as more and more libraries and frameworks are created, building things with canvas will be easier and easier. I’m done with this as an actual series of tutorials now, but I think I might come back to it once and a while to use as an example for other features that we might want to add to games.</p>
<p>Here’s the complete code:</p>
<div class="codecolorer-container html4strict twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br />118<br />119<br />120<br />121<br />122<br />123<br />124<br />125<br />126<br />127<br />128<br />129<br />130<br />131<br />132<br />133<br />134<br />135<br />136<br />137<br />138<br />139<br />140<br />141<br />142<br />143<br />144<br />145<br />146<br />147<br />148<br />149<br />150<br />151<br />152<br />153<br />154<br />155<br />156<br />157<br />158<br />159<br />160<br />161<br />162<br />163<br />164<br />165<br />166<br />167<br />168<br />169<br />170<br />171<br />172<br />173<br />174<br />175<br />176<br />177<br />178<br />179<br />180<br />181<br />182<br />183<br />184<br />185<br />186<br />187<br />188<br />189<br />190<br />191<br />192<br />193<br />194<br />195<br />196<br />197<br />198<br />199<br />200<br />201<br />202<br />203<br />204<br />205<br />206<br />207<br />208<br />209<br />210<br />211<br />212<br />213<br />214<br />215<br />216<br />217<br />218<br />219<br />220<br />221<br />222<br />223<br />224<br />225<br />226<br />227<br />228<br />229<br />230<br />231<br />232<br />233<br />234<br />235<br />236<br />237<br />238<br />239<br />240<br />241<br />242<br />243<br />244<br />245<br />246<br />247<br />248<br />249<br />250<br />251<br />252<br />253<br />254<br />255<br />256<br />257<br />258<br />259<br />260<br />261<br />262<br />263<br />264<br />265<br />266<br />267<br />268<br />269<br />270<br />271<br />272<br />273<br />274<br />275<br />276<br />277<br />278<br />279<br />280<br />281<br />282<br />283<br />284<br />285<br />286<br />287<br />288<br />289<br />290<br />291<br />292<br />293<br />294<br />295<br />296<br />297<br />298<br />299<br />300<br />301<br />302<br />303<br />304<br />305<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #00bbdd;">&lt;!DOCTYPE html&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">html</span></a> <span style="color: #000066;">lang</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;en&quot;</span>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/meta.html"><span style="color: #000000; font-weight: bold;">meta</span></a> <span style="color: #000066;">charset</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;utf-8&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/title.html"><span style="color: #000000; font-weight: bold;">title</span></a>&gt;</span>HTML5 Canvas Game Part 6 || Atomic Robot Design<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/title.html"><span style="color: #000000; font-weight: bold;">title</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/link.html"><span style="color: #000000; font-weight: bold;">link</span></a> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'http://fonts.googleapis.com/css?family=VT323'</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'stylesheet'</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/style.html"><span style="color: #000000; font-weight: bold;">style</span></a>&gt;</span><br />
body {<br />
&nbsp; padding:0;<br />
&nbsp; margin:0;<br />
&nbsp; background:#666;<br />
}<br />
canvas {<br />
&nbsp; display:block;<br />
&nbsp; margin:30px auto 0;<br />
&nbsp; border:1px dashed #ccc;<br />
&nbsp; background:#000;<br />
}<br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/style.html"><span style="color: #000000; font-weight: bold;">style</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span><br />
var canvas,<br />
&nbsp; &nbsp; ctx,<br />
&nbsp; &nbsp; width = 600,<br />
&nbsp; &nbsp; height = 600,<br />
&nbsp; &nbsp; enemyTotal = 5,<br />
&nbsp; &nbsp; enemies = [],<br />
&nbsp; &nbsp; enemy_x = 50,<br />
&nbsp; &nbsp; enemy_y = -45,<br />
&nbsp; &nbsp; enemy_w = 50,<br />
&nbsp; &nbsp; enemy_h = 38,<br />
&nbsp; &nbsp; speed = 3,<br />
&nbsp; &nbsp; enemy,<br />
&nbsp; &nbsp; rightKey = false,<br />
&nbsp; &nbsp; leftKey = false,<br />
&nbsp; &nbsp; upKey = false,<br />
&nbsp; &nbsp; downKey = false,<br />
&nbsp; &nbsp; ship,<br />
&nbsp; &nbsp; ship_x = (width / 2) - 25, ship_y = height - 75, ship_w = 50, ship_h = 57,<br />
&nbsp; &nbsp; laserTotal = 2,<br />
&nbsp; &nbsp; lasers = [],<br />
&nbsp; &nbsp; score = 0,<br />
&nbsp; &nbsp; alive = true,<br />
&nbsp; &nbsp; lives = 3,<br />
&nbsp; &nbsp; starfield,<br />
&nbsp; &nbsp; starX = 0, starY = 0, starY2 = -600,<br />
&nbsp; &nbsp; gameStarted = false;<br />
<br />
//Array to hold all the enemies on screen<br />
for (var i = 0; i <span style="color: #009900;">&lt; enemyTotal; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp;enemies.push<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>enemy_x, enemy_y, enemy_w, enemy_h, speed<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;"> &nbsp;enemy_x +<span style="color: #66cc66;">=</span> enemy_w + <span style="color: #cc66cc;">60</span>;</span><br />
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span><br />
<br />
<span style="color: #009900;"><span style="color: #66cc66;">//</span>Clears the canvas so it can be updated</span><br />
<span style="color: #009900;">function clearCanvas<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp;ctx.clearRect<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #000066;">width</span>,<span style="color: #000066;">height</span><span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span><br />
<br />
<span style="color: #009900;"><span style="color: #66cc66;">//</span>Cycles through the array and draws the updated enemy position</span><br />
<span style="color: #009900;">function drawEnemies<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp;<span style="color: #000066;">for</span> <span style="color: #66cc66;">&#40;</span>var i <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>; i &lt; enemies.length; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;ctx.drawImage<span style="color: #66cc66;">&#40;</span>enemy, enemies<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>, enemies<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;"> &nbsp;<span style="color: #66cc66;">&#125;</span></span><br />
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span><br />
<br />
<span style="color: #009900;"><span style="color: #66cc66;">//</span>If an arrow key is being pressed, moves the ship in the right direction</span><br />
<span style="color: #009900;">function drawShip<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp;if <span style="color: #66cc66;">&#40;</span>rightKey<span style="color: #66cc66;">&#41;</span> ship_x +<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">5</span>;</span><br />
<span style="color: #009900;"> &nbsp;else if <span style="color: #66cc66;">&#40;</span>leftKey<span style="color: #66cc66;">&#41;</span> ship_x -<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">5</span>;</span><br />
<span style="color: #009900;"> &nbsp;if <span style="color: #66cc66;">&#40;</span>upKey<span style="color: #66cc66;">&#41;</span> ship_y -<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">5</span>;</span><br />
<span style="color: #009900;"> &nbsp;else if <span style="color: #66cc66;">&#40;</span>downKey<span style="color: #66cc66;">&#41;</span> ship_y +<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">5</span>;</span><br />
<span style="color: #009900;"> &nbsp;if <span style="color: #66cc66;">&#40;</span>ship_x &lt;<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> ship_x <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>;</span><br />
<span style="color: #009900;"> &nbsp;if <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>ship_x + ship_w<span style="color: #66cc66;">&#41;</span> &gt;</span>= width) ship_x = width - ship_w;<br />
&nbsp; if (ship_y <span style="color: #009900;">&lt;<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> ship_y <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>;</span><br />
<span style="color: #009900;"> &nbsp;if <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>ship_y + ship_h<span style="color: #66cc66;">&#41;</span> &gt;</span>= height) ship_y = height - ship_h;<br />
&nbsp; ctx.drawImage(ship, ship_x, ship_y);<br />
}<br />
<br />
//This moves the enemies downwards on the canvas and if one passes the bottom of the canvas, it moves it to above the canvas<br />
function moveEnemies() {<br />
&nbsp; for (var i = 0; i <span style="color: #009900;">&lt; enemies.length; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;if <span style="color: #66cc66;">&#40;</span>enemies<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> &lt; <span style="color: #000066;">height</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp;enemies<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> +<span style="color: #66cc66;">=</span> enemies<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#93;</span>;</span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #66cc66;">&#125;</span> else if <span style="color: #66cc66;">&#40;</span>enemies<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> &gt;</span> height - 1) {<br />
&nbsp; &nbsp; &nbsp; enemies[i][1] = -45;<br />
&nbsp; &nbsp; }<br />
&nbsp; }<br />
}<br />
<br />
//If there are lasers in the lasers array, then this will draw them on the canvas<br />
function drawLaser() {<br />
&nbsp; if (lasers.length)<br />
&nbsp; &nbsp; for (var i = 0; i <span style="color: #009900;">&lt; lasers.length; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp;ctx.fillStyle <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'#f00'</span>;</span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp;ctx.fillRect<span style="color: #66cc66;">&#40;</span>lasers<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>,lasers<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>,lasers<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span>,lasers<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #66cc66;">&#125;</span></span><br />
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span><br />
<br />
<span style="color: #009900;"><span style="color: #66cc66;">//</span>If we<span style="color: #ff0000;">'re drawing lasers on the canvas, this moves them up the canvas</span><br />
<span style="color: #009900;">function moveLaser() {</span><br />
<span style="color: #009900;"> &nbsp;for (var i = 0; i &lt; lasers.length; i++) {</span><br />
<span style="color: #009900;"> &nbsp; &nbsp;if (lasers[i][1] &gt;</span></span> -11) {<br />
&nbsp; &nbsp; &nbsp; lasers[i][1] -= 10;<br />
&nbsp; &nbsp; } else if (lasers[i][1] <span style="color: #009900;">&lt; -<span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp;lasers.splice<span style="color: #66cc66;">&#40;</span>i, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #66cc66;">&#125;</span></span><br />
<span style="color: #009900;"> &nbsp;<span style="color: #66cc66;">&#125;</span></span><br />
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span><br />
<br />
<span style="color: #009900;"><span style="color: #66cc66;">//</span>Runs a couple of loops to see if any of the lasers have hit any of the enemies</span><br />
<span style="color: #009900;">function hitTest<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp;var remove <span style="color: #66cc66;">=</span> false;</span><br />
<span style="color: #009900;"> &nbsp;<span style="color: #000066;">for</span> <span style="color: #66cc66;">&#40;</span>var i <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>; i &lt; lasers.length; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">for</span> <span style="color: #66cc66;">&#40;</span>var j <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>; j &lt; enemies.length; j++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp;if <span style="color: #66cc66;">&#40;</span>lasers<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> &lt;<span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>enemies<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> + enemies<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> &amp;&amp; lasers<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> &gt;</span>= enemies[j][0] <span style="color: #ddbb00;">&amp;&amp; lasers[i][0] &lt;= (enemies[j][0] + enemies[j][2])) {</span><br />
<span style="color: #ddbb00;"> &nbsp; &nbsp; &nbsp; &nbsp;remove = true;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; enemies.splice(j, 1);<br />
&nbsp; &nbsp; &nbsp; &nbsp; score += 10;<br />
&nbsp; &nbsp; &nbsp; &nbsp; enemies.push([(Math.random() * 500) + 50, -45, enemy_w, enemy_h, speed]);<br />
&nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; if (remove == true) {<br />
&nbsp; &nbsp; &nbsp; lasers.splice(i, 1);<br />
&nbsp; &nbsp; &nbsp; remove = false;<br />
&nbsp; &nbsp; }<br />
&nbsp; }<br />
}<br />
<br />
//Similar to the laser hit test, this function checks to see if the player's ship collides with any of the enemies<br />
function shipCollision() {<br />
&nbsp; var ship_xw = ship_x + ship_w,<br />
&nbsp; &nbsp; &nbsp; ship_yh = ship_y + ship_h;<br />
&nbsp; for (var i = 0; i <span style="color: #009900;">&lt; enemies.length; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;if <span style="color: #66cc66;">&#40;</span>ship_x &gt;</span> enemies[i][0] <span style="color: #ddbb00;">&amp;&amp; ship_x &lt; enemies[i][0] + enemy_w &amp;&amp; ship_y &gt; enemies[i][1] &amp;&amp; ship_y &lt; enemies[i][1] + enemy_h) {</span><br />
<span style="color: #ddbb00;"> &nbsp; &nbsp; &nbsp;checkLives();</span><br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; if (ship_xw <span style="color: #009900;">&lt; enemies<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> + enemy_w &amp;&amp; ship_xw &gt;</span> enemies[i][0] <span style="color: #ddbb00;">&amp;&amp; ship_y &gt; enemies[i][1] &amp;&amp; ship_y &lt; enemies[i][1] + enemy_h) {</span><br />
<span style="color: #ddbb00;"> &nbsp; &nbsp; &nbsp;checkLives();</span><br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; if (ship_yh &gt; enemies[i][1] <span style="color: #ddbb00;">&amp;&amp; ship_yh &lt; enemies[i][1] + enemy_h &amp;&amp; ship_x &gt; enemies[i][0] &amp;&amp; ship_x &lt; enemies[i][0] + enemy_w) {</span><br />
<span style="color: #ddbb00;"> &nbsp; &nbsp; &nbsp;checkLives();</span><br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; if (ship_yh &gt; enemies[i][1] <span style="color: #ddbb00;">&amp;&amp; ship_yh &lt; enemies[i][1] + enemy_h &amp;&amp; ship_xw &lt; enemies[i][0] + enemy_w &amp;&amp; ship_xw &gt; enemies[i][0]) {</span><br />
<span style="color: #ddbb00;"> &nbsp; &nbsp; &nbsp;checkLives();</span><br />
&nbsp; &nbsp; }<br />
&nbsp; }<br />
}<br />
<br />
//This function runs whenever the player's ship hits an enemy and either subtracts a life or sets the alive variable to false if the player runs out of lives<br />
function checkLives() {<br />
&nbsp; lives -= 1;<br />
&nbsp; if (lives &gt; 0) {<br />
&nbsp; &nbsp; reset();<br />
&nbsp; } else if (lives == 0) {<br />
&nbsp; &nbsp; alive = false;<br />
&nbsp; }<br />
}<br />
<br />
//This simply resets the ship and enemies to their starting positions<br />
function reset() {<br />
&nbsp; var enemy_reset_x = 50;<br />
&nbsp; ship_x = (width / 2) - 25, ship_y = height - 75, ship_w = 50, ship_h = 57;<br />
&nbsp; for (var i = 0; i <span style="color: #009900;">&lt; enemies.length; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;enemies<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> enemy_reset_x;</span><br />
<span style="color: #009900;"> &nbsp; &nbsp;enemies<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> -<span style="color: #cc66cc;">45</span>;</span><br />
<span style="color: #009900;"> &nbsp; &nbsp;enemy_reset_x <span style="color: #66cc66;">=</span> enemy_reset_x + enemy_w + <span style="color: #cc66cc;">60</span>;</span><br />
<span style="color: #009900;"> &nbsp;<span style="color: #66cc66;">&#125;</span></span><br />
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span><br />
<br />
<span style="color: #009900;"><span style="color: #66cc66;">//</span>After the player loses all their lives, the continue button is shown and if clicked, it resets the game and removes the event listener <span style="color: #000066;">for</span> the continue button</span><br />
<span style="color: #009900;">function continueButton<span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp;var cursorPos <span style="color: #66cc66;">=</span> getCursorPos<span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;"> &nbsp;if <span style="color: #66cc66;">&#40;</span>cursorPos.x &gt;</span> (width / 2) - 53 <span style="color: #ddbb00;">&amp;&amp; cursorPos.x &lt; (width / 2) + 47 &amp;&amp; cursorPos.y &gt; (height / 2) + 10 &amp;&amp; cursorPos.y &lt; (height / 2) + 50) {</span><br />
<span style="color: #ddbb00;"> &nbsp; &nbsp;alive = true;</span><br />
&nbsp; &nbsp; lives = 3;<br />
&nbsp; &nbsp; reset();<br />
&nbsp; &nbsp; canvas.removeEventListener('click', continueButton, false);<br />
&nbsp; }<br />
}<br />
<br />
//holds the cursors position<br />
function cursorPosition(x,y) {<br />
&nbsp; this.x = x;<br />
&nbsp; this.y = y;<br />
}<br />
<br />
//finds the cursor's position after the mouse is clicked<br />
function getCursorPos(e) {<br />
&nbsp; var x;<br />
&nbsp; var y;<br />
&nbsp; if (e.pageX || e.pageY) {<br />
&nbsp; &nbsp; x = e.pageX;<br />
&nbsp; &nbsp; y = e.pageY;<br />
&nbsp; } else {<br />
&nbsp; &nbsp; x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;<br />
&nbsp; &nbsp; y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;<br />
&nbsp; }<br />
&nbsp; x -= canvas.offsetLeft;<br />
&nbsp; y -= canvas.offsetTop;<br />
&nbsp; var cursorPos = new cursorPosition(x, y);<br />
&nbsp; return cursorPos;<br />
}<br />
<br />
//Draws the text for the score and lives on the canvas and if the player runs out of lives, it's draws the game over text and continue button as well as adding the event listener for the continue button<br />
function scoreTotal() {<br />
&nbsp; ctx.font = 'bold 20px VT323';<br />
&nbsp; ctx.fillStyle = '#fff';<br />
&nbsp; ctx.fillText('Score: ', 10, 55);<br />
&nbsp; ctx.fillText(score, 70, 55);<br />
&nbsp; ctx.fillText('Lives:', 10, 30);<br />
&nbsp; ctx.fillText(lives, 68, 30);<br />
&nbsp; &nbsp; &nbsp; &nbsp; if (!gameStarted) {<br />
&nbsp; &nbsp; ctx.font = 'bold 50px VT323';<br />
&nbsp; &nbsp; ctx.fillText('Canvas Shooter', width / 2 - 150, height / 2);<br />
&nbsp; &nbsp; ctx.font = 'bold 20px VT323';<br />
&nbsp; &nbsp; ctx.fillText('Click to Play', width / 2 - 56, height / 2 + 30);<br />
&nbsp; &nbsp; ctx.fillText('Use arrow keys to move', width / 2 - 100, height / 2 + 60);<br />
&nbsp; &nbsp; ctx.fillText('Use the x key to shoot', width / 2 - 100, height / 2 + 90);<br />
&nbsp; }<br />
&nbsp; if (!alive) {<br />
&nbsp; &nbsp; ctx.fillText('Game Over!', 245, height / 2);<br />
&nbsp; &nbsp; ctx.fillRect((width / 2) - 60, (height / 2) + 10,100,40);<br />
&nbsp; &nbsp; ctx.fillStyle = '#000';<br />
&nbsp; &nbsp; ctx.fillText('Continue?', 250, (height / 2) + 35);<br />
&nbsp; &nbsp; canvas.addEventListener('click', continueButton, false);<br />
&nbsp; }<br />
}<br />
<br />
//Draws and animates the background starfield<br />
function drawStarfield() {<br />
&nbsp; ctx.drawImage(starfield,starX,starY);<br />
&nbsp; ctx.drawImage(starfield,starX,starY2);<br />
&nbsp; if (starY &gt; 600) {<br />
&nbsp; &nbsp; starY = -599;<br />
&nbsp; }<br />
&nbsp; if (starY2 &gt; 600) {<br />
&nbsp; &nbsp; starY2 = -599;<br />
&nbsp; }<br />
&nbsp; starY += 1;<br />
&nbsp; starY2 += 1;<br />
}<br />
<br />
//The initial function called when the page first loads. Loads the ship, enemy and starfield images and adds the event listeners for the arrow keys. It then calls the gameLoop function.<br />
function init() {<br />
&nbsp; canvas = document.getElementById('canvas');<br />
&nbsp; ctx = canvas.getContext('2d');<br />
&nbsp; enemy = new Image();<br />
&nbsp; enemy.src = '8bit_enemy.png';<br />
&nbsp; ship = new Image();<br />
&nbsp; ship.src = 'ship.png';<br />
&nbsp; starfield = new Image();<br />
&nbsp; starfield.src = 'starfield.jpg';<br />
&nbsp; document.addEventListener('keydown', keyDown, false);<br />
&nbsp; document.addEventListener('keyup', keyUp, false);<br />
&nbsp; &nbsp; &nbsp; &nbsp; canvas.addEventListener('click', gameStart, false);<br />
&nbsp; gameLoop();<br />
}<br />
<br />
function gameStart() {<br />
&nbsp; gameStarted = true;<br />
&nbsp; canvas.removeEventListener('click', gameStart, false);<br />
}<br />
<br />
//The main function of the game, it calls all the other functions needed to make the game run<br />
function gameLoop() {<br />
&nbsp; clearCanvas();<br />
&nbsp; drawStarfield()<br />
&nbsp; if (alive <span style="color: #ddbb00;">&amp;&amp; gameStarted &amp;&amp; lives &gt; 0) {</span><br />
<span style="color: #ddbb00;"> &nbsp; &nbsp;hitTest();</span><br />
&nbsp; &nbsp; shipCollision();<br />
&nbsp; &nbsp; moveLaser();<br />
&nbsp; &nbsp; moveEnemies();<br />
&nbsp; &nbsp; drawEnemies();<br />
&nbsp; &nbsp; drawShip();<br />
&nbsp; &nbsp; drawLaser();<br />
&nbsp; }<br />
&nbsp; scoreTotal();<br />
&nbsp; game = setTimeout(gameLoop, 1000 / 30);<br />
}<br />
<br />
//Checks to see which key has been pressed and either to move the ship or fire a laser<br />
function keyDown(e) {<br />
&nbsp; if (e.keyCode == 39) rightKey = true;<br />
&nbsp; else if (e.keyCode == 37) leftKey = true;<br />
&nbsp; if (e.keyCode == 38) upKey = true;<br />
&nbsp; else if (e.keyCode == 40) downKey = true;<br />
&nbsp; if (e.keyCode == 88 <span style="color: #ddbb00;">&amp;&amp; lasers.length &lt;= laserTotal) lasers.push([ship_x + 25, ship_y - 20, 4, 20]);</span><br />
}<br />
<br />
//Checks to see if a pressed key has been released and stops the ships movement if it has<br />
function keyUp(e) {<br />
&nbsp; if (e.keyCode == 39) rightKey = false;<br />
&nbsp; else if (e.keyCode == 37) leftKey = false;<br />
&nbsp; if (e.keyCode == 38) upKey = false;<br />
&nbsp; else if (e.keyCode == 40) downKey = false;<br />
}<br />
<br />
window.onload = init;<br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span><br />
<br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;canvas <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;canvas&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;600&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;600&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>canvas&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">html</span></a>&gt;</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-6/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>It might be too early to build client work with canvas but that doesn’t mean it’s too early to learn</title>
		<link>http://atomicrobotdesign.com/blog/web-development/it-might-be-too-early-to-build-client-work-with-canvas-but-that-doesnt-mean-its-too-early-to-learn/</link>
		<comments>http://atomicrobotdesign.com/blog/web-development/it-might-be-too-early-to-build-client-work-with-canvas-but-that-doesnt-mean-its-too-early-to-learn/#comments</comments>
		<pubDate>Thu, 07 Apr 2011 04:34:10 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[web development]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://atomicrobotdesign.com/blog/?p=996</guid>
		<description><![CDATA[I’ve been asked a couple of times since HTML5’s canvas element isn’t that widely adopted yet, since versions of Internet Explorer that don’t support it still account for more than 50% of browser market share, why I spend so much &#8230; <a href="http://atomicrobotdesign.com/blog/web-development/it-might-be-too-early-to-build-client-work-with-canvas-but-that-doesnt-mean-its-too-early-to-learn/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I’ve been asked a couple of times since HTML5’s canvas element isn’t that widely adopted yet, since versions of Internet Explorer that don’t support it still account for more than<a href="http://en.wikipedia.org/wiki/Internet_Explorer#Market_adoption_and_usage_share"> 50% of browser market share</a>, why I spend so much time working with it. I think there’s a few good reasons for developers to experimenting with canvas now.</p>
<p>When I first got into web development, I became a big fan of Flash. I spent a lot of time learning ActionScript. I would check out every Flash site on <a href="http://www.thefwa.com">thefwa.com</a> looking for inspiration about what I should learn next. But, as I learned more and more about Flash development, I realized that most of these Flash sites and applications were built by teams. I had missed the days of the solo Flash developer making kickass stuff. Flash has moved on into more complicated territory and if I wanted to do any cutting edge Flash work, I would have to join a big company. </p>
<p>With the canvas element, and I’ve seen some Flash developers say this mockingly, but it’s true, it’s just like the early days of Flash. We don’t really know fully what we can do with canvas yet. Every week I see developers pushing what canvas can do and it’s usually just a developer messing around on their own time. This is something that’s attracted me to working with canvas, unlike with Flash, I can’t just Google how to do something, I have to figure it out on my own and I like that.</p>
<p>Flash has been evolving into something far beyond what I think the original programmers could have imagined. We’re at the point now where Flash is going to be used to make<a href="http://www.youtube.com/watch?v=KcKvS983K8c"> Playstation 2 quality games</a>. Now, that’s actually pretty amazing, being able to play a game like that in the browsers, even if you need a plug-in. But if I want to build video games that complex, then I would probably have got into actually building video games. But if you still want to build simple video games that still might impress someone, canvas is probably the way to go.</p>
<p>It’s going to be a while before canvas is widespread. The reason? IE 9 doesn’t work on Windows XP and the truth is, most people that use it aren’t going to be switching from IE 8 to Firefox or Chrome. And IE 9 isn’t going to really take off until XP dies out and Windows 7 takes over. And, realistically, Windows 7 isn’t going to really take off until corporations start installing it on their computers.</p>
<p>So, if we have to wait for IE 9 to replace all the other versions, then what’s the point of working with canvas now? Well, eventually, that is going to happen and when it does, I think it would be better to be able to say to clients, “Yes, I can build that for you” as opposed to “I’ll have to learn how to do it.” I think if you want to work with canvas in the future, it’s smart to start working with it now because I’m pretty sure that the canvas API is going to evolve and be more mature. I would rather start learning the basics now so that when more features are added, I’ll be able to just add it to what I already know instead having to start at the beginning when canvas becomes more widespread.</p>
<p>Right now, I think when clients say that they want something built in canvas, I think they really mean they want it to work on the iPad. So even now there will be opportunity to do client work with canvas, but I think that might be few and far between. I’m sure there will be more opportunities in the future and some of us just might luck out and get a cutting edge client that doesn’t mind if what they want doesn’t work in some browsers.</p>
<p>Really though, I think I’ve found working with the canvas tag to be a great way to learn JavaScript. And if you have any experience with Flash and ActionScript, building similar things with canvas isn’t as different as you’d think. And you can say you’re all about HTML5 now and that will get you some instant cred with the cool kids.</p>
]]></content:encoded>
			<wfw:commentRss>http://atomicrobotdesign.com/blog/web-development/it-might-be-too-early-to-build-client-work-with-canvas-but-that-doesnt-mean-its-too-early-to-learn/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HTML5 Canvas: You don’t always have to clear the entire thing</title>
		<link>http://atomicrobotdesign.com/blog/web-development/html5-canvas-you-dont-always-have-to-clear-the-entire-thing/</link>
		<comments>http://atomicrobotdesign.com/blog/web-development/html5-canvas-you-dont-always-have-to-clear-the-entire-thing/#comments</comments>
		<pubDate>Mon, 04 Apr 2011 02:50:38 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[web development]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://atomicrobotdesign.com/blog/?p=991</guid>
		<description><![CDATA[When I first started working with the HTML5 canvas element, the one thing I found a bit frustrating was the fact that you had to clear the canvas and then redraw it. Coming from Flash, I wasn’t used to this &#8230; <a href="http://atomicrobotdesign.com/blog/web-development/html5-canvas-you-dont-always-have-to-clear-the-entire-thing/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>When I first started working with the HTML5 canvas element, the one thing I found a bit frustrating was the fact that you had to clear the canvas and then redraw it. Coming from Flash, I wasn’t used to this because it’s one of the many things Flash does for you. With canvas, if you’re doing something like moving a space ship around in a game and you don’t clear it before you draw the ship’s new position, eventually you’ll just end up with canvas full of ships. So, we call clearRect and clear the canvas before we draw the ship’s new position and then it looks like the ship is moving around, when it’s really just being redrawn in it’s new position.</p>
<p>But the thing I didn’t realize at first, and I’m sure many other newbies at working with canvas don’t realize either, but you don’t have to clear the entire canvas, you can just simulate what Flash does and just redraw the area that’s changed. Why would you want to do this? Well, think about a graphically heavy game but there’s only one or two objects moving around, you could cut down on CPU use if you just redraw the areas affected by the moving objects.</p>
<p>This is actually a lot simpler than you would think. Instead of something like this:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ctx.<span style="color: #660066;">clearRect</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>canvasWidth<span style="color: #339933;">,</span>canvasHeight<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>we can do something like this:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ctx.<span style="color: #660066;">clearRect</span><span style="color: #009900;">&#40;</span>square.<span style="color: #660066;">x</span><span style="color: #339933;">,</span>square.<span style="color: #660066;">y</span><span style="color: #339933;">,</span>square.<span style="color: #660066;">w</span><span style="color: #339933;">,</span>square.<span style="color: #660066;">h</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>In the first example, we’re clearing the entire canvas, in the second, we’re just clearing the area of a square. So now we’re just redrawing a 20 by 20 pixel area instead of say, a 500 by 500 pixel area, a lot less work for our broswer and CPU!</p>
<p>Plus, we can call clearRect more than once. So, here’s the code for an example that has two moving squares, one we can control with the arrow keys and one that moves down on it’s own. And instead of clearing the entire canvas, we just clear two square sized areas.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;!</span>DOCTYPE html<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>html lang<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;en&quot;</span><span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>meta charset<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;utf-8&quot;</span> <span style="color: #339933;">/&gt;</span><br />
<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>ClearRect Test<span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>style<span style="color: #339933;">&gt;</span><br />
body <span style="color: #009900;">&#123;</span><br />
&nbsp; margin<span style="color: #339933;">:</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span><br />
&nbsp; padding<span style="color: #339933;">:</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span><br />
&nbsp; background<span style="color: #339933;">:</span>#000<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
canvas <span style="color: #009900;">&#123;</span><br />
&nbsp; display<span style="color: #339933;">:</span>block<span style="color: #339933;">;</span><br />
&nbsp; margin<span style="color: #339933;">:</span>20px auto<span style="color: #339933;">;</span><br />
&nbsp; border<span style="color: #339933;">:</span>1px dashed #0f0<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #339933;">&lt;/</span>style<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> canvas<span style="color: #339933;">,</span> ctx<span style="color: #339933;">,</span> loop<span style="color: #339933;">,</span> square<span style="color: #339933;">,</span> square2<span style="color: #339933;">,</span> rightKey <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> leftKey <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> upKey <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> downKey <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; canvas <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'canvas'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; ctx <span style="color: #339933;">=</span> canvas.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'2d'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; square <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>x<span style="color: #339933;">:</span><span style="color: #CC0000;">10</span><span style="color: #339933;">,</span>y<span style="color: #339933;">:</span><span style="color: #CC0000;">10</span><span style="color: #339933;">,</span>w<span style="color: #339933;">:</span><span style="color: #CC0000;">20</span><span style="color: #339933;">,</span>h<span style="color: #339933;">:</span><span style="color: #CC0000;">20</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; square2 <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>x<span style="color: #339933;">:</span><span style="color: #CC0000;">400</span><span style="color: #339933;">,</span>y<span style="color: #339933;">:</span><span style="color: #CC0000;">10</span><span style="color: #339933;">,</span>w<span style="color: #339933;">:</span><span style="color: #CC0000;">20</span><span style="color: #339933;">,</span>h<span style="color: #339933;">:</span><span style="color: #CC0000;">20</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; draw<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; document.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'keydown'</span><span style="color: #339933;">,</span>keyDown<span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; document.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'keyup'</span><span style="color: #339933;">,</span>keyUp<span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #003366; font-weight: bold;">function</span> clearRect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; ctx.<span style="color: #660066;">clearRect</span><span style="color: #009900;">&#40;</span>square2.<span style="color: #660066;">x</span><span style="color: #339933;">,</span>square2.<span style="color: #660066;">y</span><span style="color: #339933;">,</span>square2.<span style="color: #660066;">w</span><span style="color: #339933;">,</span>square2.<span style="color: #660066;">h</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; ctx.<span style="color: #660066;">clearRect</span><span style="color: #009900;">&#40;</span>square.<span style="color: #660066;">x</span><span style="color: #339933;">,</span>square.<span style="color: #660066;">y</span><span style="color: #339933;">,</span>square.<span style="color: #660066;">w</span><span style="color: #339933;">,</span>square.<span style="color: #660066;">h</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #003366; font-weight: bold;">function</span> drawSquare<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>rightKey<span style="color: #009900;">&#41;</span> square.<span style="color: #660066;">x</span> <span style="color: #339933;">+=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>leftKey<span style="color: #009900;">&#41;</span> square.<span style="color: #660066;">x</span> <span style="color: #339933;">-=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>upKey<span style="color: #009900;">&#41;</span> square.<span style="color: #660066;">y</span> <span style="color: #339933;">-=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>downKey<span style="color: #009900;">&#41;</span> square.<span style="color: #660066;">y</span> <span style="color: #339933;">+=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span><br />
&nbsp; ctx.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#0f0'</span><span style="color: #339933;">;</span><br />
&nbsp; ctx.<span style="color: #660066;">fillRect</span><span style="color: #009900;">&#40;</span>square.<span style="color: #660066;">x</span><span style="color: #339933;">,</span>square.<span style="color: #660066;">y</span><span style="color: #339933;">,</span>square.<span style="color: #660066;">w</span><span style="color: #339933;">,</span>square.<span style="color: #660066;">h</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; square2.<span style="color: #660066;">y</span> <span style="color: #339933;">+=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span><br />
&nbsp; ctx.<span style="color: #660066;">fillRect</span><span style="color: #009900;">&#40;</span>square2.<span style="color: #660066;">x</span><span style="color: #339933;">,</span>square2.<span style="color: #660066;">y</span><span style="color: #339933;">,</span>square2.<span style="color: #660066;">w</span><span style="color: #339933;">,</span>square2.<span style="color: #660066;">h</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #003366; font-weight: bold;">function</span> keyDown<span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">keyCode</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">39</span><span style="color: #009900;">&#41;</span> rightKey <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">keyCode</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">37</span><span style="color: #009900;">&#41;</span> leftKey <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">keyCode</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">38</span><span style="color: #009900;">&#41;</span> upKey <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">keyCode</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">40</span><span style="color: #009900;">&#41;</span> downKey <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> keyUp<span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">keyCode</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">39</span><span style="color: #009900;">&#41;</span> rightKey <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">keyCode</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">37</span><span style="color: #009900;">&#41;</span> leftKey <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">keyCode</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">38</span><span style="color: #009900;">&#41;</span> upKey <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">keyCode</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">40</span><span style="color: #009900;">&#41;</span> downKey <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #003366; font-weight: bold;">function</span> draw<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; clearRect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; drawSquare<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; loop <span style="color: #339933;">=</span> setTimeout<span style="color: #009900;">&#40;</span>draw<span style="color: #339933;">,</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">/</span><span style="color: #CC0000;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
window.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> init<span style="color: #339933;">;</span><br />
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span><br />
<br />
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>canvas id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;canvas&quot;</span> width<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;500&quot;</span> height<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;500&quot;</span><span style="color: #339933;">&gt;&lt;/</span>canvas<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></div></td></tr></tbody></table></div>
<p>I’ve just started experimenting with this technique, but I can really see it’s potential. There will still be plenty of times when it’s better to use clearRect to clear the entire canvas, but having this in your arsenal will help you become a better JavaScript developer.</p>
]]></content:encoded>
			<wfw:commentRss>http://atomicrobotdesign.com/blog/web-development/html5-canvas-you-dont-always-have-to-clear-the-entire-thing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Build a vertical scrolling shooter game with HTML5 canvas &#8211; Part 5</title>
		<link>http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-5/</link>
		<comments>http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-5/#comments</comments>
		<pubDate>Mon, 21 Mar 2011 00:23:36 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[HTML/CSS]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://atomicrobotdesign.com/blog/?p=958</guid>
		<description><![CDATA[Part 1 &#8211; Game Basics Part 2 &#8211; Graphics and Lasers Part 3 &#8211; Laser Hit Test Part 4 &#8211; Ship Hit Test and Score Part 5 &#8211; Player Lives and Continue Button Part 6 &#8211; Font fixes and Start &#8230; <a href="http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-5/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div style="width:350px; border:1px solid #333; margin-left:30px; margin-top:10px;">
<ul>
<li><a style="color:black; text-decoration:none;" href="http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-1/">Part 1 &#8211; Game Basics</a></li>
<li><a style="color:black; text-decoration:none;" href="http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-2/">Part 2 &#8211; Graphics and Lasers</a></li>
<li><a style="color:black; text-decoration:none;" href="http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-3/">Part 3 &#8211; Laser Hit Test</a></li>
<li><a style="color:black; text-decoration:none;" href="http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-4/">Part 4 &#8211; Ship Hit Test and Score</a></li>
<li><a style="color:black; text-decoration:none;" href="http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-5/">Part 5 &#8211; Player Lives and Continue Button</a></li>
<li><a style="color:black; text-decoration:none;" href="http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-6/">Part 6 &#8211; Font fixes and Start screen</a></li>
</ul>
</div>
<p>Last time we added collision detection for our ship and a score to our game. But only having one life isn’t all that fun and the player has to refresh the browser to play again. So in this part, we’re going to give our player multiple lives and we’ll add a continue button to the game over screen so that it’s easier for the player to play another round.</p>
<p>First, we’ll add the lives to our game. Add this to the list of variables at the top, because, of course, we need to keep track of how many lives our player has:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">lives <span style="color: #339933;">=</span> <span style="color: #CC0000;">3</span></div></td></tr></tbody></table></div>
<p>Next, we need to add some text to the canvas so our player can see how many lives they have left, so add this to the scoreTotal function:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Lives:'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span>lives<span style="color: #339933;">,</span> <span style="color: #CC0000;">68</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Since we’re already drawing text on the canvas with this function, we might as well add the lives to it and keep all over our text in one function. This way, if there’s a problem, it’s easier to find it and it’s also easier to change or update because we know exactly where to look. Next, we actually have to tie the lives into the game and set it up so that when the ship is hit, the player loses a life and if the player runs out of lives, the game ends. Currently, if the ship hits an enemy, the alive variable is just changed from true to false and the game ends. We can’t just replace that with lives -= 1 because all that will do is continuously subtract one from the score. What we need to do is create a new function called checkLives, which will see if the player has move than one life and if not end the game, otherwise, the game will just reset to the start position and the player will lose one life. Here’s the code for checkLives:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> checkLives<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp;lives <span style="color: #339933;">-=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lives <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;reset<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lives <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;alive <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>With this function, we subtract one live from the lives total right off the bat and then we check to see if the total of lives is greater than 0 and if it is, then we call the reset function which we’ll write next. If live is equal to zero, then we change alive to false, as we’ve been doing and it ends the game. Now in the shipCollision function we need to change the line:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">alive <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>to:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">checkLives<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Now, whenever the player’s ship hits an enemy, the checkLives function will be called. But what about the reset function? It’s just setting the player’s ship and the enemy ships back to the same position they have when the game starts because it would be unfair to just throw the player back into the middle of the game. Here’s the reset function:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> reset<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp;<span style="color: #003366; font-weight: bold;">var</span> enemy_reset_x <span style="color: #339933;">=</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">;</span><br />
&nbsp;ship_x <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>width <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">,</span> ship_y <span style="color: #339933;">=</span> height <span style="color: #339933;">-</span> <span style="color: #CC0000;">75</span><span style="color: #339933;">,</span> ship_w <span style="color: #339933;">=</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">,</span> ship_h <span style="color: #339933;">=</span> <span style="color: #CC0000;">57</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> enemies.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> enemy_reset_x<span style="color: #339933;">;</span><br />
&nbsp; &nbsp;enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">45</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;enemy_reset_x <span style="color: #339933;">=</span> enemy_reset_x <span style="color: #339933;">+</span> enemy_w <span style="color: #339933;">+</span> <span style="color: #CC0000;">60</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Everything is the same as when we start the game, including using a loop to position the enemies. The only difference this time is that we need to introduce a variable called enemy_reset_x in order to evenly space the enemies across the x axis. This function will also be used when we set up the continue button, we’ll simply call it when the button is clicked and all the work to reset the game is done.</p>
<p>The continue button is going to be a little more complex to set up. In Flash, something like this would be very easy to set up. All we’d need to do is create a button or movieclip on the stage, give it an instance name like continue_btn and write some ActionScript saying when continue_btn is clicked, then call the reset function. The canvas API hasn’t evolved this far yet, unfortunately, so to get the same functionality takes a bit more code. A lot of thanks goes out to Mark Pilgrim’s <a href="http://diveintohtml5.org/canvas.html">Dive into HTML5</a> for this, because as far as I can find, his is the only example of how to set up click events for the canvas.</p>
<p>To make this work, we need to do a few things, first draw the button on the canvas, next when the mouse is clicked within the canvas find the co-ordinates of the mouse’s position, then check those co-ordinates to see if they fall within our button’s boundaries. A bit more complicated than Flash, but I’m sure that it will either be simplified in then actually API or someone will write a plug-in that enables it for us to do quickly and easily.</p>
<p>Back to the code. First things first, we need to actually draw the button on the canvas, so we have something to click. Update the scoreTotal function to this:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> scoreTotal<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp;ctx.<span style="color: #660066;">font</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'bold 18px Arial'</span><span style="color: #339933;">;</span><br />
&nbsp;ctx.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#fff'</span><span style="color: #339933;">;</span><br />
&nbsp;ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Score: '</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">490</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span>score<span style="color: #339933;">,</span> <span style="color: #CC0000;">550</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Lives:'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span>lives<span style="color: #339933;">,</span> <span style="color: #CC0000;">68</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>alive<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Game Over!'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">245</span><span style="color: #339933;">,</span> height <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;ctx.<span style="color: #660066;">fillRect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>width <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">53</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span>height <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span><span style="color: #CC0000;">100</span><span style="color: #339933;">,</span><span style="color: #CC0000;">40</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;ctx.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#000'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Continue?'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">252</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span>height <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">35</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;canvas.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> continueButton<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Now, when the player runs our of lives, a white box with black text on top saying continue is added. As well as a new event listener. The event listener is added to the canvas element, not the document, like our keypress listeners. This is because we only care if the player clicks on the canvas, not if they click elsewhere on the page. So now when the player clicks on the canvas, a function called continueButton is called, so we’ll write that now:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> continueButton<span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp;<span style="color: #003366; font-weight: bold;">var</span> cursorPos <span style="color: #339933;">=</span> getCursorPos<span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>cursorPos.<span style="color: #660066;">x</span> <span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#40;</span>width <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">53</span> <span style="color: #339933;">&amp;&amp;</span> cursorPos.<span style="color: #660066;">x</span> <span style="color: #339933;">&lt;</span> <span style="color: #009900;">&#40;</span>width <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">47</span> <span style="color: #339933;">&amp;&amp;</span> cursorPos.<span style="color: #660066;">y</span> <span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#40;</span>height <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">10</span> <span style="color: #339933;">&amp;&amp;</span> cursorPos.<span style="color: #660066;">y</span> <span style="color: #339933;">&lt;</span> <span style="color: #009900;">&#40;</span>height <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;alive <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;lives <span style="color: #339933;">=</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;reset<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;canvas.<span style="color: #660066;">removeEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> continueButton<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>A bunch of things are going on with this function, the first is variable called cursorPos equaling to something called getCursorPos. This is the function we’ll use to find the cursor’s position when the mouse is clicked and then return the values in cursorPos so we can check them against our button. If the mouse’s x and y position falls within our continue button, then we set alive back to true, the lives back to 3, we call the reset function and, importantly, we remove the event listener so that the player can’t accidentally click on the canvas and reset the game. So what’s going on with getCursorPos?</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> getCursorPos<span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp;<span style="color: #003366; font-weight: bold;">var</span> x<span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #003366; font-weight: bold;">var</span> y<span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">pageX</span> <span style="color: #339933;">||</span> e.<span style="color: #660066;">pageY</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;x <span style="color: #339933;">=</span> e.<span style="color: #660066;">pageX</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;y <span style="color: #339933;">=</span> e.<span style="color: #660066;">pageY</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;x <span style="color: #339933;">=</span> e.<span style="color: #660066;">clientX</span> <span style="color: #339933;">+</span> document.<span style="color: #660066;">body</span>.<span style="color: #660066;">scrollLeft</span> <span style="color: #339933;">+</span> document.<span style="color: #660066;">documentElement</span>.<span style="color: #660066;">scrollLeft</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;y <span style="color: #339933;">=</span> e.<span style="color: #660066;">clientY</span> <span style="color: #339933;">+</span> document.<span style="color: #660066;">body</span>.<span style="color: #660066;">scrollTop</span> <span style="color: #339933;">+</span> document.<span style="color: #660066;">documentElement</span>.<span style="color: #660066;">scrollTop</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp;x <span style="color: #339933;">-=</span> canvas.<span style="color: #660066;">offsetLeft</span><span style="color: #339933;">;</span><br />
&nbsp;y <span style="color: #339933;">-=</span> canvas.<span style="color: #660066;">offsetTop</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #003366; font-weight: bold;">var</span> cursorPos <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> cursorPosition<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #000066; font-weight: bold;">return</span> cursorPos<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>This is a code that was written by Mark Pilgrim for his Halma game example, I’ve just modified the last two lines to fit better with our game. It’s using either the web standards way, pageX and pageY or the Microsoft way, with clientX and clientY. And then it subtracts the top and left offsets of the canvas, ie. subtracting the distance of the canvas’ left side with the left side of the browser and the same with the top. Then we create a local variable called cursorPos, which isn’t the same as the cursorPos in the continueButton function but it’s what we’ll return, so that cursorPos will equal this cursorPos, so it’s simpler to just name them the same. cursorPos is going to equal a new cursorPosition:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> cursorPosition<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span>y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp;<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">=</span> x<span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">y</span> <span style="color: #339933;">=</span> y<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>We’ll use cursorPosition to store our position info. So when we return cursorPos, in the continueButton function, we can check cursorPos.x and cursorPos.y instead of creating two variables for each one. Now, if the mouse cursor is within the continue button when the mouse is clicked, we can reset the game! You can check out the game <a href="http://atomicrobotdesign.com/blog_media/canvas_game/Part_5/part_5.html">so far here</a>.</p>
<p>That’s a lot of work for a continue button, it was more work then setting up most of the games main functions, but, right now, that’s how we have to do things with the canvas tag. I think it’s worth it though, because who wants to reset a game by refreshing the browser?</p>
<p>Our game is getting there, but it still needs a couple of things before we can say we have a basic game built. Next time, we’ll add a moving background, change the text to something works better with our 8-bit feel and finally, we’ll add a start screen so that the game just doesn’t start when the page is loaded.</p>
<p>Complete code so far:</p>
<div class="codecolorer-container html4strict twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br />118<br />119<br />120<br />121<br />122<br />123<br />124<br />125<br />126<br />127<br />128<br />129<br />130<br />131<br />132<br />133<br />134<br />135<br />136<br />137<br />138<br />139<br />140<br />141<br />142<br />143<br />144<br />145<br />146<br />147<br />148<br />149<br />150<br />151<br />152<br />153<br />154<br />155<br />156<br />157<br />158<br />159<br />160<br />161<br />162<br />163<br />164<br />165<br />166<br />167<br />168<br />169<br />170<br />171<br />172<br />173<br />174<br />175<br />176<br />177<br />178<br />179<br />180<br />181<br />182<br />183<br />184<br />185<br />186<br />187<br />188<br />189<br />190<br />191<br />192<br />193<br />194<br />195<br />196<br />197<br />198<br />199<br />200<br />201<br />202<br />203<br />204<br />205<br />206<br />207<br />208<br />209<br />210<br />211<br />212<br />213<br />214<br />215<br />216<br />217<br />218<br />219<br />220<br />221<br />222<br />223<br />224<br />225<br />226<br />227<br />228<br />229<br />230<br />231<br />232<br />233<br />234<br />235<br />236<br />237<br />238<br />239<br />240<br />241<br />242<br />243<br />244<br />245<br />246<br />247<br />248<br />249<br />250<br />251<br />252<br />253<br />254<br />255<br />256<br />257<br />258<br />259<br />260<br />261<br />262<br />263<br />264<br />265<br />266<br />267<br />268<br />269<br />270<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #00bbdd;">&lt;!DOCTYPE html&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">html</span></a> <span style="color: #000066;">lang</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;en&quot;</span>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/meta.html"><span style="color: #000000; font-weight: bold;">meta</span></a> <span style="color: #000066;">charset</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;utf-8&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/title.html"><span style="color: #000000; font-weight: bold;">title</span></a>&gt;</span>Game with score<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/title.html"><span style="color: #000000; font-weight: bold;">title</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/style.html"><span style="color: #000000; font-weight: bold;">style</span></a>&gt;</span><br />
body {<br />
&nbsp; padding:0;<br />
&nbsp; margin:0;<br />
&nbsp; background:#666;<br />
}<br />
canvas {<br />
&nbsp; display:block;<br />
&nbsp; margin:30px auto 0;<br />
&nbsp; border:1px dashed #ccc;<br />
&nbsp; background:#000;<br />
}<br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/style.html"><span style="color: #000000; font-weight: bold;">style</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span><br />
var canvas,<br />
&nbsp; &nbsp; ctx,<br />
&nbsp; &nbsp; width = 600,<br />
&nbsp; &nbsp; height = 600,<br />
&nbsp; &nbsp; enemyTotal = 5,<br />
&nbsp; &nbsp; enemies = [],<br />
&nbsp; &nbsp; enemy_x = 50,<br />
&nbsp; &nbsp; enemy_y = -45,<br />
&nbsp; &nbsp; enemy_w = 50,<br />
&nbsp; &nbsp; enemy_h = 38,<br />
&nbsp; &nbsp; speed = 3,<br />
&nbsp; &nbsp; enemy,<br />
&nbsp; &nbsp; rightKey = false,<br />
&nbsp; &nbsp; leftKey = false,<br />
&nbsp; &nbsp; upKey = false,<br />
&nbsp; &nbsp; downKey = false,<br />
&nbsp; &nbsp; ship,<br />
&nbsp; &nbsp; ship_x = (width / 2) - 25, ship_y = height - 75, ship_w = 50, ship_h = 57,<br />
&nbsp; &nbsp; laserTotal = 2,<br />
&nbsp; &nbsp; lasers = [],<br />
&nbsp; &nbsp; score = 0,<br />
&nbsp; &nbsp; alive = true,<br />
&nbsp; &nbsp; lives = 3;<br />
<br />
//Array to hold all the enemies on screen<br />
for (var i = 0; i <span style="color: #009900;">&lt; enemyTotal; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp;enemies.push<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>enemy_x, enemy_y, enemy_w, enemy_h, speed<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;"> &nbsp;enemy_x +<span style="color: #66cc66;">=</span> enemy_w + <span style="color: #cc66cc;">60</span>;</span><br />
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span><br />
<br />
<span style="color: #009900;"><span style="color: #66cc66;">//</span>Clears the canvas so it can be updated</span><br />
<span style="color: #009900;">function clearCanvas<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp;ctx.clearRect<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #000066;">width</span>,<span style="color: #000066;">height</span><span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span><br />
<br />
<span style="color: #009900;"><span style="color: #66cc66;">//</span>Cycles through the array and draws the updated enemy position</span><br />
<span style="color: #009900;">function drawEnemies<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp;<span style="color: #000066;">for</span> <span style="color: #66cc66;">&#40;</span>var i <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>; i &lt; enemies.length; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;ctx.drawImage<span style="color: #66cc66;">&#40;</span>enemy, enemies<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>, enemies<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;"> &nbsp;<span style="color: #66cc66;">&#125;</span></span><br />
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span><br />
<br />
<span style="color: #009900;"><span style="color: #66cc66;">//</span>If an arrow key is being pressed, moves the ship in the right direction</span><br />
<span style="color: #009900;">function drawShip<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp;if <span style="color: #66cc66;">&#40;</span>rightKey<span style="color: #66cc66;">&#41;</span> ship_x +<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">5</span>;</span><br />
<span style="color: #009900;"> &nbsp;else if <span style="color: #66cc66;">&#40;</span>leftKey<span style="color: #66cc66;">&#41;</span> ship_x -<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">5</span>;</span><br />
<span style="color: #009900;"> &nbsp;if <span style="color: #66cc66;">&#40;</span>upKey<span style="color: #66cc66;">&#41;</span> ship_y -<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">5</span>;</span><br />
<span style="color: #009900;"> &nbsp;else if <span style="color: #66cc66;">&#40;</span>downKey<span style="color: #66cc66;">&#41;</span> ship_y +<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">5</span>;</span><br />
<span style="color: #009900;"> &nbsp;if <span style="color: #66cc66;">&#40;</span>ship_x &lt;<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> ship_x <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>;</span><br />
<span style="color: #009900;"> &nbsp;if <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>ship_x + ship_w<span style="color: #66cc66;">&#41;</span> &gt;</span>= width) ship_x = width - ship_w;<br />
&nbsp; if (ship_y <span style="color: #009900;">&lt;<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> ship_y <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>;</span><br />
<span style="color: #009900;"> &nbsp;if <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>ship_y + ship_h<span style="color: #66cc66;">&#41;</span> &gt;</span>= height) ship_y = height - ship_h;<br />
&nbsp; ctx.drawImage(ship, ship_x, ship_y);<br />
}<br />
<br />
//This moves the enemies downwards on the canvas and if one passes the bottom of the canvas, it moves it to above the canvas<br />
function moveEnemies() {<br />
&nbsp; for (var i = 0; i <span style="color: #009900;">&lt; enemies.length; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;if <span style="color: #66cc66;">&#40;</span>enemies<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> &lt; <span style="color: #000066;">height</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp;enemies<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> +<span style="color: #66cc66;">=</span> enemies<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#93;</span>;</span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #66cc66;">&#125;</span> else if <span style="color: #66cc66;">&#40;</span>enemies<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> &gt;</span> height - 1) {<br />
&nbsp; &nbsp; &nbsp; enemies[i][1] = -45;<br />
&nbsp; &nbsp; }<br />
&nbsp; }<br />
}<br />
<br />
//If there are lasers in the lasers array, then this will draw them on the canvas<br />
function drawLaser() {<br />
&nbsp; if (lasers.length)<br />
&nbsp; &nbsp; for (var i = 0; i <span style="color: #009900;">&lt; lasers.length; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp;ctx.fillStyle <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'#f00'</span>;</span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp;ctx.fillRect<span style="color: #66cc66;">&#40;</span>lasers<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>,lasers<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>,lasers<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span>,lasers<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #66cc66;">&#125;</span></span><br />
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span><br />
<br />
<span style="color: #009900;"><span style="color: #66cc66;">//</span>If we<span style="color: #ff0000;">'re drawing lasers on the canvas, this moves them up the canvas</span><br />
<span style="color: #009900;">function moveLaser() {</span><br />
<span style="color: #009900;"> &nbsp;for (var i = 0; i &lt; lasers.length; i++) {</span><br />
<span style="color: #009900;"> &nbsp; &nbsp;if (lasers[i][1] &gt;</span></span> -11) {<br />
&nbsp; &nbsp; &nbsp; lasers[i][1] -= 10;<br />
&nbsp; &nbsp; } else if (lasers[i][1] <span style="color: #009900;">&lt; -<span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp;lasers.splice<span style="color: #66cc66;">&#40;</span>i, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #66cc66;">&#125;</span></span><br />
<span style="color: #009900;"> &nbsp;<span style="color: #66cc66;">&#125;</span></span><br />
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span><br />
<br />
<span style="color: #009900;"><span style="color: #66cc66;">//</span>Runs a couple of loops to see if any of the lasers have hit any of the enemies</span><br />
<span style="color: #009900;">function hitTest<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp;var remove <span style="color: #66cc66;">=</span> false;</span><br />
<span style="color: #009900;"> &nbsp;<span style="color: #000066;">for</span> <span style="color: #66cc66;">&#40;</span>var i <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>; i &lt; lasers.length; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">for</span> <span style="color: #66cc66;">&#40;</span>var j <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>; j &lt; enemies.length; j++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp;if <span style="color: #66cc66;">&#40;</span>lasers<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> &lt;<span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>enemies<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> + enemies<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> &amp;&amp; lasers<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> &gt;</span>= enemies[j][0] <span style="color: #ddbb00;">&amp;&amp; lasers[i][0] &lt;= (enemies[j][0] + enemies[j][2])) {</span><br />
<span style="color: #ddbb00;"> &nbsp; &nbsp; &nbsp; &nbsp;remove = true;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; enemies.splice(j, 1);<br />
&nbsp; &nbsp; &nbsp; &nbsp; score += 10;<br />
&nbsp; &nbsp; &nbsp; &nbsp; enemies.push([(Math.random() * 500) + 50, -45, enemy_w, enemy_h, speed]);<br />
&nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; if (remove == true) {<br />
&nbsp; &nbsp; &nbsp; lasers.splice(i, 1);<br />
&nbsp; &nbsp; &nbsp; remove = false;<br />
&nbsp; &nbsp; }<br />
&nbsp; }<br />
}<br />
<br />
//Similar to the laser hit test, this function checks to see if the player's ship collides with any of the enemies<br />
function shipCollision() {<br />
&nbsp; var ship_xw = ship_x + ship_w,<br />
&nbsp; &nbsp; &nbsp; ship_yh = ship_y + ship_h;<br />
&nbsp; for (var i = 0; i <span style="color: #009900;">&lt; enemies.length; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;if <span style="color: #66cc66;">&#40;</span>ship_x &gt;</span> enemies[i][0] <span style="color: #ddbb00;">&amp;&amp; ship_x &lt; enemies[i][0] + enemy_w &amp;&amp; ship_y &gt; enemies[i][1] &amp;&amp; ship_y &lt; enemies[i][1] + enemy_h) {</span><br />
<span style="color: #ddbb00;"> &nbsp; &nbsp; &nbsp;checkLives();</span><br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; if (ship_xw <span style="color: #009900;">&lt; enemies<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> + enemy_w &amp;&amp; ship_xw &gt;</span> enemies[i][0] <span style="color: #ddbb00;">&amp;&amp; ship_y &gt; enemies[i][1] &amp;&amp; ship_y &lt; enemies[i][1] + enemy_h) {</span><br />
<span style="color: #ddbb00;"> &nbsp; &nbsp; &nbsp;checkLives();</span><br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; if (ship_yh &gt; enemies[i][1] <span style="color: #ddbb00;">&amp;&amp; ship_yh &lt; enemies[i][1] + enemy_h &amp;&amp; ship_x &gt; enemies[i][0] &amp;&amp; ship_x &lt; enemies[i][0] + enemy_w) {</span><br />
<span style="color: #ddbb00;"> &nbsp; &nbsp; &nbsp;checkLives();</span><br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; if (ship_yh &gt; enemies[i][1] <span style="color: #ddbb00;">&amp;&amp; ship_yh &lt; enemies[i][1] + enemy_h &amp;&amp; ship_xw &lt; enemies[i][0] + enemy_w &amp;&amp; ship_xw &gt; enemies[i][0]) {</span><br />
<span style="color: #ddbb00;"> &nbsp; &nbsp; &nbsp;checkLives();</span><br />
&nbsp; &nbsp; }<br />
&nbsp; }<br />
}<br />
<br />
//This function runs whenever the player's ship hits an enemy and either subtracts a life or sets the alive variable to false if the player runs out of lives<br />
function checkLives() {<br />
&nbsp; lives -= 1;<br />
&nbsp; if (lives &gt; 0) {<br />
&nbsp; &nbsp; reset();<br />
&nbsp; } else if (lives == 0) {<br />
&nbsp; &nbsp; alive = false;<br />
&nbsp; }<br />
}<br />
<br />
//This simply resets the ship and enemies to their starting positions<br />
function reset() {<br />
&nbsp; var enemy_reset_x = 50;<br />
&nbsp; ship_x = (width / 2) - 25, ship_y = height - 75, ship_w = 50, ship_h = 57;<br />
&nbsp; for (var i = 0; i <span style="color: #009900;">&lt; enemies.length; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;enemies<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> enemy_reset_x;</span><br />
<span style="color: #009900;"> &nbsp; &nbsp;enemies<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> -<span style="color: #cc66cc;">45</span>;</span><br />
<span style="color: #009900;"> &nbsp; &nbsp;enemy_reset_x <span style="color: #66cc66;">=</span> enemy_reset_x + enemy_w + <span style="color: #cc66cc;">60</span>;</span><br />
<span style="color: #009900;"> &nbsp;<span style="color: #66cc66;">&#125;</span></span><br />
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span><br />
<br />
<span style="color: #009900;"><span style="color: #66cc66;">//</span>After the player loses all their lives, the continue button is shown and if clicked, it resets the game and removes the event listener <span style="color: #000066;">for</span> the continue button</span><br />
<span style="color: #009900;">function continueButton<span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp;var cursorPos <span style="color: #66cc66;">=</span> getCursorPos<span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;"> &nbsp;if <span style="color: #66cc66;">&#40;</span>cursorPos.x &gt;</span> (width / 2) - 53 <span style="color: #ddbb00;">&amp;&amp; cursorPos.x &lt; (width / 2) + 47 &amp;&amp; cursorPos.y &gt; (height / 2) + 10 &amp;&amp; cursorPos.y &lt; (height / 2) + 50) {</span><br />
<span style="color: #ddbb00;"> &nbsp; &nbsp;alive = true;</span><br />
&nbsp; &nbsp; lives = 3;<br />
&nbsp; &nbsp; reset();<br />
&nbsp; &nbsp; canvas.removeEventListener('click', continueButton, false);<br />
&nbsp; }<br />
}<br />
<br />
//holds the cursors position<br />
function cursorPosition(x,y) {<br />
&nbsp; this.x = x;<br />
&nbsp; this.y = y;<br />
}<br />
<br />
//finds the cursor's position after the mouse is clicked<br />
function getCursorPos(e) {<br />
&nbsp; var x;<br />
&nbsp; var y;<br />
&nbsp; if (e.pageX || e.pageY) {<br />
&nbsp; &nbsp; x = e.pageX;<br />
&nbsp; &nbsp; y = e.pageY;<br />
&nbsp; } else {<br />
&nbsp; &nbsp; x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;<br />
&nbsp; &nbsp; y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;<br />
&nbsp; }<br />
&nbsp; x -= canvas.offsetLeft;<br />
&nbsp; y -= canvas.offsetTop;<br />
&nbsp; var cursorPos = new cursorPosition(x, y);<br />
&nbsp; return cursorPos;<br />
}<br />
<br />
//Draws the text for the score and lives on the canvas and if the player runs out of lives, it's draws the game over text and continue button as well as adding the event listener for the continue button<br />
function scoreTotal() {<br />
&nbsp; ctx.font = 'bold 18px Arial';<br />
&nbsp; ctx.fillStyle = '#fff';<br />
&nbsp; ctx.fillText('Score: ', 490, 30);<br />
&nbsp; ctx.fillText(score, 550, 30);<br />
&nbsp; ctx.fillText('Lives:', 10, 30);<br />
&nbsp; ctx.fillText(lives, 68, 30);<br />
&nbsp; if (!alive) {<br />
&nbsp; &nbsp; ctx.fillText('Game Over!', 245, height / 2);<br />
&nbsp; &nbsp; ctx.fillRect((width / 2) - 53, (height / 2) + 10,100,40);<br />
&nbsp; &nbsp; ctx.fillStyle = '#000';<br />
&nbsp; &nbsp; ctx.fillText('Continue?', 252, (height / 2) + 35);<br />
&nbsp; &nbsp; canvas.addEventListener('click', continueButton, false);<br />
&nbsp; }<br />
}<br />
<br />
//The initial function called when the page first loads. Loads the ship, enemy and starfield images and adds the event listeners for the arrow keys. It then calls the gameLoop function.<br />
function init() {<br />
&nbsp; canvas = document.getElementById('canvas');<br />
&nbsp; ctx = canvas.getContext('2d');<br />
&nbsp; enemy = new Image();<br />
&nbsp; enemy.src = '8bit_enemy.png';<br />
&nbsp; ship = new Image();<br />
&nbsp; ship.src = 'ship.png';<br />
&nbsp; document.addEventListener('keydown', keyDown, false);<br />
&nbsp; document.addEventListener('keyup', keyUp, false);<br />
&nbsp; gameLoop();<br />
}<br />
<br />
//The main function of the game, it calls all the other functions needed to make the game run<br />
function gameLoop() {<br />
&nbsp; clearCanvas();<br />
&nbsp; if (alive <span style="color: #ddbb00;">&amp;&amp; lives &gt; 0) {</span><br />
<span style="color: #ddbb00;"> &nbsp; &nbsp;hitTest();</span><br />
&nbsp; &nbsp; shipCollision();<br />
&nbsp; &nbsp; moveLaser();<br />
&nbsp; &nbsp; moveEnemies();<br />
&nbsp; &nbsp; drawEnemies();<br />
&nbsp; &nbsp; drawShip();<br />
&nbsp; &nbsp; drawLaser();<br />
&nbsp; }<br />
&nbsp; scoreTotal();<br />
&nbsp; game = setTimeout(gameLoop, 1000 / 30);<br />
}<br />
<br />
//Checks to see which key has been pressed and either to move the ship or fire a laser<br />
function keyDown(e) {<br />
&nbsp; if (e.keyCode == 39) rightKey = true;<br />
&nbsp; else if (e.keyCode == 37) leftKey = true;<br />
&nbsp; if (e.keyCode == 38) upKey = true;<br />
&nbsp; else if (e.keyCode == 40) downKey = true;<br />
&nbsp; if (e.keyCode == 88 <span style="color: #ddbb00;">&amp;&amp; lasers.length &lt;= laserTotal) lasers.push([ship_x + 25, ship_y - 20, 4, 20]);</span><br />
}<br />
<br />
//Checks to see if a pressed key has been released and stops the ships movement if it has<br />
function keyUp(e) {<br />
&nbsp; if (e.keyCode == 39) rightKey = false;<br />
&nbsp; else if (e.keyCode == 37) leftKey = false;<br />
&nbsp; if (e.keyCode == 38) upKey = false;<br />
&nbsp; else if (e.keyCode == 40) downKey = false;<br />
}<br />
<br />
window.onload = init;<br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span><br />
<br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;canvas <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;canvas&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;600&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;600&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>canvas&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">html</span></a>&gt;</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Build a vertical scrolling shooter game with HTML5 canvas &#8211; Part 4</title>
		<link>http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-4/</link>
		<comments>http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-4/#comments</comments>
		<pubDate>Wed, 16 Mar 2011 04:06:28 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[HTML/CSS]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://atomicrobotdesign.com/blog/?p=949</guid>
		<description><![CDATA[Part 1 &#8211; Game Basics Part 2 &#8211; Graphics and Lasers Part 3 &#8211; Laser Hit Test Part 4 &#8211; Ship Hit Test and Score Part 5 &#8211; Player Lives and Continue Button Part 6 &#8211; Font fixes and Start &#8230; <a href="http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-4/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div style="width:350px; border:1px solid #333; margin-left:30px; margin-top:10px;">
<ul>
<li><a style="color:black; text-decoration:none;" href="http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-1/">Part 1 &#8211; Game Basics</a></li>
<li><a style="color:black; text-decoration:none;" href="http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-2/">Part 2 &#8211; Graphics and Lasers</a></li>
<li><a style="color:black; text-decoration:none;" href="http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-3/">Part 3 &#8211; Laser Hit Test</a></li>
<li><a style="color:black; text-decoration:none;" href="http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-4/">Part 4 &#8211; Ship Hit Test and Score</a></li>
<li><a style="color:black; text-decoration:none;" href="http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-5/">Part 5 &#8211; Player Lives and Continue Button</a></li>
<li><a style="color:black; text-decoration:none;" href="http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-6/">Part 6 &#8211; Font fixes and Start screen</a></li>
</ul>
</div>
<p>Last time we made it so that our laser would hit an enemy ship and they would be removed and a new enemy would be added to the game. This time we’re going to add a score and set up hit detection on the player’s ship because it would be a pretty boring game if the player can’t die.</p>
<p>The first thing we need to add is a new variable. Add this to the end of the list of variables at the top:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">score <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span></div></td></tr></tbody></table></div>
<p>Now we need to create a function that will display the score on the canvas. Creating and displaying text on the canvas is pretty simple, in fact it’s a lot like drawing a shape. We need to set three things, the font, the style and the content and position. Let’s build the function that will display our score:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> scoreTotal<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; ctx.<span style="color: #660066;">font</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'bold 18px Arial'</span><span style="color: #339933;">;</span><br />
&nbsp; ctx.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#fff'</span><span style="color: #339933;">;</span><br />
&nbsp; ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Score: '</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">490</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span>score<span style="color: #339933;">,</span> <span style="color: #CC0000;">550</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>With ctx.font, we are setting the weight, size and font of our text. Then with ctx.fillStyle, we set the color to white. Next, we use fillText to actually draw the text on the canvas. For fillText, we need to set three parameters, ctx.fillText(content, x position, y position). In the first instance of fillText, we pass the content as ‘Score’ and the x position to 490px and the y position to 30px. For the second instance, we use the variable score to set the content and the score will update on the canvas for us. And, of course, none of this will work until we actually call the function. So add this to the gameLoop function:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">scoreTotal<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>If you test it now, there should be ‘Score: 0’ in the top right corner. Except when an enemy is shot, the score doesn’t increase yet. We just need to add one line to the hitTest function. After the line enemies.splice(j, 1), add this:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">score <span style="color: #339933;">+=</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Now, every time that an enemy is destroyed and removed, the score will be increased by 10. Since we have the score working and it wasn’t that hard to set up, we’ll create the hit detection for the player’s ship. We’ll start with this:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> shipCollision<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> ship_xw <span style="color: #339933;">=</span> ship_x <span style="color: #339933;">+</span> ship_w<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; ship_yh <span style="color: #339933;">=</span> ship_y <span style="color: #339933;">+</span> ship_h<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> enemies.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>The first thing we do is create a couple of variables called ship_xw and ship_yh. These two variables find us the top right and bottom right corners of the ship by adding the x position to the width of the ship and the y position to the height. This function is pretty much the same as hit detection for the laser, we’re just checking more, because the ship has four side and the enemies have four sides and any one of them could touch, so we need to check for each. The for loop checks our ship against every enemy in the enemies array. Now the code that checks to see if the ship is touching any of the enemies:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> shipCollision<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> ship_xw <span style="color: #339933;">=</span> ship_x <span style="color: #339933;">+</span> ship_w<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; ship_yh <span style="color: #339933;">=</span> ship_y <span style="color: #339933;">+</span> ship_h<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> enemies.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ship_x <span style="color: #339933;">&gt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;&amp;</span> ship_x <span style="color: #339933;">&lt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> enemy_w <span style="color: #339933;">&amp;&amp;</span> ship_y <span style="color: #339933;">&gt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;&amp;</span> ship_y <span style="color: #339933;">&lt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> enemy_h<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ship_xw <span style="color: #339933;">&lt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> enemy_w <span style="color: #339933;">&amp;&amp;</span> ship_xw <span style="color: #339933;">&gt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;&amp;</span> ship_y <span style="color: #339933;">&gt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;&amp;</span> ship_y <span style="color: #339933;">&lt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> enemy_h<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ship_yh <span style="color: #339933;">&gt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;&amp;</span> ship_yh <span style="color: #339933;">&lt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> enemy_h <span style="color: #339933;">&amp;&amp;</span> ship_x <span style="color: #339933;">&gt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;&amp;</span> ship_x <span style="color: #339933;">&lt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> enemy_w<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ship_yh <span style="color: #339933;">&gt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;&amp;</span> ship_yh <span style="color: #339933;">&lt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> enemy_h <span style="color: #339933;">&amp;&amp;</span> ship_xw <span style="color: #339933;">&lt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> enemy_w <span style="color: #339933;">&amp;&amp;</span> ship_xw <span style="color: #339933;">&gt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Each of these four if statements checks to see if any part of the ship is within one of the enemies. I’m not going to go through each one, they essentially say, if the ship’s x position is greater than the enemy’s x position but less than it’s x position plus it’s width and it’s y position is greater than the enemy’s y position but less than the enemy’s y position plus it’s height, then it’s a hit. And this is repeated for the other three sides of the ship.</p>
<p>This is all great and everything, but it won’t amount to much unless we actually have something happen when there’s a collision between the ship and an enemy. So we need to create a new variable that we’ll add to the list at the top:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">alive <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span></div></td></tr></tbody></table></div>
<p>Pretty self explanatory, if alive equals true, then our ship is alive, now back in the shipCollision function, we just need to update it to this:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> shipCollision<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> ship_xw <span style="color: #339933;">=</span> ship_x <span style="color: #339933;">+</span> ship_w<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; ship_yh <span style="color: #339933;">=</span> ship_y <span style="color: #339933;">+</span> ship_h<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> enemies.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ship_x <span style="color: #339933;">&gt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;&amp;</span> ship_x <span style="color: #339933;">&lt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> enemy_w <span style="color: #339933;">&amp;&amp;</span> ship_y <span style="color: #339933;">&gt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;&amp;</span> ship_y <span style="color: #339933;">&lt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> enemy_h<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; alive <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ship_xw <span style="color: #339933;">&lt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> enemy_w <span style="color: #339933;">&amp;&amp;</span> ship_xw <span style="color: #339933;">&gt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;&amp;</span> ship_y <span style="color: #339933;">&gt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;&amp;</span> ship_y <span style="color: #339933;">&lt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> enemy_h<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; alive <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ship_yh <span style="color: #339933;">&gt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;&amp;</span> ship_yh <span style="color: #339933;">&lt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> enemy_h <span style="color: #339933;">&amp;&amp;</span> ship_x <span style="color: #339933;">&gt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;&amp;</span> ship_x <span style="color: #339933;">&lt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> enemy_w<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; alive <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ship_yh <span style="color: #339933;">&gt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;&amp;</span> ship_yh <span style="color: #339933;">&lt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> enemy_h <span style="color: #339933;">&amp;&amp;</span> ship_xw <span style="color: #339933;">&lt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> enemy_w <span style="color: #339933;">&amp;&amp;</span> ship_xw <span style="color: #339933;">&gt;</span> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; alive <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Now, whenever the ship hits an enemy, alive will be set to false. Of course, this needs to be added to the gameLoop so that the function actually runs:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">shipCollision<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>We still have to change one thing, right now, if alive is changed to false, it doesn’t matter because it’s not actually affecting anything. To make shipCollision actually do something, the gameLoop function needs to be updated to this:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> gameLoop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; clearCanvas<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>alive<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; hitTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; shipCollision<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; moveLaser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; moveEnemies<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; drawEnemies<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; drawShip<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; drawLaser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; scoreTotal<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Now the game will only run if alive is true and if it’s false, we get a black screen with just the score showing. That’s kind of boring, so for now we can at least tell the player the game is over by adding this to the scoreTotal function:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>alive<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Game Over!'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">245</span><span style="color: #339933;">,</span> height <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>When alive is false, the words game over will appear at the center of the canvas. And with that, we continue to add functionality to our game. Next time, we’ll add lives and give the player the option to play again after they’ve lost all their lives. One more thing, as I learn more about building games using JavaScript and the canvas tag, I’ve discovered one other change we should make to the game. Up until now, we’ve been using setInterval to fire off our code, but I’ve learned that it’s not always the best option, mainly because, as in our case, we’ve set the game to fire every 25 milliseconds. This can lead to a problem if the browser can’t run all the code in those 25 milliseconds. It seems the smart way to go, is to use setTimeout, which fires won’t fire until all the code has run. Instead of having:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">setInterval<span style="color: #009900;">&#40;</span>gameLoop<span style="color: #339933;">,</span> <span style="color: #CC0000;">25</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>inside the init function, we’ll add this to the gameLoop function:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">game <span style="color: #339933;">=</span> setTimeout<span style="color: #009900;">&#40;</span>gameLoop<span style="color: #339933;">,</span> <span style="color: #CC0000;">1000</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Setting it to 1000/30 amounts to running the game at thirty frames a second, which is what I’ve always set for my Flash games. You can check out the game so far <a href="http://atomicrobotdesign.com/blog_media/canvas_game/Part_4/part_4.html" target="_blank">right here</a>.</p>
<p>Here’s the entire code:</p>
<div class="codecolorer-container html4strict twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br />118<br />119<br />120<br />121<br />122<br />123<br />124<br />125<br />126<br />127<br />128<br />129<br />130<br />131<br />132<br />133<br />134<br />135<br />136<br />137<br />138<br />139<br />140<br />141<br />142<br />143<br />144<br />145<br />146<br />147<br />148<br />149<br />150<br />151<br />152<br />153<br />154<br />155<br />156<br />157<br />158<br />159<br />160<br />161<br />162<br />163<br />164<br />165<br />166<br />167<br />168<br />169<br />170<br />171<br />172<br />173<br />174<br />175<br />176<br />177<br />178<br />179<br />180<br />181<br />182<br />183<br />184<br />185<br />186<br />187<br />188<br />189<br />190<br />191<br />192<br />193<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #00bbdd;">&lt;!DOCTYPE html&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">html</span></a> <span style="color: #000066;">lang</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;en&quot;</span>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/meta.html"><span style="color: #000000; font-weight: bold;">meta</span></a> <span style="color: #000066;">charset</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;utf-8&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/title.html"><span style="color: #000000; font-weight: bold;">title</span></a>&gt;</span>Game with score<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/title.html"><span style="color: #000000; font-weight: bold;">title</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/style.html"><span style="color: #000000; font-weight: bold;">style</span></a>&gt;</span><br />
body {<br />
&nbsp; padding:0;<br />
&nbsp; margin:0;<br />
&nbsp; background:#666;<br />
}<br />
canvas {<br />
&nbsp; display:block;<br />
&nbsp; margin:30px auto 0;<br />
&nbsp; border:1px dashed #ccc;<br />
&nbsp; background:#000;<br />
}<br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/style.html"><span style="color: #000000; font-weight: bold;">style</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span><br />
var canvas,<br />
&nbsp; &nbsp; ctx,<br />
&nbsp; &nbsp; width = 600,<br />
&nbsp; &nbsp; height = 600,<br />
&nbsp; &nbsp; enemyTotal = 5,<br />
&nbsp; &nbsp; enemies = [],<br />
&nbsp; &nbsp; enemy_x = 50,<br />
&nbsp; &nbsp; enemy_y = -45,<br />
&nbsp; &nbsp; enemy_w = 50,<br />
&nbsp; &nbsp; enemy_h = 38,<br />
&nbsp; &nbsp; speed = 3,<br />
&nbsp; &nbsp; enemy,<br />
&nbsp; &nbsp; rightKey = false,<br />
&nbsp; &nbsp; leftKey = false,<br />
&nbsp; &nbsp; upKey = false,<br />
&nbsp; &nbsp; downKey = false,<br />
&nbsp; &nbsp; ship,<br />
&nbsp; &nbsp; ship_x = (width / 2) - 25, ship_y = height - 75, ship_w = 50, ship_h = 57,<br />
&nbsp; &nbsp; laserTotal = 2,<br />
&nbsp; &nbsp; lasers = [],<br />
&nbsp; &nbsp; score = 0,<br />
&nbsp; &nbsp; alive = true;<br />
<br />
for (var i = 0; i <span style="color: #009900;">&lt; enemyTotal; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp;enemies.push<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>enemy_x, enemy_y, enemy_w, enemy_h, speed<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;"> &nbsp;enemy_x +<span style="color: #66cc66;">=</span> enemy_w + <span style="color: #cc66cc;">60</span>;</span><br />
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span><br />
<br />
<span style="color: #009900;">function clearCanvas<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp;ctx.clearRect<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #000066;">width</span>,<span style="color: #000066;">height</span><span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span><br />
<br />
<span style="color: #009900;">function drawEnemies<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp;<span style="color: #000066;">for</span> <span style="color: #66cc66;">&#40;</span>var i <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>; i &lt; enemies.length; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;ctx.drawImage<span style="color: #66cc66;">&#40;</span>enemy, enemies<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>, enemies<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;"> &nbsp;<span style="color: #66cc66;">&#125;</span></span><br />
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span><br />
<br />
<span style="color: #009900;">function drawShip<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp;if <span style="color: #66cc66;">&#40;</span>rightKey<span style="color: #66cc66;">&#41;</span> ship_x +<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">5</span>;</span><br />
<span style="color: #009900;"> &nbsp;else if <span style="color: #66cc66;">&#40;</span>leftKey<span style="color: #66cc66;">&#41;</span> ship_x -<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">5</span>;</span><br />
<span style="color: #009900;"> &nbsp;if <span style="color: #66cc66;">&#40;</span>upKey<span style="color: #66cc66;">&#41;</span> ship_y -<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">5</span>;</span><br />
<span style="color: #009900;"> &nbsp;else if <span style="color: #66cc66;">&#40;</span>downKey<span style="color: #66cc66;">&#41;</span> ship_y +<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">5</span>;</span><br />
<span style="color: #009900;"> &nbsp;if <span style="color: #66cc66;">&#40;</span>ship_x &lt;<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> ship_x <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>;</span><br />
<span style="color: #009900;"> &nbsp;if <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>ship_x + ship_w<span style="color: #66cc66;">&#41;</span> &gt;</span>= width) ship_x = width - ship_w;<br />
&nbsp; if (ship_y <span style="color: #009900;">&lt;<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> ship_y <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>;</span><br />
<span style="color: #009900;"> &nbsp;if <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>ship_y + ship_h<span style="color: #66cc66;">&#41;</span> &gt;</span>= height) ship_y = height - ship_h;<br />
&nbsp; ctx.drawImage(ship, ship_x, ship_y);<br />
}<br />
<br />
function moveEnemies() {<br />
&nbsp; for (var i = 0; i <span style="color: #009900;">&lt; enemies.length; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;if <span style="color: #66cc66;">&#40;</span>enemies<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> &lt; <span style="color: #000066;">height</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp;enemies<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> +<span style="color: #66cc66;">=</span> enemies<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#93;</span>;</span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #66cc66;">&#125;</span> else if <span style="color: #66cc66;">&#40;</span>enemies<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> &gt;</span> height - 1) {<br />
&nbsp; &nbsp; &nbsp; enemies[i][1] = -45;<br />
&nbsp; &nbsp; }<br />
&nbsp; }<br />
}<br />
<br />
function drawLaser() {<br />
&nbsp; if (lasers.length)<br />
&nbsp; &nbsp; for (var i = 0; i <span style="color: #009900;">&lt; lasers.length; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp;ctx.fillStyle <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'#f00'</span>;</span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp;ctx.fillRect<span style="color: #66cc66;">&#40;</span>lasers<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>,lasers<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>,lasers<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span>,lasers<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #66cc66;">&#125;</span></span><br />
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span><br />
<span style="color: #009900;">function moveLaser<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp;<span style="color: #000066;">for</span> <span style="color: #66cc66;">&#40;</span>var i <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>; i &lt; lasers.length; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;if <span style="color: #66cc66;">&#40;</span>lasers<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> &gt;</span> -11) {<br />
&nbsp; &nbsp; &nbsp; lasers[i][1] -= 10;<br />
&nbsp; &nbsp; } else if (lasers[i][1] <span style="color: #009900;">&lt; -<span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp;lasers.splice<span style="color: #66cc66;">&#40;</span>i, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #66cc66;">&#125;</span></span><br />
<span style="color: #009900;"> &nbsp;<span style="color: #66cc66;">&#125;</span></span><br />
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span><br />
<br />
<span style="color: #009900;">function hitTest<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp;var remove <span style="color: #66cc66;">=</span> false;</span><br />
<span style="color: #009900;"> &nbsp;<span style="color: #000066;">for</span> <span style="color: #66cc66;">&#40;</span>var i <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>; i &lt; lasers.length; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">for</span> <span style="color: #66cc66;">&#40;</span>var j <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>; j &lt; enemies.length; j++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp;if <span style="color: #66cc66;">&#40;</span>lasers<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> &lt;<span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>enemies<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> + enemies<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> &amp;&amp; lasers<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> &gt;</span>= enemies[j][0] <span style="color: #ddbb00;">&amp;&amp; lasers[i][0] &lt;= (enemies[j][0] + enemies[j][2])) {</span><br />
<span style="color: #ddbb00;"> &nbsp; &nbsp; &nbsp; &nbsp;remove = true;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; enemies.splice(j, 1);<br />
&nbsp; &nbsp; &nbsp; &nbsp; score += 10;<br />
&nbsp; &nbsp; &nbsp; &nbsp; enemies.push([(Math.random() * 500) + 50, -45, enemy_w, enemy_h, speed]);<br />
&nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; if (remove == true) {<br />
&nbsp; &nbsp; &nbsp; lasers.splice(i, 1);<br />
&nbsp; &nbsp; &nbsp; remove = false;<br />
&nbsp; &nbsp; }<br />
&nbsp; }<br />
}<br />
<br />
function shipCollision() {<br />
&nbsp; var ship_xw = ship_x + ship_w,<br />
&nbsp; &nbsp; &nbsp; ship_yh = ship_y + ship_h;<br />
&nbsp; for (var i = 0; i <span style="color: #009900;">&lt; enemies.length; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;if <span style="color: #66cc66;">&#40;</span>ship_x &gt;</span> enemies[i][0] <span style="color: #ddbb00;">&amp;&amp; ship_x &lt; enemies[i][0] + enemy_w &amp;&amp; ship_y &gt; enemies[i][1] &amp;&amp; ship_y &lt; enemies[i][1] + enemy_h) {</span><br />
<span style="color: #ddbb00;"> &nbsp; &nbsp; &nbsp;alive = false;</span><br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; if (ship_xw <span style="color: #009900;">&lt; enemies<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> + enemy_w &amp;&amp; ship_xw &gt;</span> enemies[i][0] <span style="color: #ddbb00;">&amp;&amp; ship_y &gt; enemies[i][1] &amp;&amp; ship_y &lt; enemies[i][1] + enemy_h) {</span><br />
<span style="color: #ddbb00;"> &nbsp; &nbsp; &nbsp;alive = false;</span><br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; if (ship_yh &gt; enemies[i][1] <span style="color: #ddbb00;">&amp;&amp; ship_yh &lt; enemies[i][1] + enemy_h &amp;&amp; ship_x &gt; enemies[i][0] &amp;&amp; ship_x &lt; enemies[i][0] + enemy_w) {</span><br />
<span style="color: #ddbb00;"> &nbsp; &nbsp; &nbsp;alive = false;</span><br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; if (ship_yh &gt; enemies[i][1] <span style="color: #ddbb00;">&amp;&amp; ship_yh &lt; enemies[i][1] + enemy_h &amp;&amp; ship_xw &lt; enemies[i][0] + enemy_w &amp;&amp; ship_xw &gt; enemies[i][0]) {</span><br />
<span style="color: #ddbb00;"> &nbsp; &nbsp; &nbsp;alive = false;</span><br />
&nbsp; &nbsp; }<br />
&nbsp; }<br />
}<br />
<br />
function scoreTotal() {<br />
&nbsp; ctx.font = 'bold 18px Arial';<br />
&nbsp; ctx.fillStyle = '#fff';<br />
&nbsp; ctx.fillText('Score: ', 490, 30);<br />
&nbsp; ctx.fillText(score, 550, 30);<br />
&nbsp; if (!alive) {<br />
&nbsp; &nbsp; ctx.fillText('Game Over!', 245, height / 2);<br />
&nbsp; }<br />
}<br />
<br />
function init() {<br />
&nbsp; canvas = document.getElementById('canvas');<br />
&nbsp; ctx = canvas.getContext('2d');<br />
&nbsp; enemy = new Image();<br />
&nbsp; enemy.src = '8bit_enemy.png';<br />
&nbsp; ship = new Image();<br />
&nbsp; ship.src = 'ship.png';<br />
&nbsp; //setInterval(gameLoop, 25);<br />
&nbsp; document.addEventListener('keydown', keyDown, false);<br />
&nbsp; document.addEventListener('keyup', keyUp, false);<br />
&nbsp; gameLoop();<br />
}<br />
function gameLoop() {<br />
&nbsp; clearCanvas();<br />
&nbsp; if (alive) {<br />
&nbsp; &nbsp; hitTest();<br />
&nbsp; &nbsp; shipCollision();<br />
&nbsp; &nbsp; moveLaser();<br />
&nbsp; &nbsp; moveEnemies();<br />
&nbsp; &nbsp; drawEnemies();<br />
&nbsp; &nbsp; drawShip();<br />
&nbsp; &nbsp; drawLaser(); &nbsp;<br />
&nbsp; }<br />
&nbsp; scoreTotal();<br />
&nbsp; game = setTimeout(gameLoop, 1000 / 30);<br />
}<br />
<br />
function keyDown(e) {<br />
&nbsp; if (e.keyCode == 39) rightKey = true;<br />
&nbsp; else if (e.keyCode == 37) leftKey = true;<br />
&nbsp; if (e.keyCode == 38) upKey = true;<br />
&nbsp; else if (e.keyCode == 40) downKey = true;<br />
&nbsp; if (e.keyCode == 88 <span style="color: #ddbb00;">&amp;&amp; lasers.length &lt;= laserTotal) lasers.push([ship_x + 25, ship_y - 20, 4, 20]);</span><br />
}<br />
<br />
function keyUp(e) {<br />
&nbsp; if (e.keyCode == 39) rightKey = false;<br />
&nbsp; else if (e.keyCode == 37) leftKey = false;<br />
&nbsp; if (e.keyCode == 38) upKey = false;<br />
&nbsp; else if (e.keyCode == 40) downKey = false;<br />
}<br />
<br />
window.onload = init;<br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span><br />
<br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;canvas <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;canvas&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;600&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;600&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>canvas&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">html</span></a>&gt;</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

