<?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; HTML/CSS</title>
	<atom:link href="http://atomicrobotdesign.com/blog/category/htmlcss/feed/" rel="self" type="application/rss+xml" />
	<link>http://atomicrobotdesign.com/blog</link>
	<description>Design From The Future &#124; Blog</description>
	<lastBuildDate>Thu, 03 May 2012 04:33:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>An animated expanding search input is easy with the CSS focus pseudo-class</title>
		<link>http://atomicrobotdesign.com/blog/htmlcss/an-animated-expanding-search-input-is-easy-with-the-css-focus-pseudo-class/</link>
		<comments>http://atomicrobotdesign.com/blog/htmlcss/an-animated-expanding-search-input-is-easy-with-the-css-focus-pseudo-class/#comments</comments>
		<pubDate>Wed, 04 Apr 2012 04:06:58 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[HTML/CSS]]></category>

		<guid isPermaLink="false">http://atomicrobotdesign.com/blog/?p=1795</guid>
		<description><![CDATA[The great thing about CSS is when you can do something with it, it usually only takes a couple of lines. Recently, I was helping on a project and it had a little effect on the search input where it &#8230; <a href="http://atomicrobotdesign.com/blog/htmlcss/an-animated-expanding-search-input-is-easy-with-the-css-focus-pseudo-class/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The great thing about CSS is when you can do something with it, it usually only takes a couple of lines. Recently, I was helping on a project and it had a little effect on the search input where it expanded when you clicked in it. When I looked at how he did it, I was surprised to see a bunch of jQuery. I thought, hey, this is the perfect kind of thing that we can use CSS transitions for but when I mentioned this to him, he wasn’t sure how you’d trigger the transition. Lucky for us, this is where the focus pseudo-class comes in.</p>
<p><iframe style="width: 100%; height: 300px" src="http://jsfiddle.net/mike_thomas/fUDBC/embedded/result,html,css" allowfullscreen="allowfullscreen" frameborder="0"></iframe></p>
<p>The important CSS is this:</p>
<div class="codecolorer-container css 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="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6666ff;">.search</span> <span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">75px</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp;-webkit-transition<span style="color: #00AA00;">:</span> all .5s ease<span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp;-moz-transition<span style="color: #00AA00;">:</span> all .5s ease<span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp;transition<span style="color: #00AA00;">:</span> all .5s ease<span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<span style="color: #6666ff;">.search</span><span style="color: #3333ff;">:focus </span><span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">200px</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span></div></td></tr></tbody></table></div>
<p>Crazy simple stuff here, normally the input with the class search has a width of 75 pixels but when the user clicks in it, the focus pseudo-class is activated, just like the :hover pseudo-class and then the width is set to 200 pixels. And with the transition set on the search class, the effect is animated. Plus, in my example, I’ve floated the input to the right, so it expands to the left, which would require some extra jQuery code to pull off.</p>
<p>One thing, this won’t animate in any version of IE (it’s coming in IE10) but the box will expand to the larger width when the input is clicked in.</p>
]]></content:encoded>
			<wfw:commentRss>http://atomicrobotdesign.com/blog/htmlcss/an-animated-expanding-search-input-is-easy-with-the-css-focus-pseudo-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Recreate the 3D slider effect from the Android home page</title>
		<link>http://atomicrobotdesign.com/blog/htmlcss/recreate-the-3d-slider-effect-from-the-android-home-page/</link>
		<comments>http://atomicrobotdesign.com/blog/htmlcss/recreate-the-3d-slider-effect-from-the-android-home-page/#comments</comments>
		<pubDate>Thu, 29 Dec 2011 03:58:16 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[HTML/CSS]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://atomicrobotdesign.com/blog/?p=1447</guid>
		<description><![CDATA[I think I’ve mentioned on here before that once in a while I’ll see an effect on a web page and I’ll become obsessed with how it was down. The latest thing to catch my attention is the 3D slide &#8230; <a href="http://atomicrobotdesign.com/blog/htmlcss/recreate-the-3d-slider-effect-from-the-android-home-page/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I think I’ve mentioned on here before that once in a while I’ll see an effect on a web page and I’ll become obsessed with how it was down. The latest thing to catch my attention is the 3D slide effect that’s on the home page of the <a href="http://www.android.com/" target="_blank">Android site</a>. It’s a great effect and makes use of CSS3 3D transforms. But it does continue a trend I’ve been seeing from Google lately, it only works in Webkit browsers like Chrome and Safari although it could be set to work in Firefox 10, which is in beta right now. This might be because Google wanted it to work a certain way and Firefox’s implementation doesn’t work quite the same as Webkit’s.</p>
<p>The trick to how the effect works is that it’s powered by CSS but it uses JavaScript to trigger the effect. And the CSS it uses are translateX, transitions and animations. The animations are used to create a fade effect when the slider moves more than one “slide”. I’m not going to go step by step on how to build this, I’ll just go over the key parts and then if you really want to build your own version, you can go through the <a href="http://atomicrobotdesign.com/blog_media/android-page-effect/android-page-effect.html" target="_blank">demo</a> I’ve created. Here’s our basic HTML:</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 /></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/div.html"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box-wrapper&quot;</span>&gt;</span><br />
&nbsp;<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box-container&quot;</span>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;home&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;about&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;work&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image-left&quot;</span>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;contact&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span><br />
&nbsp;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span></div></td></tr></tbody></table></div>
<p>First off, anytime you use 3D with CSS, then you need to set the <a href="https://developer.mozilla.org/en/CSS/perspective" target="_blank">perspective</a>:</p>
<div class="codecolorer-container css 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="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6666ff;">.box-wrapper</span> <span style="color: #00AA00;">&#123;</span><br />
&nbsp;-webkit-perspective<span style="color: #00AA00;">:</span> <span style="color: #933;">600px</span><span style="color: #00AA00;">;</span><br />
&nbsp;-webkit-perspective-origin<span style="color: #00AA00;">:</span> <span style="color: #933;">2000px</span> <span style="color: #933;">-200px</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span></div></td></tr></tbody></table></div>
<p>A div with the class box-wrapper is the wrapper div for everything. The perspective property is set to 600 pixels, which essentially means the content is 600 pixels away on the z index. Next, the <a href="https://developer.mozilla.org/en/CSS/perspective-origin" target="_blank">perspective-origin</a> property is set to 2000px on the x axis and -200px on the y axis. This sets up the view that the user has. Next, we need to set up our transform-style:</p>
<div class="codecolorer-container css 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="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #cc00cc;">#box-container</span> <span style="color: #00AA00;">&#123;</span><br />
&nbsp;-webkit-transform-style<span style="color: #00AA00;">:</span> preserve-3d<span style="color: #00AA00;">;</span><br />
&nbsp;-webkit-transition<span style="color: #00AA00;">:</span> -webkit-transform .6s ease-in-out<span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span></div></td></tr></tbody></table></div>
<p>Inside box-wrapper is a div with the id of box-container. I’ve set the <a href="https://developer.mozilla.org/en/CSS/transform-style" target="_blank">transform-style</a> to perserve-3d. There’s only two options, perserve-3d or flat, the first says everything should be positioned in 3D space, the other positions everything on the same plane as the container element.</p>
<p>Now it needs some JavaScript to set up the translateZ property of all the divs. The reason I decided to use to set all the z axis positioning on the divs is mainly because it allows you to add or remove divs without worrying about setting anything for them manually.</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;">var</span> startDist <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span><br />
boxContainer.<span style="color: #660066;">style</span><span style="color: #009900;">&#91;</span>transform<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'translateZ('</span> <span style="color: #339933;">+</span> startDist <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px)'</span><span style="color: #339933;">;</span><br />
<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> boxes.<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;boxes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span><span style="color: #009900;">&#91;</span>transform<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'translateZ('</span> <span style="color: #339933;">+</span> startDist <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px)'</span><span style="color: #339933;">;</span><br />
&nbsp;startDist <span style="color: #339933;">-=</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>First thing this does is set the translateZ property of the box-container div to 0. Next, the boxes array contains all the divs with a class of box and in the loop they are all given a translateZ value that’s 400 less than the previous one.</p>
<p>The basics to how this works is this, there’s four divs, the first one is 0, the next -400, then -800 and finally -1200. To get the moving through 3D space effect, the box-container div’s translateX value is changed to be the same as the one we want to see, so if the 3rd one is selected, it will be set to -800 and that div is the one in the view port. To trigger this effect, the translateX of the box-container will be set depending on what link is clicked:</p>
<p>There’s one other effect that really helps flesh this out and that’s when the user clicks on a div that not the next div, say they click on div 3 when they’re currently on div 1, there’s a nice fading animation to make it look like the user is moving through the other divs. First the animation:</p>
<div class="codecolorer-container css 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="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #a1a100;">@-webkit-keyframes half-fade {</span><br />
&nbsp;<span style="color: #933;">0%</span> <span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp;opacity<span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><br />
&nbsp;<span style="color: #00AA00;">&#125;</span><br />
&nbsp;<span style="color: #933;">50%</span> <span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp;opacity<span style="color: #00AA00;">:</span> .4<span style="color: #00AA00;">;</span><br />
&nbsp;<span style="color: #00AA00;">&#125;</span><br />
&nbsp;<span style="color: #933;">100%</span> <span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp;opacity<span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><br />
&nbsp;<span style="color: #00AA00;">&#125;</span><br />
<span style="color: #00AA00;">&#125;</span></div></td></tr></tbody></table></div>
<p>So this just fades the div in to 40% opacity and then back to zero to add to the transition effect. To add this effect, there’s a class called transition:</p>
<div class="codecolorer-container css 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="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6666ff;">.transition</span> <span style="color: #00AA00;">&#123;</span><br />
&nbsp;-webkit-animation<span style="color: #00AA00;">:</span> half-fade .6s ease<span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span></div></td></tr></tbody></table></div>
<p>What happens is that if the user is going from say, div 1 to div 3, then div 2 would have the transition class added to it and you’d get that fade effect. But there’s one problem, once a CSS animation is fired, there’s no way to restart it. This was the part that caused me the most problems but a quick search lead me to <a href="http://css-tricks.com/restart-css-animation/" target="_blank">CSS-Tricks</a>, where of course <a href="https://twitter.com/#!/chriscoyier" target="_blank">Chris Coyier</a> had the answer. The solution is that you have remove the element and then add a new version of it. Luckily, that only takes 5 lines of JS:</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> clone<span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp;<span style="color: #003366; font-weight: bold;">var</span> el <span style="color: #339933;">=</span> boxes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #003366; font-weight: bold;">var</span> nel <span style="color: #339933;">=</span> el.<span style="color: #660066;">cloneNode</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;el.<span style="color: #660066;">parentNode</span>.<span style="color: #660066;">replaceChild</span><span style="color: #009900;">&#40;</span>nel<span style="color: #339933;">,</span> el<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>This code gets the element, clones the element and then replaces the element with the new element. This allows use to reset the animations.</p>
<p>Poke around the code in my <a href="http://atomicrobotdesign.com/blog_media/android-page-effect/android-page-effect.html" target="_blank">demo</a>. I didn’t use jQuery because A) Google didn’t on their site and B) because I want to make sure that I don’t rely on jQuery for everything. I made my demo to work in Firefox and also future proofed it to work in the other major browsers for the one day when they include 3D transforms.</p>
<p>One other thing, in at least my version of Chrome, the dev channel on Windows 7, you can’t click on either of the back two divs, in my version or on <a href="http://www.android.com/" target="_blank">android.com</a>. The same goes for Firefox. It does let you click on them in Safari, so I’m guessing this is a temporary bug but one that really make using this on real sites impossible, hopefully it gets fixed soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://atomicrobotdesign.com/blog/htmlcss/recreate-the-3d-slider-effect-from-the-android-home-page/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why I used the Flexible Box Model in my blog redesign</title>
		<link>http://atomicrobotdesign.com/blog/htmlcss/why-i-used-the-flexible-box-model-in-my-blog-redesign/</link>
		<comments>http://atomicrobotdesign.com/blog/htmlcss/why-i-used-the-flexible-box-model-in-my-blog-redesign/#comments</comments>
		<pubDate>Fri, 06 May 2011 01:25:13 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[HTML/CSS]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[modernizr]]></category>

		<guid isPermaLink="false">http://atomicrobotdesign.com/blog/?p=1041</guid>
		<description><![CDATA[This week I redesigned my blog and I decided to used the CSS3 flexible box model to control the positioning of most of the elements. If you’ve taken a look at it, the flexible box model is a great addition &#8230; <a href="http://atomicrobotdesign.com/blog/htmlcss/why-i-used-the-flexible-box-model-in-my-blog-redesign/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This week I redesigned my blog and I decided to used the <a href="http://www.html5rocks.com/tutorials/flexbox/quick/">CSS3 flexible box model</a> to control the positioning of most of the elements. If you’ve taken a look at it, the flexible box model is a great addition to our web site building tools and makes positioning a lot easier. Any experienced web designer or developer knows that sometimes the biggest challenge of turning a design into an actual web page is positioning elements like columns horizontally and enabling the page to stretch based on content. This is one of the problems that the flexible box model solves.</p>
<p>You’ve probably heard of the <a href="http://nicolasgallagher.com/micro-clearfix-hack/">clearfix hack</a>. Honestly, it took me a while before I started using it, mainly because for the longest time I followed the advice that you shouldn’t use hacks in your CSS. But eventually, I saw how many developers that I looked up to were using and I will admit, it made things a lot easier. At the same time though, it is still a hack and it’s the kind of thing that the flexible box model make unnecessary. At least in the browsers that implement it that is and predictably, Internet Explorer isn’t one that supports it, not even IE9.</p>
<p>So, wait. If not version of IE doesn’t support it and it’s the most widely used browser, then why would I use the flexible box model? There’s two reasons. The first being, I looked at my analytics and saw the IE users amounted to a very small about of the visitors to my blog, and since Firefox, Chrome and Safari all support it, then I shouldn’t have that much to worry about. But what kind of web developer would I be, if I didn’t make my blog display properly in most browsers. I needed a way to know if the browser supports the flexible box model or not.</p>
<p>That’s where <a href="http://www.modernizr.com/">Modernizr</a> came in. Like the clearfix hack, for a while I was a bit wary of relying on a JavaScript file to make my site work. But, the more I looked into it and the more I built sites that used more and more JS, I realized the chance of it not loading is pretty low. What Modernizr does is add class names to the html tag of a web page. In my blog’s case, if the browser has implemented Flexbox, the it will Modernizr will add a class of flexbox and, in my CSS file, it will run this (shown without the vendor pre-fixes):</p>
<div class="codecolorer-container css 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="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6666ff;">.flexbox</span> <span style="color: #cc00cc;">#wrapper</span> <span style="color: #00AA00;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">100%</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span>box<span style="color: #00AA00;">;</span><br />
&nbsp; box-orient<span style="color: #00AA00;">:</span>vertical<span style="color: #00AA00;">;</span><br />
&nbsp; box-align<span style="color: #00AA00;">:</span><span style="color: #993333;">center</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span></div></td></tr></tbody></table></div>
<p>If it doesn’t support Flexbox, then Modernizr adds a class of no-flexbox and this CSS will be used:</p>
<div class="codecolorer-container css 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="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6666ff;">.no-flexbox</span> <span style="color: #cc00cc;">#content</span> <span style="color: #00AA00;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span></div></td></tr></tbody></table></div>
<p>I’ve become a big fan of Modernizr and I’m using it on more and more sites. If you’re planning on using it for just one or two CSS3 or HTML5 features, then you can now just download a <a href="http://modernizr.github.com/Modernizr/2.0-beta/">custom build of Modernizr</a> so it’s an even smaller file to load and run. Use Modernizr and you can start building websites using the modern features of HTML5 and CSS3 that you may have been putting off because you weren’t sure if it would work or not.</p>
]]></content:encoded>
			<wfw:commentRss>http://atomicrobotdesign.com/blog/htmlcss/why-i-used-the-flexible-box-model-in-my-blog-redesign/feed/</wfw:commentRss>
		<slash:comments>1</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>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>1</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>
		<item>
		<title>Build a vertical scrolling shooter game with HTML5 canvas &#8211; Part 3</title>
		<link>http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-3/</link>
		<comments>http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-3/#comments</comments>
		<pubDate>Mon, 21 Feb 2011 02:51:30 +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=908</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-3/">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’ve been following along, you’ll know that in the first two parts, we built a shooter game using HTML5 canvas that has a ship that we can control and shoot lasers from and enemies that move. Now we’re going to check to see if a laser hits an enemy and then remove that enemy from the game. If you’re coming from building games using Flash and ActionScript 3.0, the first thing you’re going to miss is hitTestObject. There’s nothing like this in JavaScript and the canvas API, so we’ll have to set up the hit test ourselves.</p>
<p>Adobe recognized that people were using Flash to build games and made things easier by introducing hitTestObject that allowed you to just write something like this:</p>
<div class="codecolorer-container actionscript 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="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">laser.<span style="color: #006600;">hitTestObject</span><span style="color: #66cc66;">&#40;</span>enemy<span style="color: #66cc66;">&#41;</span></div></td></tr></tbody></table></div>
<p>That would check to see if the laser hit the enemy ship for you. Unfortunately, since the canvas API is still pretty new, we don’t have anything like that yet, so we’ll have to build our own hit test function. What we’ll need is to set up two loops, one for the lasers and one for the enemy ships. Then we check the position of each laser against each enemy ship and if a laser’s x position is greater than an enemy ship’s x position and the laser’s x position is less than the enemy’s x position plus it’s width, as well see if the laser’s y position is less than the enemy’s y position plus it’s height. If all of those are true then it means that the laser has hit the enemy ship.</p>
<p>Here’s that all translated into JavaScript:</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> hitTest<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;">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> lasers.<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;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> enemies.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lasers<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;">&lt;=</span> <span style="color: #009900;">&#40;</span>enemies<span style="color: #009900;">&#91;</span>j<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> enemies<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> lasers<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;">&gt;=</span> enemies<span style="color: #009900;">&#91;</span>j<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> lasers<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;">&lt;=</span> <span style="color: #009900;">&#40;</span>enemies<span style="color: #009900;">&#91;</span>j<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> enemies<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</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>We also need to call hitTest, so update the gameLoop 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> 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; hitTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; moveEnemies<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; moveLaser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; drawEnemies<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; drawShip<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; drawLaser<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>Right now, this function will check to see if the laser has hit the enemy ship but it won’t actually do anything when it does. Of course, we want to remove both the laser and the ship. You would think we could just 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 />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<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> hitTest<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;">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> lasers.<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;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> enemies.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lasers<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;">&lt;=</span> <span style="color: #009900;">&#40;</span>enemies<span style="color: #009900;">&#91;</span>j<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> enemies<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> lasers<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;">&gt;=</span> enemies<span style="color: #009900;">&#91;</span>j<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> lasers<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;">&lt;=</span> <span style="color: #009900;">&#40;</span>enemies<span style="color: #009900;">&#91;</span>j<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> enemies<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; lasers.<span style="color: #660066;">splice</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; enemies.<span style="color: #660066;">splice</span><span style="color: #009900;">&#40;</span>j<span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</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>This will remove both, but it will also give us an error. The problem is that for every time we run the loop for the lasers, we run the loop for the enemies for as many times as the length of the enemies array. So if our laser hits something before the enemies loop ends, it will still be checking to see if that laser has hit anything even though it’s already been removed. We can fix this by just using a boolean. If the laser hits, then remove the laser is true. And then we can move the lasers.splice outside of the enemies loop and we won’t get any errors.</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> hitTest<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> remove <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;">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> lasers.<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;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> enemies.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lasers<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;">&lt;=</span> <span style="color: #009900;">&#40;</span>enemies<span style="color: #009900;">&#91;</span>j<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> enemies<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> lasers<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;">&gt;=</span> enemies<span style="color: #009900;">&#91;</span>j<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> lasers<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;">&lt;=</span> <span style="color: #009900;">&#40;</span>enemies<span style="color: #009900;">&#91;</span>j<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> enemies<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; remove <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; enemies.<span style="color: #660066;">splice</span><span style="color: #009900;">&#40;</span>j<span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</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>remove <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; lasers.<span style="color: #660066;">splice</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; remove <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>We’ve just added a variable called remove, it’s declared in the function because we don’t need it anywhere else in the program. We set it to false and then when a laser hits an enemy ship, we change remove to true and then outside the enemy loop but inside the laser loop, we have an if statement checking if remove is true and if it is, we remove that laser and then set remove back to false.</p>
<p>Now both the laser and enemy ship will be removed. But since we only have five enemy ships, this will be a pretty quick game. So we’ll just add one line to our hitTest function and we can set up the game to have a continuous stream of enemies. Update the hitTest 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 />16<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> hitTest<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> remove <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;">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> lasers.<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;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> enemies.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lasers<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;">&lt;=</span> <span style="color: #009900;">&#40;</span>enemies<span style="color: #009900;">&#91;</span>j<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> enemies<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> lasers<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;">&gt;=</span> enemies<span style="color: #009900;">&#91;</span>j<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> lasers<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;">&lt;=</span> <span style="color: #009900;">&#40;</span>enemies<span style="color: #009900;">&#91;</span>j<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> enemies<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; remove <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; enemies.<span style="color: #660066;">splice</span><span style="color: #009900;">&#40;</span>j<span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; enemies.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">45</span><span style="color: #339933;">,</span> enemy_w<span style="color: #339933;">,</span> enemy_h<span style="color: #339933;">,</span> speed<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</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>remove <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; lasers.<span style="color: #660066;">splice</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; remove <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>You can check out the game so far <a href="http://atomicrobotdesign.com/blog_media/canvas_game/Part_3/part_3.html">here</a>. Right now, our ship is invincible, so we’ll add another hit test to see if our ship has hit an enemy and we’ll also add scoring. 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 /></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 Shooter Game Tutorial Part 3<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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; enemy,<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 = 50,<br />
&nbsp; &nbsp; speed = 3,<br />
&nbsp; &nbsp; rightKey = false,<br />
&nbsp; &nbsp; leftKey = false,<br />
&nbsp; &nbsp; upKey = false,<br />
&nbsp; &nbsp; downKey = false,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ship,<br />
&nbsp; &nbsp; ship_x = (width / 2) - 25, ship_y = height - 75, ship_w = 50, ship_h = 50,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; laserTotal = 2,<br />
&nbsp; &nbsp; lasers = [];<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; &nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &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 />
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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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 init() {<br />
&nbsp; canvas = document.getElementById('canvas');<br />
&nbsp; ctx = canvas.getContext('2d');<br />
&nbsp; &nbsp; &nbsp; &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 />
}<br />
function gameLoop() {<br />
&nbsp; clearCanvas();<br />
&nbsp; &nbsp; &nbsp; &nbsp; hitTest();<br />
&nbsp; moveEnemies();<br />
&nbsp; &nbsp; &nbsp; &nbsp; moveLaser();<br />
&nbsp; drawEnemies();<br />
&nbsp; drawShip();<br />
&nbsp; &nbsp; &nbsp; &nbsp; drawLaser();<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 />
<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-3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Build a vertical scrolling shooter game with HTML5 canvas &#8211; Part 2</title>
		<link>http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-2/</link>
		<comments>http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-2/#comments</comments>
		<pubDate>Mon, 14 Feb 2011 03:45:39 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[HTML/CSS]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://atomicrobotdesign.com/blog/?p=898</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-2/">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 built the basic structure of our game, but moving a block around while some others move down the screen doesn’t really make a great game. So let’s change the blocks to actual space ship graphics and give our ship the ability to shoot lasers.</p>
<p>Changing the blocks to ship graphics isn’t that hard. We just need to change the method we use to draw the ship and enemies from fillRect to drawImage. Here’s the two images I’m using:</p>
<div style="width:120px; margin:auto;">
<img style="margin-right:15px;" src="http://atomicrobotdesign.com/blog/wp-content/uploads/2011/02/ship.png" alt="" title="ship" width="50" height="57" class="aligncenter size-full wp-image-901" /><img src="http://atomicrobotdesign.com/blog/wp-content/uploads/2011/02/8bit_enemy.png" alt="" title="8bit_enemy" width="50" height="38" class="aligncenter size-full wp-image-900" />
</div>
<p>Now we just need to make some adjustments to the JavaScript. The first thing we do is load the images, so add these two 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"><span style="color: #003366; font-weight: bold;">var</span> enemy<span style="color: #339933;">,</span> ship<span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>And then change the init 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 /></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> 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; enemy <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 />
&nbsp; enemy.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'8bit_enemy.png'</span><span style="color: #339933;">;</span><br />
&nbsp; 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 />
&nbsp; 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 />
&nbsp; 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><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></div></td></tr></tbody></table></div>
<p>We’ve add four lines that first tell the variables ship and enemy that they’re images and then we set the source of each image. Now that we’ve loaded the images, we need to actually draw them on the canvas. In the drawShip function, we need to 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;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#0f0'</span><span style="color: #339933;">;</span><br />
ctx.<span style="color: #660066;">fillRect</span><span style="color: #009900;">&#40;</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></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;">drawImage</span><span style="color: #009900;">&#40;</span>ship<span style="color: #339933;">,</span> ship_x<span style="color: #339933;">,</span> ship_y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Now if you test the game, you’ll see the green block has been replaced with the ship graphic. Now on to the enemy ships. It’s the exact same thing, in the drawEnemies function, 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;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#f00'</span><span style="color: #339933;">;</span><br />
ctx.<span style="color: #660066;">fillRect</span><span style="color: #009900;">&#40;</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> 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_w<span style="color: #339933;">,</span> enemy_h<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 /></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>enemy<span style="color: #339933;">,</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> 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: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Now, the enemies will appear on the canvas as the enemy ship png I created. Now let’s make our ship shoot something. First we need to add a couple of 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"><span style="color: #003366; font-weight: bold;">var</span> laserTotal <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> lasers <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>We want to put a limit on the amount of lasers that a player can have on the screen at once, just to make things a bit more challenging, so laserTotal is set to 2, which actually means 3 lasers because arrays start at 0. Next, we need to add this line to the keyDown 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>e.<span style="color: #660066;">keyCode</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">88</span> <span style="color: #339933;">&amp;&amp;</span> lasers.<span style="color: #660066;">length</span> <span style="color: #339933;">&lt;=</span> laserTotal<span style="color: #009900;">&#41;</span> lasers.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>ship_x <span style="color: #339933;">+</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">,</span> ship_y <span style="color: #339933;">-</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>This says, if the x key is pressed and then number of lasers in the laser array is less then totalLaser, then add a laser to our laser array. We’re positioning the laser to be at the front of the current position of our ship. The next step is to draw the laser on the stage. We need to create a function called drawLaser.</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> drawLaser<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>lasers.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &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> lasers.<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; &nbsp; ctx.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#f00'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; ctx.<span style="color: #660066;">fillRect</span><span style="color: #009900;">&#40;</span>lasers<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>lasers<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>lasers<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>lasers<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>It’s pretty much the same thing as how drawEnemies function, except we’re checking to see if there are any lasers in the array first because if the player hasn’t shot one yet, we don’t want to draw any. The lasers aren’t going to do any good unless we make them move, so now we’ll add the moveLaser 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> moveLaser<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;">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> lasers.<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>lasers<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;">&gt;</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">11</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; lasers<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: #CC0000;">10</span><span style="color: #339933;">;</span><br />
&nbsp; &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>lasers<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;">&lt;</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; lasers.<span style="color: #660066;">splice</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</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>With moveLaser, we’re running a loop and if the laser is visible in the canvas, we move it up 10 pixels, otherwise, if it’s passed beyond the top, we remove it from the array. The last thing we have to do is up date 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 />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> 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; moveEnemies<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; moveLaser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; drawEnemies<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; drawShip<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; drawLaser<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>And all these changes should get us <a href="http://atomicrobotdesign.com/blog_media/canvas_game/Part_2/part_2.html">this</a>. Next time, we’ll have the lasers actually hit something! 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 /></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 Shooter Game Tutorial Part 1<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; enemy,<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 = 50,<br />
&nbsp; &nbsp; speed = 3,<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 = 50,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; laserTotal = 2,<br />
&nbsp; &nbsp; lasers = [];<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; &nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &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 />
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 init<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;canvas <span style="color: #66cc66;">=</span> document.getElementById<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'canvas'</span><span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;"> &nbsp;ctx <span style="color: #66cc66;">=</span> canvas.getContext<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'2d'</span><span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;"> &nbsp;enemy <span style="color: #66cc66;">=</span> new Image<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;"> &nbsp;enemy.<span style="color: #000066;">src</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'8bit_enemy.png'</span>;</span><br />
<span style="color: #009900;"> &nbsp;ship <span style="color: #66cc66;">=</span> new Image<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;"> &nbsp;ship.<span style="color: #000066;">src</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'ship.png'</span>;</span><br />
<span style="color: #009900;"> &nbsp;setInterval<span style="color: #66cc66;">&#40;</span>gameLoop, <span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;"> &nbsp;document.addEventListener<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'keydown'</span>, keyDown, false<span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;"> &nbsp;document.addEventListener<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'keyup'</span>, keyUp, false<span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span><br />
<span style="color: #009900;">function gameLoop<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;clearCanvas<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;"> &nbsp;moveEnemies<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;"> &nbsp;moveLaser<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;"> &nbsp;drawEnemies<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;"> &nbsp;drawShip<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;"> &nbsp;drawLaser<span style="color: #66cc66;">&#40;</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 keyDown<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;if <span style="color: #66cc66;">&#40;</span>e.keyCode <span style="color: #66cc66;">==</span> <span style="color: #cc66cc;">39</span><span style="color: #66cc66;">&#41;</span> rightKey <span style="color: #66cc66;">=</span> true;</span><br />
<span style="color: #009900;"> &nbsp;else if <span style="color: #66cc66;">&#40;</span>e.keyCode <span style="color: #66cc66;">==</span> <span style="color: #cc66cc;">37</span><span style="color: #66cc66;">&#41;</span> leftKey <span style="color: #66cc66;">=</span> true;</span><br />
<span style="color: #009900;"> &nbsp;if <span style="color: #66cc66;">&#40;</span>e.keyCode <span style="color: #66cc66;">==</span> <span style="color: #cc66cc;">38</span><span style="color: #66cc66;">&#41;</span> upKey <span style="color: #66cc66;">=</span> true;</span><br />
<span style="color: #009900;"> &nbsp;else if <span style="color: #66cc66;">&#40;</span>e.keyCode <span style="color: #66cc66;">==</span> <span style="color: #cc66cc;">40</span><span style="color: #66cc66;">&#41;</span> downKey <span style="color: #66cc66;">=</span> true;</span><br />
<span style="color: #009900;"> &nbsp;if <span style="color: #66cc66;">&#40;</span>e.keyCode <span style="color: #66cc66;">==</span> <span style="color: #cc66cc;">88</span> &amp;&amp; lasers.length &lt;<span style="color: #66cc66;">=</span> laserTotal<span style="color: #66cc66;">&#41;</span> lasers.push<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>ship_x + <span style="color: #cc66cc;">25</span>, ship_y - <span style="color: #cc66cc;">20</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;</span><br />
<br />
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span><br />
<br />
<span style="color: #009900;">function keyUp<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;if <span style="color: #66cc66;">&#40;</span>e.keyCode <span style="color: #66cc66;">==</span> <span style="color: #cc66cc;">39</span><span style="color: #66cc66;">&#41;</span> rightKey <span style="color: #66cc66;">=</span> false;</span><br />
<span style="color: #009900;"> &nbsp;else if <span style="color: #66cc66;">&#40;</span>e.keyCode <span style="color: #66cc66;">==</span> <span style="color: #cc66cc;">37</span><span style="color: #66cc66;">&#41;</span> leftKey <span style="color: #66cc66;">=</span> false;</span><br />
<span style="color: #009900;"> &nbsp;if <span style="color: #66cc66;">&#40;</span>e.keyCode <span style="color: #66cc66;">==</span> <span style="color: #cc66cc;">38</span><span style="color: #66cc66;">&#41;</span> upKey <span style="color: #66cc66;">=</span> false;</span><br />
<span style="color: #009900;"> &nbsp;else if <span style="color: #66cc66;">&#40;</span>e.keyCode <span style="color: #66cc66;">==</span> <span style="color: #cc66cc;">40</span><span style="color: #66cc66;">&#41;</span> downKey <span style="color: #66cc66;">=</span> false;</span><br />
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span><br />
<br />
<span style="color: #009900;">window.<span style="color: #000066;">onload</span> <span style="color: #66cc66;">=</span> init;</span><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-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Build a vertical scrolling shooter game with HTML5 canvas &#8211; Part 1</title>
		<link>http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-1/</link>
		<comments>http://atomicrobotdesign.com/blog/htmlcss/build-a-vertical-scrolling-shooter-game-with-html5-canvas-part-1/#comments</comments>
		<pubDate>Mon, 07 Feb 2011 03:03:06 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[HTML/CSS]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[shooter]]></category>

		<guid isPermaLink="false">http://atomicrobotdesign.com/blog/?p=884</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-1/">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>I’ve always found that, for me at least, one of the best ways to learn a new programming language is to make a game. It’s how I learned ActionScript 2 and then ActionScript 3 and it’s really helped me learn JavaScript. I’ve become a bit obsessed with the canvas tag over the last couple of weeks and I really wanted to see how easy or difficult to create a game with it compared to Flash. To see how difficult it is, we’ll build a vertical scrolling shooter game.</p>
<p>We’re going to start off by creating a ship that we can move around and then five enemy ships that move from the top to the bottom of the canvas. We won’t worry about game graphics in this part, we just want to get the functionality down and then build on that. I’m not going to worry about compatability with browsers like IE8, if the canvas doesn’t work in a browser, that’s it’s problem. This is just an exercise in learning how to make something that will work with a modern browser.</p>
<p>Let’s get started. We only need one line of HTML:</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;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></div></td></tr></tbody></table></div>
<p>And a little bit of CSS so we can see what’s going on:</p>
<div class="codecolorer-container css 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="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">body <span style="color: #00AA00;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#666</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
canvas <span style="color: #00AA00;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span><span style="color: #993333;">block</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #933;">30px</span> <span style="color: #993333;">auto</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">1px</span> <span style="color: #993333;">dashed</span> <span style="color: #cc00cc;">#ccc</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span></div></td></tr></tbody></table></div>
<p>Now, let’s get onto the part that will make this game run, the JavaScript. First, we’ll get our ship working, so we need to set up some 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 />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;">var</span> canvas<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; ctx<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; width <span style="color: #339933;">=</span> <span style="color: #CC0000;">600</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; height <span style="color: #339933;">=</span> <span style="color: #CC0000;">600</span><span style="color: #339933;">,</span><br />
&nbsp; &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;">50</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>I’m not going to go over the basics of the canvas tag. If you don’t know how it works, you can learn more here. The canvas and ctx variables are for find in the canvas and then drawing on it, width and height are the canvas dimensions. The ship variables are for both the initial positioning and dimensions of the square that’s going to represent the ship for now. Next let’s write the code that will actually draw the ship block on the canvas.</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 /></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><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><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>width<span style="color: #339933;">,</span>height<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> drawShip<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;">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>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: #009900;">&#125;</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; 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><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<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; drawShip<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
window.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> init<span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>So this code will get us <a href="http://atomicrobotdesign.com/blog_media/canvas_game/Part_1/part_1_ship.html">this</a>. A green block near the bottom of the canvas. Let’s go through the functions and see what’s happening here. The clearCanvas function is the something that we have to have in order for this to work. We have to redraw the canvas every time we we run the game loop and we need to clear everything on the canvas first or else we’ll get multiple copies of the ship on the canvas. So we clear it using the clearRect function and we set it to clear the entire canvas. Next we have the drawShip function where we set the fillStyle to green and then use fillRect to set the ship’s x and y positions and it’s width and height. The init function is what we use with the window.onload function at the bottom to get everything started. We get the canvas element, then get it’s context and finally, we use setInterval to call the gameLoop function every 25 milliseconds. With the gameLoop function we call clearCanvas and drawShip. This is the heart of our game, the init function sets everything up and then gameLoop makes it run.</p>
<p>Now, let’s make our ship move. Add these 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 />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">rightKey <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span><br />
leftKey <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span><br />
upKey <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span><br />
downKey <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span></div></td></tr></tbody></table></div>
<p>Then change the init 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 /></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> 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; 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><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></div></td></tr></tbody></table></div>
<p>We’ve added event listeners to listen for both keys being pressed down and keys being released. Now add these two functions:</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> 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></div></td></tr></tbody></table></div>
<p>Here, we are checking to see which key is being pressed and if it’s one of the arrow keys, then we change the corresponding variable to true and when that arrow key is released, we change the variable back to false. This will all set up the movement of the ship. Now, change the drawShip function to this:</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> drawShip<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> ship_x <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> ship_x <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> ship_y <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> ship_y <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>ship_x <span style="color: #339933;">&lt;=</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> ship_x <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</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: #009900;">&#40;</span>ship_x <span style="color: #339933;">+</span> ship_w<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> width<span style="color: #009900;">&#41;</span> ship_x <span style="color: #339933;">=</span> width <span style="color: #339933;">-</span> ship_w<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ship_y <span style="color: #339933;">&lt;=</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> ship_y <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</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: #009900;">&#40;</span>ship_y <span style="color: #339933;">+</span> ship_h<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> height<span style="color: #009900;">&#41;</span> ship_y <span style="color: #339933;">=</span> height <span style="color: #339933;">-</span> ship_h<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>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: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Now we are checking to see if the left or right arrow key is pressed and if so, move the ship 5 pixels in that direction, the same with the up and down arrows. At this point, we&#8217;ll have <a href="http://atomicrobotdesign.com/blog_media/canvas_game/Part_1/part_1_movement.html">this</a>. Ok, now let’s add some enemies to our game. First add these variables to the ones at the top of the page:</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">enemyTotal <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span><br />
enemies <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
enemy_x <span style="color: #339933;">=</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">,</span><br />
enemy_y <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">45</span><span style="color: #339933;">,</span><br />
enemy_w <span style="color: #339933;">=</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">,</span><br />
enemy_h <span style="color: #339933;">=</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">,</span><br />
speed <span style="color: #339933;">=</span> <span style="color: #CC0000;">3</span></div></td></tr></tbody></table></div>
<p>We are setting the total number of enemies, the array we’ll store our enemies in, the positioning of the first enemy, the width and height and finally, the speed our enemies will move at. Now, we’ll use a for loop to add our enemies to our enemies array:</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: #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> enemyTotal<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; enemies.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>enemy_x<span style="color: #339933;">,</span> enemy_y<span style="color: #339933;">,</span> enemy_w<span style="color: #339933;">,</span> enemy_h<span style="color: #339933;">,</span> speed<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; enemy_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 />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>With the loop we’re also increasing the x position of each enemy by 60 in order to spread them out across the canvas. Next, we have to actually draw our enemies onto the canvas:</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> drawEnemies<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;">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; ctx.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#f00'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; ctx.<span style="color: #660066;">fillRect</span><span style="color: #009900;">&#40;</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> 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_w<span style="color: #339933;">,</span> enemy_h<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>We just run another for loop and draw an enemy for each enemy that’s in the enemies array. And as it runs through the entire array, it uses the individual x and y positions of each enemy in the array. This is important now for the x position of each enemy and will be import down the road when we want to replace dead enemies. Next, let’s get the enemies to move.</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> moveEnemies<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;">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>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;">&lt;</span> height<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &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> enemies<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &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>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;">&gt;</span> height <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &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; <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>With this function, we’re running a for loop to move each enemy and checking to see if they’ve passed beyond the bottom of the canvas. If they have, then we move them back to above the top of the canvas, so it seems like an new enemy has entered. We still have one more thing to do, we need to change the gameLoop function to actually call these functions.</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> 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; moveEnemies<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; drawEnemies<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; drawShip<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, you should have <a href="http://atomicrobotdesign.com/blog_media/canvas_game/Part_1/part_1.html">this</a>. We’ve got the basics of our game and next time, we’ll change the colored boxes to actual ship graphics and give our ship the ability to shoot lasers. Here’s the complete code:</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 /></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 Shooter Game Tutorial Part 1<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 = 50,<br />
&nbsp; &nbsp; speed = 3,<br />
&nbsp; &nbsp; rightKey = false,<br />
&nbsp; &nbsp; leftKey = false,<br />
&nbsp; &nbsp; upKey = false,<br />
&nbsp; &nbsp; downKey = false,<br />
&nbsp; &nbsp; ship_x = (width / 2) - 25, ship_y = height - 75, ship_w = 50, ship_h = 50;<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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.fillStyle <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'#f00'</span>;</span><br />
<span style="color: #009900;"> &nbsp; &nbsp;ctx.fillRect<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;">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>, enemy_w, enemy_h<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.fillStyle = '#0f0';<br />
&nbsp; ctx.fillRect(ship_x, ship_y, ship_w, ship_h);<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 init() {<br />
&nbsp; canvas = document.getElementById('canvas');<br />
&nbsp; ctx = canvas.getContext('2d');<br />
&nbsp; setInterval(gameLoop, 25);<br />
&nbsp; document.addEventListener('keydown', keyDown, false);<br />
&nbsp; document.addEventListener('keyup', keyUp, false);<br />
}<br />
function gameLoop() {<br />
&nbsp; clearCanvas();<br />
&nbsp; moveEnemies();<br />
&nbsp; drawEnemies();<br />
&nbsp; drawShip();<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 />
}<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-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Sometimes it’s the simpler things (Or how jQuery has warped my brain)</title>
		<link>http://atomicrobotdesign.com/blog/htmlcss/sometimes-its-the-simpler-things-or-how-jquery-has-warped-my-brain/</link>
		<comments>http://atomicrobotdesign.com/blog/htmlcss/sometimes-its-the-simpler-things-or-how-jquery-has-warped-my-brain/#comments</comments>
		<pubDate>Wed, 26 Jan 2011 03:00:08 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[HTML/CSS]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://atomicrobotdesign.com/blog/?p=863</guid>
		<description><![CDATA[I was reading an interview on .net magazine with Dan Cederholm, the creator of Dribbble among other things. Dan runs a studio called SimpleBits and after reading the interview, I went to his site to check it out. For some &#8230; <a href="http://atomicrobotdesign.com/blog/htmlcss/sometimes-its-the-simpler-things-or-how-jquery-has-warped-my-brain/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I was reading an interview on <a href="http://www.netmag.co.uk/">.net magazine</a> with <a href="http://www.netmag.co.uk/zine/discover-interview/dan-cederholm">Dan Cederholm</a>, the creator of <a href="http://dribbble.com/">Dribbble</a> among other things. Dan runs a studio called <a href="http://simplebits.com/">SimpleBits</a> and after reading the interview, I went to his site to check it out. For some reason, I resized my browser window and I noticed something. If you change the width of your browser, the background of the SimpleBits website moves in the opposite direction.</p>
<p>I thought, “How is that done? It must be jQuery or something.” There’s jQuery being used on the site, but it’s simple stuff that is used for some hover animations and class changes, but nothing about positioning the background. So I decided to check out the CSS. And I was a bit surprised to see how simple the effect was to produce.</p>
<div class="codecolorer-container css 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="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">background.jpg</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">repeat</span> <span style="color: #933;">-50%</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333;">fixed</span><span style="color: #00AA00;">;</span></div></td></tr></tbody></table></div>
<p>To create the effect, all you need to do is set the background’s x position to negative 50% and then when the browser is resized, it will move in the opposite direction. After I saw that, I realized, of course, that’s how you would do it, but I think because I’m so used to see effects done with jQuery or CSS3, that it didn’t cross my mind that it could be this simple.</p>
<p>I don’t know if it’s a bad thing that my first guess was jQuery, but I think that it’s reminded me, that while things like jQuery are great, sometimes there’s a simpler way to do it. If I can do it with CSS and not use JavaScript, then that’s been the way that I’ve tried to go. I’ve been really excited about CSS3 and what it lets me do, but I need to remember that we can still do cool things with the CSS that works in all browsers. And when it’s something as simple as that, it makes it even more interesting.</p>
]]></content:encoded>
			<wfw:commentRss>http://atomicrobotdesign.com/blog/htmlcss/sometimes-its-the-simpler-things-or-how-jquery-has-warped-my-brain/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

