<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Aditya Ravi Shankar</title>
	
	<link>http://www.adityaravishankar.com</link>
	<description>Journey to Perfection</description>
	<lastBuildDate>Wed, 01 Feb 2012 11:23:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/AdityaRaviShankar" /><feedburner:info uri="adityaravishankar" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>AdityaRaviShankar</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>HTML5 Game Development: Using sprite sheets for better performance (and protecting your server)</title>
		<link>http://feedproxy.google.com/~r/AdityaRaviShankar/~3/IsYWSVWoARk/</link>
		<comments>http://www.adityaravishankar.com/2012/01/html5-game-development-using-sprite-sheets-for-better-performance/#comments</comments>
		<pubDate>Tue, 31 Jan 2012 21:27:39 +0000</pubDate>
		<dc:creator>Adi</dc:creator>
				<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[game development]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.adityaravishankar.com/?p=550</guid>
		<description><![CDATA[When I started developing the HTML5 version of Command and Conquer, I never expected more than a few close friends to look at the game. (Based on the experience with my last game, Breakout). So while I spent a lot &#8230; <a href="http://www.adityaravishankar.com/2012/01/html5-game-development-using-sprite-sheets-for-better-performance/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>When I started developing the <a href="http://www.adityaravishankar.com/2011/11/command-and-conquer-programming-an-rts-game-in-html5-and-javascript/" title="Command and Conquer – Programming an RTS game in HTML5 and Javascript">HTML5 version of Command and Conquer</a>, I never expected more than a few close friends to look at the game. (Based on the experience with <a href="http://www.adityaravishankar.com/2011/10/javascript-game-development-html5/" title="Javascript Game Development with HTML5 Canvas – Breakout released">my last game, Breakout</a>). </p>
<p><a href="http://www.adityaravishankar.com/projects/games/command-and-conquer"><img class="alignright size-thumbnail wp-image-483" title="Command and Conquer Javascript " src="http://www.adityaravishankar.com/wp-content/uploads/2011/11/Command-and-Conquer-Javascript-Screen-Shot-150x150.jpg" alt="Command and Conquer Javascript Screenshot" width="150" height="150" /></a>So while I spent a lot of time making the game look as close to the original Command and Conquer as possible, I never spent much time optimizing the code. Most of my development and testing was on my own Macbook, and I didn&#8217;t pay attention to the image loading time or network traffic. </p>
<p>Unfortunately, this game uses a lot of images. </p>
<p>Each unit can face 32 different directions.  This means at least 32 different images for each unit (more if there are animations like &#8216;harvesting&#8217;). </p>
<p>Buildings need a whole set of images for each state &#8211; under construction, regular &#8211; with different sets of images for healthy and damaged buildings, and for any additional states (like &#8216;unloading a harvester&#8217;). The Construction Yard for example needs 82 different images for it&#8217;s animations.</p>
<p><img src="http://www.adityaravishankar.com/wp-content/uploads/2012/01/network-before-sprite-sheets.png" alt="" title="network before sprite sheets" width="220" height="160" class="alignright size-full wp-image-564" />As a result, when the game loaded, it made nearly 1,000 HTTP requests to load all the assets (including images and sounds). </p>
<p>Since most browsers only make a few simultaneous requests at time, downloading all these images took a lot of time, with an overload of HTTP requests. </p>
<p>While this wasn&#8217;t a problem when I was testing the code locally, it was a bit of a pain when the code went onto the server. My patient friends ended up waiting for the game to load for 5-10 minutes (sometimes longer) before they could actually start playing. </p>
<p><a href="http://www.adityaravishankar.com/wp-content/uploads/2012/01/Traffic-AdityaRaviShankar.com-ScreenShot.png"><img src="http://www.adityaravishankar.com/wp-content/uploads/2012/01/Traffic-AdityaRaviShankar.com-ScreenShot-300x116.png" alt="AdityaRaviShankar.com - Traffic Screen Shot" title="AdityaRaviShankar.com - Traffic Screen Shot" width="300" height="116" class="alignright size-medium wp-image-553" /></a>The problem came when my game hit the front page of Hacker News and Wired.com on the same night, resulting in a slight spike in traffic <img src='http://www.adityaravishankar.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . It then got worse when the game got 1,000+ tweets and 1,000+ FB likes in just a few hours. The last time I checked, searching for my game gave over 5,000 google results.</p>
<p>What this meant was my shared hosting server was getting close to 14,000,000 HTTP requests in one day from just my domain. </p>
<p>I don&#8217;t think too many shared hosts are designed to take this kind of load, which resulted in my account being disabled with this interesting email from my provider.</p>
<blockquote><p>
Hello,</p>
<p>Your account adityaravishankar.com on the server quebec.unisonplatform.com was recently found to be causing high load that resulted in slowness/outages of various system services. In order to ensure quality of service to the other clients on this server we regret to inform you that the account had to be disabled to prevent any further interruption of service to our other clients.</p>
<p>If nothing can be done to lower the resource usage you may need to look into purchasing a dedicated server or cloud server which you can find more information about at http://www.eleven2.com.  Please contact us as soon as possible to resolve this issue. </p></blockquote>
<p>I was able to find temporary hosting on another server but the biggest priority was to optimize the way I stored images, which of course brings us to sprite sheets.</p>
<p><a href="http://www.adityaravishankar.com/wp-content/uploads/2012/01/hand-of-nod-sprite-sheet.png"><img src="http://www.adityaravishankar.com/wp-content/uploads/2012/01/hand-of-nod-sprite-sheet-150x72.png" alt="Hand of Nod Sprite Sheet" title="Hand of Nod Sprite Sheet" width="150" height="72" class="alignright size-thumbnail wp-image-556" /></a>Sprite sheets store all the sprites for an object in a single large image file. </p>
<p>When displaying the images, we calculate the offset of the sprite we want to show and use the ability of the drawImage() method to draw only a part of an image.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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 /></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: #006600; font-style: italic;">//Before: (Load individual images and store in a big array)</span><br />
<span style="color: #006600; font-style: italic;">// Three arguments: the element, destination (x,y) coordinates.</span><br />
<span style="color: #003366; font-weight: bold;">var</span> image <span style="color: #339933;">=</span> imageArray<span style="color: #009900;">&#91;</span>imageNumber<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>image<span style="color: #339933;">,</span>x<span style="color: #339933;">,</span>y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// After: (Load single sprite sheet image)</span><br />
<span style="color: #006600; font-style: italic;">// Nine arguments: the element, source (x,y) coordinates, </span><br />
<span style="color: #006600; font-style: italic;">// source width and height (for cropping), </span><br />
<span style="color: #006600; font-style: italic;">// destination (x,y) coordinates, and </span><br />
<span style="color: #006600; font-style: italic;">// destination width and height (resize).</span><br />
<br />
context.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">spriteImage</span><span style="color: #339933;">,</span> <br />
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">imageWidth</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>imageNumber<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">imageWidth</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">imageHeight</span><span style="color: #339933;">,</span> <br />
x<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">imageWidth</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">imageHeight</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Creating these sprite sheets is incredibly simple using <a href="http://www.imagemagick.org/script/montage.php" title="ImageMagick Montage Command Line Tool" target="_blank">ImageMagick&#8217;s montage command line tool</a>. This single command will convert a folder full of images into a single row sprite sheet.</p>
<div class="codecolorer-container text railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">montage -background transparent -tile x1 -geometry +0+0 construction-yard/*.gif construction-yard-sprite-sheet.png</div></td></tr></tbody></table></div>
<p>After comparing PNG and GIF, I found that PNGs tend to compress the sprite sheets a lot more. </p>
<p>The advantages of using Sprite Sheets?</p>
<ol>
<li>Fewer HTTP requests &#8211; The Command Center went from 81 requests to a SINGLE HTTP request</li>
<li>Better Compression &#8211; An advantage of storing the images in a single file is that the header information doesn&#8217;t repeat and the combined file&#8217;s size is much smaller than the sum of the individual files. The command center went from 496KB in 81 files to only 37KB in a single file. (Less than 8% of the original size, which is incredible) </li>
<li>Easier Manipulation &#8211; With all the sprites in a single image file, it became easier to do RGB color manipulations, and I was able to optimize the drawing code for performance.</li>
</ol>
<p><a href="http://www.adityaravishankar.com/wp-content/uploads/2012/01/network-after-sprite-sheets.png"><img src="http://www.adityaravishankar.com/wp-content/uploads/2012/01/network-after-sprite-sheets.png" alt="" title="network after sprite sheets" width="238" height="159" class="alignright size-full wp-image-563" /></a>From almost a 1,000 requests to 120 requests in one simple code rewrite. And the total download size went from a few MBs to around 200KB. </p>
<p>Game load time went from 10+ minutes to under a minute. The bandwidth usage dropped to a tenth of the original. The number of requests dropped to a tenth of the original. And now my shared hosting can survive a very decent amount of traffic. </p>
<p>Moral of the story? When developing a large game that is image heavy, track your network usage and when possible use sprite sheets <img src='http://www.adityaravishankar.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<p><a href="http://feedads.g.doubleclick.net/~a/1k0j0aT1obJRUU-mXD_sXOKDVz0/0/da"><img src="http://feedads.g.doubleclick.net/~a/1k0j0aT1obJRUU-mXD_sXOKDVz0/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/1k0j0aT1obJRUU-mXD_sXOKDVz0/1/da"><img src="http://feedads.g.doubleclick.net/~a/1k0j0aT1obJRUU-mXD_sXOKDVz0/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/AdityaRaviShankar/~4/IsYWSVWoARk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.adityaravishankar.com/2012/01/html5-game-development-using-sprite-sheets-for-better-performance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.adityaravishankar.com/2012/01/html5-game-development-using-sprite-sheets-for-better-performance/</feedburner:origLink></item>
		<item>
		<title>Command and Conquer – Programming an RTS game in HTML5 and Javascript</title>
		<link>http://feedproxy.google.com/~r/AdityaRaviShankar/~3/gctDC8di18I/</link>
		<comments>http://www.adityaravishankar.com/2011/11/command-and-conquer-programming-an-rts-game-in-html5-and-javascript/#comments</comments>
		<pubDate>Tue, 08 Nov 2011 11:00:33 +0000</pubDate>
		<dc:creator>Adi</dc:creator>
				<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[game development]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[pathfinding]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[real time strategy]]></category>

		<guid isPermaLink="false">http://www.adityaravishankar.com/?p=479</guid>
		<description><![CDATA[After writing my first HTML5 game I decided to take on a more challenging project. My latest project is a recreation of the original Command and Conquer entirely in HTML5 and Javascript. Command and Conquer is the grand-daddy of all &#8230; <a href="http://www.adityaravishankar.com/2011/11/command-and-conquer-programming-an-rts-game-in-html5-and-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>After writing <a title="Javascript Game Development with HTML5 Canvas – Breakout released" href="http://www.adityaravishankar.com/2011/10/javascript-game-development-html5/">my first HTML5 game</a> I decided to take on a more challenging project.</p>
<p><a href="http://www.adityaravishankar.com/projects/games/command-and-conquer"><img class="alignright size-thumbnail wp-image-483" title="Command and Conquer Javascript " src="http://www.adityaravishankar.com/wp-content/uploads/2011/11/Command-and-Conquer-Javascript-Screen-Shot-150x150.jpg" alt="Command and Conquer Javascript Screenshot" width="150" height="150" /></a>My latest project is a recreation of the original Command and Conquer entirely in HTML5 and Javascript.</p>
<p>Command and Conquer is the grand-daddy of all Real Time Strategy games and is probably the game that made the genre popular. My friends and I spent countless hours playing the original as well as subsequent sequels (Tiberian Sun, Red Alert).</p>
<p>Check out the demo video.</p>
<p><iframe width="584" height="438" src="http://www.youtube.com/embed/oPjHuFtnb2o?fs=1&#038;feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>You can play the actual game here. <a title="Command and Conquer - HTML5 version" href="http://www.adityaravishankar.com/projects/games/command-and-conquer/">Command and Conquer &#8211; HTML5 version</a></p>
<p>This project was a lot more challenging than<a title="Javascript Game Development with HTML5 Canvas – Breakout released" href="http://www.adityaravishankar.com/2011/10/javascript-game-development-html5/"> my previous game, Breakout</a>. In hindsight, I might have wanted to take smaller steps and make a tower defense game instead of jumping directly into an RTS.</p>
<p>Creating even this simple version of the game covered a lot of things.</p>
<ol>
<li>Using images to recreate the sidebar and game interface. </li>
<li>Using mouse input for unit selection, panning, attacking and user input</li>
<li>Using images as sprites for unit and building animation, and <a href="http://www.adityaravishankar.com/2012/01/html5-game-development-using-sprite-sheets-for-better-performance/" title="HTML5 Game Development: Using sprite sheets for better performance">using sprite sheets for better performance</a></li>
<li>A lot more sounds for units and buildings</li>
<li>Using a finite state machine for handling unit commands, movement, attacking etc.</li>
<li>Using path finding (A*) to navigate around obstructions like buildings, mountains and trees</li>
<li>Using hidden canvas&#8217;s for things like fog of war and image manipulation</li>
</ol>
<p>Obviously because of the size of the project, trying to do the whole thing in under a month all by myself wasn&#8217;t the smartest idea. I ended up spending lots of 18 hour work days during this time.</p>
<p>I used to switch between playing the original Tiberian Dawn in a Wine window on my Mac and my JS version on the browser to make sure my version looked EXACTLY like the original. I spent a LOT of time going through C&amp;C forums to understand how to reverse engineer the Tiberium Dawn files to extract the building/unit sprites and audio and reading up on unit specs to figure out how to make the units behave exactly like the original. </p>
<p>Every little thing took time &#8211; things like selecting single units, multiple units, being able to select by drawing the box from left to right or from right to left; making sure the panning was smooth; Figuring out a decent fog of war implementation; Allowing for building construction, dependencies (Power Plant needed for Refinery, which is needed for Factory), building placement (buildings cannot be constructed on top of other buildings); Depth sorting when drawing so units could move behind buildings and trees.</p>
<p>The next big headache was smooth unit movement. I wanted to do better than the original game, and spent a lot of time going through Starcraft 2 videos to see if I could get a smoother movement closer to the way SC2 handles it. Pathfinding using A* can do some of the work, but gives a clunky movement and causes units to take behave badly/take longer routes when going through bottle necks. I decided to mix A* with a repelling force/steering behavior to improve it. It still has some bugs I need to work out.</p>
<p>I had to fine tune the way units behaved (during patrolling, guarding, attacking). Even the fact that a MCV couldn&#8217;t attack and turned into buildings. </p>
<p>Harvesting was a whole different pain in the ass since my collision detection and steering code meant my harvester was initially afraid to return to the refinery <img src='http://www.adityaravishankar.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . </p>
<p>While a lot of people have been giving me props for just getting this project out, one of the things that I am proud of is that I completed the first version in around 3 and a half weeks, all by myself. </p>
<p>For people who have been criticizing this project, I&#8217;ll say this &#8211; <strong>&#8220;I did this, entirely on my own, unpaid, in three and a half weeks &#8211; including one week where I had a fever. Give me a break. It isn&#8217;t that bad <img src='http://www.adityaravishankar.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> &#8221;</strong>.</p>
<p>The project is far from complete and there is still some polishing up to do. However, since the underlying engine has been designed to use &#8216;units&#8217;, &#8216;buildings&#8217;, and &#8216;levels&#8217;, expanding the game and creating tons of levels and adding all the vehicles should be fairly simple.</p>
<p>Please do check out the demo and let me know what you think. <a title="Command and Conquer - HTML5 version" href="http://www.adityaravishankar.com/projects/games/command-and-conquer/">Command and Conquer &#8211; HTML5 version</a>. Any comments, feedback (including bugs), is appreciated.</p>
<p>NOTE: This game has got a lot of attention from online media and FB/Twitter/Google+. The extra traffic brought my server down on its knees and got my account temporarily suspended. This forced me to<a href="http://www.adityaravishankar.com/2012/01/html5-game-development-using-sprite-sheets-for-better-performance/" title="HTML5 Game Development: Using sprite sheets for better performance (and protecting your server)"> optimize the game using sprite sheets</a> so it could handle requests more efficiently. You can read more about the whole story here &#8211; <a href="http://www.adityaravishankar.com/2012/01/html5-game-development-using-sprite-sheets-for-better-performance/" title="HTML5 Game Development: Using sprite sheets for better performance (and protecting your server)">HTML5 Game Development: Using sprite sheets for better performance (and protecting your server)</a></p>
<p><a href="http://www.adityaravishankar.com/projects/games/pathfinding-javascript-rts-demo/"><img src="http://www.adityaravishankar.com/wp-content/uploads/2011/10/Real-Time-Strategy-Game-Pathfinding-Collision-Detection-Steering-Demo-Screenshot-300x257.jpg" alt="Real Time Strategy Game - Pathfinding, Collision Detection &amp; Steering Demo - Screenshot" title="Real Time Strategy Game - Pathfinding, Collision Detection &amp; Steering Demo - Screenshot" width="300" height="257" class="alignright size-medium wp-image-515" /></a>After lots of feedback, I&#8217;ve specifically researched and worked on improving the pathfinding of the game. Check out the <a href="http://www.adityaravishankar.com/projects/games/pathfinding-javascript-rts-demo/" title="Real Time Strategy Game - Pathfinding, Collision Detection &#038; Steering Demo - Screenshot">Pathfinding, Collision Detection &#038; Steering Demo</a>. This is a demonstration of the improved unit pathfinding and movement/steering strategies used in RTS games. Units use A* to plot a path and avoid collision using a &#8216;soft collision&#8217; radius. They also ask static units to move aside.</p>
<p>Here is a video demonstrating all the features of the new pathfinding code.</p>
<p><iframe width="584" height="438" src="http://www.youtube.com/embed/VMI2nLC7i5w?fs=1&#038;feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p><a href="http://www.adityaravishankar.com/projects/games/pathfinding-javascript-rts-demo/" title="RTS - Pathfinding, Collision Detection &#038; Steering Demo">Check out the demo here</a>. Switch on &#8220;DEBUG MODE&#8221; to see how it works. This is still a work in progress. Any comments or feedback (including bugs), is appreciated.</p>
<p>Check out some of the <a title="Games" href="http://www.adityaravishankar.com/software-projects/games/">HTML5 games I have written</a>. If you are just starting out with game programming, check out my earlier <a title="HTML5 Game Development Tutorial: Breakout Part I – Introduction to Canvas" href="http://www.adityaravishankar.com/2011/10/html5-game-development-tutorial-breakout-i-introduction-to-canvas/">tutorial on developing a simple Breakout game in HTML5</a>.</p>
<p>&#8212;&#8211;</p>
<p><strong>Command and Conquer &#8211; Source Code Released</strong><br />
<a href="https://github.com/adityaravishankar/command-and-conquer">Command And Conquer on Github</a></p>
<p>This code started out as a simple fun project. I learned a lot while doing this. And I think it was a cool proof of concept&#8230;</p>
<p>But it can be so much more. It can be a complete game, with better AI, multiplayer, better optimized code, and structured so people can contribute more easily- coders add code, level designers design levels&#8230;. Instead of one person trying to do everything &#8211; badly . </p>
<p>It would mean completely rewriting some of my crappy code (that I don&#8217;t think I can even look at any more &#8211; like pathfinding, level handling, <a href="http://www.adityaravishankar.com/2012/01/html5-game-development-using-sprite-sheets-for-better-performance/" title="HTML5 Game Development: Using sprite sheets for better performance (and protecting your server)">using sprite sheets</a>) and maybe designing with the bigger vision in mind.</p>
<p>I don&#8217;t think I have the courage or energy to try to do that on my own. But I think if a few of us got together, we could do something really cool with this.</p>
<p>For now&#8230; Here is the code.. Be gentle&#8230; I wrote the code in under a month, working 18 hour days, with a fever during a week in between. (Yes&#8230; that&#8217;s my excuse anytime you look at the code and go WTF????)</p>
<p>After taking a look, if you still want to work together, let me know&#8230; <img src='http://www.adityaravishankar.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p>

<p><a href="http://feedads.g.doubleclick.net/~a/ooanAmmaOEGP8ZV-kbsvIQo2vEc/0/da"><img src="http://feedads.g.doubleclick.net/~a/ooanAmmaOEGP8ZV-kbsvIQo2vEc/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/ooanAmmaOEGP8ZV-kbsvIQo2vEc/1/da"><img src="http://feedads.g.doubleclick.net/~a/ooanAmmaOEGP8ZV-kbsvIQo2vEc/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/AdityaRaviShankar/~4/gctDC8di18I" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.adityaravishankar.com/2011/11/command-and-conquer-programming-an-rts-game-in-html5-and-javascript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.adityaravishankar.com/2011/11/command-and-conquer-programming-an-rts-game-in-html5-and-javascript/</feedburner:origLink></item>
		<item>
		<title>HTML5 Game Development Tutorial: Breakout Part III – Collisions, Scoring and Sound</title>
		<link>http://feedproxy.google.com/~r/AdityaRaviShankar/~3/oMkaVnHC4xQ/</link>
		<comments>http://www.adityaravishankar.com/2011/10/html5-game-development-tutorial-breakout-part-iii-collisions-scoring-and-sound/#comments</comments>
		<pubDate>Tue, 25 Oct 2011 11:29:09 +0000</pubDate>
		<dc:creator>Adi</dc:creator>
				<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[audio]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[game development]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[pool]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.adityaravishankar.com/?p=453</guid>
		<description><![CDATA[This is the third in a series of tutorials on developing a full featured version of Breakout. In the second tutorial, we used setInterval() to add some animation and capture keydown and keyup events to respond to user input. In &#8230; <a href="http://www.adityaravishankar.com/2011/10/html5-game-development-tutorial-breakout-part-iii-collisions-scoring-and-sound/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.adityaravishankar.com/games/breakout"><img src="http://www.adityaravishankar.com/wp-content/uploads/2011/10/Breakout-v0.6-Screenshot-240x300.png" alt="Breakout v0.6 Screenshot" title="Breakout v0.6 Screenshot" width="240" height="300" class="alignright size-medium wp-image-504" /></a>This is the third in a series of tutorials on developing a <a href="http://www.adityaravishankar.com/2011/10/javascript-game-development-html5/" title="Javascript Game Development with HTML5 Canvas – Breakout released">full featured version of Breakout</a>. </p>
<p>In <a href="http://www.adityaravishankar.com/2011/10/html5-game-development-tutorial-breakout-part-ii-%e2%80%93-animation-and-keyboard-input/" title="HTML5 Game Development Tutorial: Breakout Part II – Animation and Keyboard Input">the second tutorial</a>, we used <strong>setInterval()</strong> to add some animation and capture <strong>keydown</strong> and <strong>keyup</strong> events to respond to user input.</p>
<p>In this tutorial, we will bring back the bricks we drew before, handle the ball colliding with these bricks, and finally increase the score when a brick gets destroyed. We will also add some sound effects to make the game more fun.</p>
<p>We will use the code from the last tutorial as our starting point. <a href="http://www.adityaravishankar.com/downloads/breakout2.html.zip">HTML5 Game Development Tutorial: Breakout Part II - Source Code</a>
<p style="color:brown;">Note: If you found this article useful enough to download the code above, then please do take the time to share this article with others using the Like/Tweet button.
</p></p>
<p>Lets get started. </p>
<p>First we add back two lines at the beginning of the <strong>animate()</strong> function to draw the scoreboard and the bricks.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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> animate <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; context.<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>canvas.<span style="color: #660066;">width</span><span style="color: #339933;">,</span>canvas.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; createBricks<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; displayScoreBoard<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Right now the ball will move over the bricks without bouncing off them. </p>
<p>We need the ball to reverse X direction if it hits the sides of the bricks, reverse Y direction if it hits the top or bottom of the bricks and damage the bricks every time it touches them.</p>
<p>Lets create functions to check whether the ball is colliding with a brick along the X or Y direction, and to damage the brick if it collides.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><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 /></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> collisionXWithBricks<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> bumpedX <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>&nbsp; &nbsp; <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> bricks.<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; &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> bricks<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<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; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>bricks<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// if brick is still visible</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> brickX <span style="color: #339933;">=</span> j <span style="color: #339933;">*</span> brickWidth<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> brickY <span style="color: #339933;">=</span> i <span style="color: #339933;">*</span> brickHeight<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// barely touching from left</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>ballX <span style="color: #339933;">+</span> ballDeltaX <span style="color: #339933;">+</span> ballRadius <span style="color: #339933;">&gt;=</span> brickX<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span>ballX <span style="color: #339933;">+</span> ballRadius <span style="color: #339933;">&lt;=</span> brickX<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">||</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// barely touching from right</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>ballX <span style="color: #339933;">+</span> ballDeltaX <span style="color: #339933;">-</span> ballRadius<span style="color: #339933;">&lt;=</span> brickX <span style="color: #339933;">+</span> brickWidth<span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;&amp;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span>ballX <span style="color: #339933;">-</span> ballRadius <span style="color: #339933;">&gt;=</span> brickX <span style="color: #339933;">+</span> brickWidth<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>ballY <span style="color: #339933;">+</span> ballDeltaY <span style="color: #339933;">-</span>ballRadius<span style="color: #339933;">&lt;=</span> brickY <span style="color: #339933;">+</span> brickHeight<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span>ballY <span style="color: #339933;">+</span> ballDeltaY <span style="color: #339933;">+</span> ballRadius <span style="color: #339933;">&gt;=</span> brickY<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// weaken brick and increase score</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; explodeBrick<span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span>j<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bumpedX <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> bumpedX<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> collisionYWithBricks<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> bumpedY <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: #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> bricks.<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; &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> bricks<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<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; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>bricks<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// if brick is still visible</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> brickX <span style="color: #339933;">=</span> j <span style="color: #339933;">*</span> brickWidth<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> brickY <span style="color: #339933;">=</span> i <span style="color: #339933;">*</span> brickHeight<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// barely touching from below</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>ballY <span style="color: #339933;">+</span> ballDeltaY <span style="color: #339933;">-</span> ballRadius <span style="color: #339933;">&lt;=</span> brickY <span style="color: #339933;">+</span> brickHeight<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span>ballY <span style="color: #339933;">-</span> ballRadius <span style="color: #339933;">&gt;=</span> brickY <span style="color: #339933;">+</span> brickHeight<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">||</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// barely touching from above</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>ballY <span style="color: #339933;">+</span> ballDeltaY <span style="color: #339933;">+</span> ballRadius <span style="color: #339933;">&gt;=</span> brickY<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span>ballY <span style="color: #339933;">+</span> ballRadius <span style="color: #339933;">&lt;=</span> brickY <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ballX <span style="color: #339933;">+</span> ballDeltaX <span style="color: #339933;">+</span> ballRadius <span style="color: #339933;">&gt;=</span> brickX <span style="color: #339933;">&amp;&amp;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ballX <span style="color: #339933;">+</span> ballDeltaX <span style="color: #339933;">-</span> ballRadius<span style="color: #339933;">&lt;=</span> brickX <span style="color: #339933;">+</span> brickWidth<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// weaken brick and increase score</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; explodeBrick<span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span>j<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bumpedY <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &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;">return</span> bumpedY<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> explodeBrick<span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span>j<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// First weaken the brick (0 means brick has gone)</span><br />
&nbsp; &nbsp; bricks<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">--;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>bricks<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><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; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// The brick is weakened but still around. Give a single point.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; score<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: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// give player an extra point when the brick disappears</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; score <span style="color: #339933;">+=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span> &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>The <strong>collisionXWithBricks()</strong> and <strong>collisionYWithBricks()</strong> functions return true when they bounce against a brick and automatically call <strong>explodeBrick()</strong> to weaken the brick and update the score. To start using these functions, we modify the collision checking conditions in <strong>moveBall()</strong> slightly.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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 /></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: #006600; font-style: italic;">// If top of the ball touches the top then reverse Y direction</span><br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ballY <span style="color: #339933;">+</span> ballDeltaY <span style="color: #339933;">-</span> ballRadius <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0</span> <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// or if ball collides in Y direction with bricks</span><br />
&nbsp; &nbsp; <span style="color: #339933;">||</span> collisionYWithBricks<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; ballDeltaY <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>ballDeltaY<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// If side of ball touches either side of the wall then reverse X direction</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//left of ball moves too far left</span><br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>ballX <span style="color: #339933;">+</span> ballDeltaX <span style="color: #339933;">-</span> ballRadius <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//or right side of ball moves too far right</span><br />
<span style="color: #009900;">&#40;</span>ballX <span style="color: #339933;">+</span> ballDeltaX <span style="color: #339933;">+</span> ballRadius <span style="color: #339933;">&gt;</span> canvas.<span style="color: #660066;">width</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #006600; font-style: italic;">// or if ball collides in Y direction with bricks</span><br />
<span style="color: #339933;">||</span> collisionXWithBricks<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; ballDeltaX <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>ballDeltaX<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>At this point, the ball bounces off the bricks as expected and weakens the brick when it hits them. Once the brick is completely destroyed, it disappears and gives the player an extra points. </p>
<p>Not bad. The game works fine, however there is still something missing. </p>
<p>A little sound tends to give players a much more immersive experience and will make the game more fun. With HTML5 Audio, implementing sound is surprisingly easy.</p>
<p>To play sounds, we load files using the <strong>Audio()</strong> object and play them using its <strong>play()</strong> method. Browsers are still a little inconsistent about which formats they support. The OGG file format is an open format supported by both Chrome and Firefox (my favorites), which is why we will stick with them for now.</p>
<p>To create the sound we place the two files at the same location as the HTML file and load them by creating new <strong>Audio()</strong> objects and storing them in variables. The OGG sound files used here are included with the source code.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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"><span style="color: #003366; font-weight: bold;">var</span> bouncingSound <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Audio<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;bounce.ogg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> breakingSound <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Audio<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;break.ogg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>To play these sounds, all we need to do is invoke the <strong>play()</strong> method.</p>
<p>We can play the breaking sound whenever a brick is completely destroyed (inside the <strong>explodeBrick()</strong> function).</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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 /></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> explodeBrick<span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span>j<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// First weaken the brick (0 means brick has gone)</span><br />
&nbsp; &nbsp; bricks<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">--;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>bricks<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><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; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// The brick is weakened but still around. Give a single point.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; score<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: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// give player an extra point when the brick disappears</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; score <span style="color: #339933;">+=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; breakingSound.<span style="color: #660066;">play</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>And the bounce sound anytime we change X or Y direction for the ball (inside the <strong>moveBall()</strong> function)</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><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 /></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> moveBall<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<span style="color: #006600; font-style: italic;">// First check if we will bump into something</span><br />
<br />
<span style="color: #006600; font-style: italic;">// If top of the ball touches the top then reverse Y direction</span><br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ballY <span style="color: #339933;">+</span> ballDeltaY <span style="color: #339933;">-</span> ballRadius <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0</span> <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// or if ball collides in Y direction with bricks</span><br />
&nbsp; &nbsp; <span style="color: #339933;">||</span> collisionYWithBricks<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; ballDeltaY <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>ballDeltaY<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; bouncingSound.<span style="color: #660066;">play</span><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 />
<span style="color: #006600; font-style: italic;">// If the bottom of the ball touches the bottom of the screen then end the game</span><br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ballY <span style="color: #339933;">+</span> ballDeltaY <span style="color: #339933;">+</span> ballRadius <span style="color: #339933;">&gt;</span> canvas.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; endGame<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 />
<span style="color: #006600; font-style: italic;">// If side of ball touches either side of the wall then reverse X direction</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//left of ball moves too far left</span><br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>ballX <span style="color: #339933;">+</span> ballDeltaX <span style="color: #339933;">-</span> ballRadius <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//or right side of ball moves too far right</span><br />
<span style="color: #009900;">&#40;</span>ballX <span style="color: #339933;">+</span> ballDeltaX <span style="color: #339933;">+</span> ballRadius <span style="color: #339933;">&gt;</span> canvas.<span style="color: #660066;">width</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #006600; font-style: italic;">// or if ball collides in Y direction with bricks</span><br />
<span style="color: #339933;">||</span> collisionXWithBricks<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; ballDeltaX <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>ballDeltaX<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; bouncingSound.<span style="color: #660066;">play</span><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 />
<span style="color: #006600; font-style: italic;">// if bottom of ball reaches the top of paddle,</span><br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ballY <span style="color: #339933;">+</span> ballDeltaY <span style="color: #339933;">+</span> ballRadius <span style="color: #339933;">&gt;=</span> paddleY<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// and it is positioned between the two ends of the paddle (is on top)</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ballX <span style="color: #339933;">+</span> ballDeltaX <span style="color: #339933;">&gt;=</span> paddleX <span style="color: #339933;">&amp;&amp;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; ballX <span style="color: #339933;">+</span> ballDeltaX <span style="color: #339933;">&lt;=</span> paddleX <span style="color: #339933;">+</span> paddleWidth<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ballDeltaY <span style="color: #339933;">=</span> <span style="color: #339933;">-</span> ballDeltaY<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; bouncingSound.<span style="color: #660066;">play</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// Move the ball</span><br />
ballX <span style="color: #339933;">=</span> ballX <span style="color: #339933;">+</span> ballDeltaX<span style="color: #339933;">;</span><br />
ballY <span style="color: #339933;">=</span> ballY <span style="color: #339933;">+</span> ballDeltaY<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>That&#8217;s really all there is to it. We have a game with sound effects, animation, keyboard control and increasing scores written entirely in HTML and Javascript. </p>
<p>To make the game more fun, we can add more interesting brick types, sounds and game mechanics. </p>
<p>In <a href="http://www.adityaravishankar.com/games/breakout" title="Breakout Game" target="_blank">my version of the game</a>, the bricks start falling down, we have Cash bricks to give money, Bomb bricks that explode everything nearby, Laser Ammo bricks that let us shoot laser from the paddle and Spider bricks that shriek and start crawling down at the user. <a href="http://www.adityaravishankar.com/games/breakout" title="Breakout Game" target="_blank">Check out the game</a> to see what you think. Leave a comment if you enjoy the game.</p>
<p>I don&#8217;t see much point showing people how to implement the exact same features in a game, because at this point it is fairly simple to do. However if you have an interesting idea that you would like to add to the game, leave me a comment below. If we get a few interesting ideas, we can have one more tutorial where we implement the ideas and create our own new version of the game.</p>
<p>If not, stay tuned for my next tutorial series on how to implement a Real Time Strategy game engine entirely in HTML5 and Javascript. Check out <a href="http://www.adityaravishankar.com/2011/11/command-and-conquer-programming-an-rts-game-in-html5-and-javascript/" title="Command and Conquer – Programming an RTS game in HTML5 and Javascript">Command and Conquer – Programming an RTS game in HTML5 and Javascript</a>.</p>
<p>You can download the finished source code for this tutorial below.<br />
<a href="http://www.adityaravishankar.com/downloads/breakout3.html.zip">HTML5 Game Development Tutorial: Breakout Part III - Source Code</a>
<p style="color:brown;">Note: If you found this article useful enough to download the code above, then please do take the time to share this article with others using the Like/Tweet button.
</p></p>

<p><a href="http://feedads.g.doubleclick.net/~a/O9MKEiJaGWwBG6eF-A3xP6r5cUI/0/da"><img src="http://feedads.g.doubleclick.net/~a/O9MKEiJaGWwBG6eF-A3xP6r5cUI/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/O9MKEiJaGWwBG6eF-A3xP6r5cUI/1/da"><img src="http://feedads.g.doubleclick.net/~a/O9MKEiJaGWwBG6eF-A3xP6r5cUI/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/AdityaRaviShankar/~4/oMkaVnHC4xQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.adityaravishankar.com/2011/10/html5-game-development-tutorial-breakout-part-iii-collisions-scoring-and-sound/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.adityaravishankar.com/2011/10/html5-game-development-tutorial-breakout-part-iii-collisions-scoring-and-sound/</feedburner:origLink></item>
		<item>
		<title>HTML5 Game Development Tutorial: Breakout Part II – Animation and Keyboard Input</title>
		<link>http://feedproxy.google.com/~r/AdityaRaviShankar/~3/HXAHLW7wahA/</link>
		<comments>http://www.adityaravishankar.com/2011/10/html5-game-development-tutorial-breakout-part-ii-animation-and-keyboard-input/#comments</comments>
		<pubDate>Fri, 14 Oct 2011 07:56:06 +0000</pubDate>
		<dc:creator>Adi</dc:creator>
				<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[game development]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.adityaravishankar.com/?p=425</guid>
		<description><![CDATA[This is the second in a series of tutorials on developing a full featured version of Breakout. In the first tutorial we covered drawing primitive objects on the screen using the Canvas element and developing the basic game screen. In &#8230; <a href="http://www.adityaravishankar.com/2011/10/html5-game-development-tutorial-breakout-part-ii-animation-and-keyboard-input/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.adityaravishankar.com/games/breakout"><img src="http://www.adityaravishankar.com/wp-content/uploads/2011/10/Breakout-v0.6-Screenshot-240x300.png" alt="Breakout v0.6 Screenshot" title="Breakout v0.6 Screenshot" width="240" height="300" class="alignright size-medium wp-image-504" /></a>This is the second in a series of tutorials on developing a <a href="http://www.adityaravishankar.com/2011/10/javascript-game-development-html5/" title="Javascript Game Development with HTML5 Canvas – Breakout released">full featured version of Breakout</a>. </p>
<p>In <a href="http://www.adityaravishankar.com/2011/10/html5-game-development-tutorial-breakout-i-introduction-to-canvas/" title="HTML5 Game Development Tutorial: Breakout Part I – Introduction to Canvas">the first tutorial</a> we covered drawing primitive objects on the screen using the Canvas element and developing the basic game screen.</p>
<p>In this tutorial, we will use <strong>setInterval()</strong> to add some animation and capture <strong>keydown</strong> and <strong>keyup</strong> events to respond to user input (so we can move the paddle). </p>
<p>We will use the code from the last tutorial as our starting point. <a href="http://www.adityaravishankar.com/downloads/breakout1.html.zip">HTML5 Game Development Tutorial: Breakout Part I  - Source Code</a>
<p style="color:brown;">Note: If you found this article useful enough to download the code above, then please do take the time to share this article with others using the Like/Tweet button.
</p></p>
<p>Lets get started. </p>
<p>To animate objects in the game, we need to draw them, move them a little, and then redraw them. To do this, we create an <strong>animate()</strong> function and place our calls to the drawing functions inside it. For now, lets focus on only the paddle and the ball.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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> animate <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; drawPaddle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; drawBall<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 we need to figure out a way to call <strong>animate()</strong> repeatedly at regular intervals. Luckily, Javascript has a function called <strong>setInterval()</strong> which keeps calling a given function repeatedly at fixed time intervals until the <strong>clearInterval()</strong> function is called.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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> startGame<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// call the animate() function every 20ms until clearInterval(gameLoop) is called</span><br />
&nbsp; &nbsp; gameLoop <span style="color: #339933;">=</span> setInterval<span style="color: #009900;">&#40;</span>animate<span style="color: #339933;">,</span><span style="color: #CC0000;">20</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> endGame<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; clearInterval<span style="color: #009900;">&#40;</span>gameLoop<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; context.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'The End!!!!'</span><span style="color: #339933;">,</span>canvas.<span style="color: #660066;">width</span><span style="color: #339933;">/</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span>canvas.<span style="color: #660066;">height</span><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><br />
<br />
startGame<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 create two functions called <strong>startGame()</strong> and <strong>endGame()</strong> to begin the animation and end it. We can also use <strong>startGame()</strong> to do any pre-game initialization of variables.</p>
<p>Now that the game loop is setup, we need to start moving the elements during the loop.<br />
We start by creating a <strong>moveBall()</strong> function and calling it from within <strong>animate()</strong>.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><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;">var</span> ballDeltaX<span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> ballDeltaY<span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> moveBall<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Move the ball</span><br />
&nbsp; &nbsp; ballX <span style="color: #339933;">=</span> ballX <span style="color: #339933;">+</span> ballDeltaX<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; ballY <span style="color: #339933;">=</span> ballY <span style="color: #339933;">+</span> ballDeltaY<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> animate <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; moveBall<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; drawPaddle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; drawBall<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 />
<span style="color: #003366; font-weight: bold;">function</span> startGame<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; ballDeltaY <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">4</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; ballDeltaX <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">2</span><span style="color: #339933;">;</span><br />
<span style="color: #006600; font-style: italic;">// call the animate() function every 200ms until clearInterval(gameLoop) is called</span><br />
&nbsp; &nbsp; gameLoop <span style="color: #339933;">=</span> setInterval<span style="color: #009900;">&#40;</span>animate<span style="color: #339933;">,</span><span style="color: #CC0000;">20</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>The <strong>moveBall()</strong> function adjusts the ball coordinates by adding the ball speed. We initalize balldeltaX and ballDeltaY when the game starts.</p>
<p>Now that everything is in place, the ball should start moving.<br />
<img src="http://www.adityaravishankar.com/wp-content/uploads/2011/10/Breakout-Game-Programming-Tutorial-2-Screenshot-239x300.png" alt="Screenshot" title="Breakout Game Programming Tutorial 2 -Screenshot" height="200" style="height:200px;" class="alignright size-medium wp-image-430" /><br />
However when we open the file in a browser, we see something weird. </p>
<p>Instead of a moving ball, we have a long black line. The ball keeps moving, however the old balls stay behind, leaving a long trail along the path. We need to clear the earlier objects before drawing new ones. </p>
<p>To do this, we can use the <strong>clearRect()</strong> method.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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"><span style="color: #003366; font-weight: bold;">function</span> animate <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; context.<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>canvas.<span style="color: #660066;">width</span><span style="color: #339933;">,</span>canvas.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>This line clears the entire canvas at the beginning of the animation loop. When we run the code now, it looks like a moving ball instead of a long snake. </p>
<p>The next problem is that our ball doesn&#8217;t bounce off the walls. It keeps moving past the edge of the canvas and disappears. To fix that, lets add some conditions to check when the ball is moving outside the boundary.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><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 /></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> moveBall<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// First check if we will bump into something</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// If top of the ball touches the top then reverse Y direction (move downwards)</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ballY <span style="color: #339933;">+</span> ballDeltaY <span style="color: #339933;">-</span> ballRadius <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ballDeltaY <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>ballDeltaY<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// If the bottom of the ball touches the bottom of the screen then end the game</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ballY <span style="color: #339933;">+</span> ballDeltaY <span style="color: #339933;">+</span> ballRadius <span style="color: #339933;">&gt;</span> canvas.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; endGame<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// If side of ball touches either side of the wall then reverse X direction</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//left of ball moves too far left</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>ballX <span style="color: #339933;">+</span> ballDeltaX <span style="color: #339933;">-</span> ballRadius <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//or right side of ball moves too far right</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#40;</span>ballX <span style="color: #339933;">+</span> ballDeltaX <span style="color: #339933;">+</span> ballRadius <span style="color: #339933;">&gt;</span> canvas.<span style="color: #660066;">width</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; ballDeltaX <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>ballDeltaX<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Move the ball</span><br />
&nbsp; &nbsp; ballX <span style="color: #339933;">=</span> ballX <span style="color: #339933;">+</span> ballDeltaX<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; ballY <span style="color: #339933;">=</span> ballY <span style="color: #339933;">+</span> ballDeltaY<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Great! Now the ball is bouncing off the walls properly and when it touches the ground the game ends.</p>
<p>Now lets animate the paddle. To move the paddle, we need to first track the left and right keys and keep track of where the paddle should move during the next <strong>animate()</strong>. We do this by using the <strong>keydown()</strong> and <strong>keyup()</strong> jQuery methods to assign event handlers for these events.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><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 /></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> paddleMove<span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> startGame<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; ballDeltaY <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">4</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; ballDeltaX <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">2</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; paddleMove <span style="color: #339933;">=</span> <span style="color: #3366CC;">'NONE'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; paddleDeltaX <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// call the animate() function every 200ms until clearInterval(gameLoop) is called</span><br />
&nbsp; &nbsp; gameLoop <span style="color: #339933;">=</span> setInterval<span style="color: #009900;">&#40;</span>animate<span style="color: #339933;">,</span><span style="color: #CC0000;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Start Tracking Keystokes</span><br />
&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">keydown</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>evt.<span style="color: #660066;">keyCode</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">39</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; paddleMove <span style="color: #339933;">=</span> <span style="color: #3366CC;">'RIGHT'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &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>evt.<span style="color: #660066;">keyCode</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">37</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; paddleMove <span style="color: #339933;">=</span> <span style="color: #3366CC;">'LEFT'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; <br />
<br />
&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">keyup</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>evt.<span style="color: #660066;">keyCode</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">39</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; paddleMove <span style="color: #339933;">=</span> <span style="color: #3366CC;">'NONE'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &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>evt.<span style="color: #660066;">keyCode</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">37</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; paddleMove <span style="color: #339933;">=</span> <span style="color: #3366CC;">'NONE'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</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>The event handlers check if the key pressed or key released is Left (key code 39) or Right (key code 37) and stores this in a paddleMove variable. We will check this variable every time we animate to see where the paddle needs to move next.</p>
<p>The next thing we need to do is define another <strong>movePaddle()</strong> function that is called just like <strong>moveBall()</strong>.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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 /></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> paddleDeltaX<span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> paddleSpeedX <span style="color: #339933;">=</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> movePaddle<span style="color: #009900;">&#40;</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>paddleMove <span style="color: #339933;">==</span> <span style="color: #3366CC;">'LEFT'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; paddleDeltaX <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>paddleSpeedX<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>paddleMove <span style="color: #339933;">==</span> <span style="color: #3366CC;">'RIGHT'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; paddleDeltaX <span style="color: #339933;">=</span> paddleSpeedX<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: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; paddleDeltaX <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// If paddle reaches the side of the screen, then don't let it move any further</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>paddleX <span style="color: #339933;">+</span> paddleDeltaX <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">||</span> paddleX <span style="color: #339933;">+</span> paddleDeltaX <span style="color: #339933;">+</span>paddleWidth <span style="color: #339933;">&gt;</span>canvas.<span style="color: #660066;">width</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; paddleDeltaX <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; paddleX <span style="color: #339933;">=</span> paddleX <span style="color: #339933;">+</span> paddleDeltaX<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Great! Now the paddle moves to the left and right when we use the cursor keys. </p>
<p>The last thing we need to do is make the ball bounce if it hits the paddle. We just need to add a condition to the other collision checks in <strong>moveBall()</strong>.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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: #006600; font-style: italic;">// if bottom of ball reaches the top of paddle,</span><br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ballY <span style="color: #339933;">+</span> ballDeltaY <span style="color: #339933;">+</span> ballRadius <span style="color: #339933;">&gt;=</span> paddleY<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// and it is positioned between the two ends of the paddle (is on top)</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ballX <span style="color: #339933;">+</span> ballDeltaX <span style="color: #339933;">&gt;=</span> paddleX <span style="color: #339933;">&amp;&amp;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; ballX <span style="color: #339933;">+</span> ballDeltaX <span style="color: #339933;">&lt;=</span> paddleX <span style="color: #339933;">+</span> paddleWidth<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ballDeltaY <span style="color: #339933;">=</span> <span style="color: #339933;">-</span> ballDeltaY<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>And thats it! The paddle moves perfectly, the ball bounces off the wall and the paddle, and if the ball hits the ground, the game ends. </p>
<p>We have come a long way from an empty rectangle on the browser. Of course, this game is still a little boring with just a ball and paddle. </p>
<p>In <a href="http://www.adityaravishankar.com/2011/10/html5-game-development-tutorial-breakout-part-iii-collisions-scoring-and-sound/" title="HTML5 Game Development Tutorial: Breakout Part III – Collisions, Scoring and Sound">the next tutorial</a>, we will bring back the bricks that we drew before, handle the ball colliding with these bricks, and finally increase the score when a brick gets destroyed.</p>
<p>You can download the finished source code for this tutorial below.<br />
<a href="http://www.adityaravishankar.com/downloads/breakout2.html.zip">HTML5 Game Development Tutorial: Breakout Part II - Source Code</a>
<p style="color:brown;">Note: If you found this article useful enough to download the code above, then please do take the time to share this article with others using the Like/Tweet button.
</p></p>
<p>Continue to part 3 of this series. <a href="http://www.adityaravishankar.com/2011/10/html5-game-development-tutorial-breakout-part-iii-collisions-scoring-and-sound/" title="HTML5 Game Development Tutorial: Breakout Part III – Collisions, Scoring and Sound">HTML5 Game Development Tutorial: Breakout Part III – Collisions, Scoring and Sound</a></p>

<p><a href="http://feedads.g.doubleclick.net/~a/r4SXP9tmIfwgeewJMQU92IDEbR0/0/da"><img src="http://feedads.g.doubleclick.net/~a/r4SXP9tmIfwgeewJMQU92IDEbR0/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/r4SXP9tmIfwgeewJMQU92IDEbR0/1/da"><img src="http://feedads.g.doubleclick.net/~a/r4SXP9tmIfwgeewJMQU92IDEbR0/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/AdityaRaviShankar/~4/HXAHLW7wahA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.adityaravishankar.com/2011/10/html5-game-development-tutorial-breakout-part-ii-animation-and-keyboard-input/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.adityaravishankar.com/2011/10/html5-game-development-tutorial-breakout-part-ii-animation-and-keyboard-input/</feedburner:origLink></item>
		<item>
		<title>HTML5 Game Development Tutorial: Breakout Part I – Introduction to Canvas</title>
		<link>http://feedproxy.google.com/~r/AdityaRaviShankar/~3/OJmBeE04EwQ/</link>
		<comments>http://www.adityaravishankar.com/2011/10/html5-game-development-tutorial-breakout-i-introduction-to-canvas/#comments</comments>
		<pubDate>Tue, 11 Oct 2011 12:48:15 +0000</pubDate>
		<dc:creator>Adi</dc:creator>
				<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[game development]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.adityaravishankar.com/?p=380</guid>
		<description><![CDATA[This is the first in a series of tutorials on developing a full featured version of Breakout. This tutorial will cover drawing primitive objects on the screen using the Canvas element and developing the basic game screen. Before we get &#8230; <a href="http://www.adityaravishankar.com/2011/10/html5-game-development-tutorial-breakout-i-introduction-to-canvas/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.adityaravishankar.com/games/breakout"><img src="http://www.adityaravishankar.com/wp-content/uploads/2011/10/Breakout-v0.6-Screenshot-240x300.png" alt="Breakout v0.6 Screenshot" title="Breakout v0.6 Screenshot" width="240" height="300" class="alignright size-medium wp-image-504" /></a>This is the first in a series of tutorials on developing a <a href="http://www.adityaravishankar.com/2011/10/javascript-game-development-html5/" title="Javascript Game Development with HTML5 Canvas – Breakout released">full featured version of Breakout</a>. This tutorial will cover drawing primitive objects on the screen using the Canvas element and developing the basic game screen.</p>
<p>Before we get started, check out <a href="http://www.adityaravishankar.com/games/breakout" title="Breakout Game" target="_blank">the finished Breakout game demo</a>. With flying bats, falling spiders, laser turrets and exploding bricks, its probably a little different from the typical breakout game. We will be recreating this game over the series.</p>
<p>All of our development will be for HTML5 capable browsers that properly support the HTML5 Canvas and Audio tags (Chrome, Safari, Firefox, Opera and maybe Internet Explorer 9). Make sure you have one of these browsers.</p>
<p>Now let&#8217;s get started. </p>
<p>First we create a simple HTML file with a canvas inside it.</p>
<div class="codecolorer-container html4strict railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><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 /></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;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Content-type&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html; charset=utf-8&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Breakout - A fast fun game<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; $(function() {<br />
&nbsp; &nbsp; &nbsp; &nbsp; // All our code will go here. jQuery will run this after the document loads completely<br />
&nbsp; &nbsp; &nbsp; &nbsp; });<br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">style</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; canvas {<br />
&nbsp; &nbsp; &nbsp; &nbsp; border:1px solid black;<br />
&nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>Breakout<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span><br />
&nbsp; &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;400&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;500&quot;</span>&gt;</span> <br />
&nbsp; &nbsp; &nbsp; Your browser does not support the HTML5 Canvas feature. This game uses new HTML5 features and will only work on the latest versions of Firefox, Safari or Chrome (and maybe Internet Explorer 9). &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>canvas&gt;</span> &nbsp; <br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></div></td></tr></tbody></table></div>
<p>This creates a Canvas element with dimensions 400 x 500 with a black border around it. If you open the HTML page in your browser, all you will see is a black rectangle on an empty white screen. In case your browser does not support HTML5 Canvas, it shows the unsupported feature message. </p>
<p>We also include jQuery to make cross browser development easier. </p>
<p>To start using the Canvas we need to get its 2d context which then gives us access to all the drawing methods we need.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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">&nbsp; <span style="color: #006600; font-style: italic;">// this is the same as document.getElementById('canvas');</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> canvas <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#canvas'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <br />
&nbsp; <span style="color: #006600; font-style: italic;">// different browsers support different contexts. All support 2d</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> context <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></div></td></tr></tbody></table></div>
<p>With this context object, we now have access to the methods that we will need for our game like <strong>fillRect()</strong>, <strong>clearRect()</strong> and <strong>drawImage()</strong>.</p>
<p>Now that we can draw, lets start with creating the paddle.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> paddleX <span style="color: #339933;">=</span> <span style="color: #CC0000;">200</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> paddleY <span style="color: #339933;">=</span> <span style="color: #CC0000;">460</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> paddleWidth <span style="color: #339933;">=</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> paddleHeight <span style="color: #339933;">=</span> <span style="color: #CC0000;">15</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> paddleDeltaX <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> paddleDeltaY <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #003366; font-weight: bold;">function</span> drawPaddle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; context.<span style="color: #660066;">fillRect</span><span style="color: #009900;">&#40;</span>paddleX<span style="color: #339933;">,</span>paddleY<span style="color: #339933;">,</span>paddleWidth<span style="color: #339933;">,</span>paddleHeight<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Note that we store all the parameters in variables so that we can easily modify and reuse them later. Drawing the paddle is as simple as using the <strong>fillRect()</strong> method.</p>
<p>Now lets draw the ball.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> ballX <span style="color: #339933;">=</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> ballY <span style="color: #339933;">=</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> ballRadius <span style="color: #339933;">=</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> drawBall<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #006600; font-style: italic;">// Context.beginPath when you draw primitive shapes</span><br />
&nbsp; context.<span style="color: #660066;">beginPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #006600; font-style: italic;">// Draw arc at center ballX, ballY with radius ballRadius, </span><br />
&nbsp; <span style="color: #006600; font-style: italic;">// From 0 to 2xPI radians (full circle)</span><br />
&nbsp; context.<span style="color: #660066;">arc</span><span style="color: #009900;">&#40;</span>ballX<span style="color: #339933;">,</span>ballY<span style="color: #339933;">,</span>ballRadius<span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>Math.<span style="color: #660066;">PI</span><span style="color: #339933;">*</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #006600; font-style: italic;">// Fill up the path that you just drew</span><br />
&nbsp; context.<span style="color: #660066;">fill</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Drawing the ball requires the <strong>arc()</strong> method to draw a circular shape and the <strong>fill()</strong> method to fill the shape we just created. While drawing the arc, the starting angle is 0 and the ending angle is 2xPI radians (or 360 degrees) which makes a full circle.</p>
<p>Now lets draw the bricks on top.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><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 /></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> bricksPerRow <span style="color: #339933;">=</span> <span style="color: #CC0000;">8</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #003366; font-weight: bold;">var</span> brickHeight <span style="color: #339933;">=</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> brickWidth <span style="color: #339933;">=</span> canvas.<span style="color: #660066;">width</span><span style="color: #339933;">/</span>bricksPerRow<span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// Brick Layout: 1 is orange, 2 is green, 3 is gray, 0 means no brick </span><br />
&nbsp;<span style="color: #003366; font-weight: bold;">var</span> bricks <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><br />
<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<br />
<br />
<span style="color: #006600; font-style: italic;">// iterate through the bricks array and draw each brick using drawBrick()</span><br />
<span style="color: #003366; font-weight: bold;">function</span> createBricks<span style="color: #009900;">&#40;</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> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> bricks.<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; &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> bricks<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<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; &nbsp; &nbsp; &nbsp; drawBrick<span style="color: #009900;">&#40;</span>j<span style="color: #339933;">,</span>i<span style="color: #339933;">,</span>bricks<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// draw a single brick</span><br />
<span style="color: #003366; font-weight: bold;">function</span> drawBrick<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span>y<span style="color: #339933;">,</span>type<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>type<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// if brick is still visible; three colors for three types of bricks</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'orange'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'rgb(100,200,100)'</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'rgba(50,100,50,.5)'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">default</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #660066;">clearRect</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">*</span>brickWidth<span style="color: #339933;">,</span>y<span style="color: #339933;">*</span>brickHeight<span style="color: #339933;">,</span>brickWidth<span style="color: #339933;">,</span>brickHeight<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</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>type<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//Draw rectangle with fillStyle color selected earlier</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #660066;">fillRect</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">*</span>brickWidth<span style="color: #339933;">,</span>y<span style="color: #339933;">*</span>brickHeight<span style="color: #339933;">,</span>brickWidth<span style="color: #339933;">,</span>brickHeight<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Also draw blackish border around the brick</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #660066;">strokeRect</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">*</span>brickWidth<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span>y<span style="color: #339933;">*</span>brickHeight<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span>brickWidth<span style="color: #339933;">-</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span>brickHeight<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; <span style="color: #009900;">&#125;</span> <br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>First we store the bricks layout in an Array making it easier to visualize and modify. Feel free to try adding a few more rows, or changing the numbers to modify the layout. </p>
<p>We then iterate over each row and column and call the drawBrick() method to place the brick correctly. Within drawBrick(), we use a switch statement to set different fill colors for different types of bricks. This function can be extended to add more brick types.</p>
<p>Finally, lets create a simple scoreboard or status bar below.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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;">var</span> score <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> displayScoreBoard<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//Set the text font and color</span><br />
&nbsp; &nbsp; context.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'rgb(50,100,50)'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; context.<span style="color: #660066;">font</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;20px Times New Roman&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//Clear the bottom 30 pixels of the canvas</span><br />
&nbsp; &nbsp; context.<span style="color: #660066;">clearRect</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>canvas.<span style="color: #660066;">height</span><span style="color: #339933;">-</span><span style="color: #CC0000;">30</span><span style="color: #339933;">,</span>canvas.<span style="color: #660066;">width</span><span style="color: #339933;">,</span><span style="color: #CC0000;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>&nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Write Text 5 pixels from the bottom of the canvas</span><br />
&nbsp; &nbsp; context.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Score: '</span><span style="color: #339933;">+</span>score<span style="color: #339933;">,</span><span style="color: #CC0000;">10</span><span style="color: #339933;">,</span>canvas.<span style="color: #660066;">height</span><span style="color: #339933;">-</span><span style="color: #CC0000;">5</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>This uses the <strong>fillText()</strong> method to write text at the bottom of the canvas.</p>
<p>We can check the output of the code so far by calling each of the functions that we wrote.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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: #006600; font-style: italic;">// Call the methods to make sure they work</span><br />
drawPaddle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
drawBall<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
createBricks<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
displayScoreBoard<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p><img src="http://www.adityaravishankar.com/wp-content/uploads/2011/10/Breakout-Tutorial-1-Screenshot-212x300.png" alt="Breakout - Tutorial 1 - Screenshot" title="Breakout - Tutorial 1 - Screenshot" width="212" height="300" class="alignright size-medium wp-image-407" />This is the final result. We have a complete game screen with all the elements drawn on it programmatically &#8211; the bricks, the paddle, the ball and the scoreboard. </p>
<p>It doesn&#8217;t seem like much yet. Everything is static. However, because we stored the parameters in variables, all we need to do to move objects around is change the parameter values and call the same functions again. Thats when things start getting interesting.</p>
<p>In <a href="http://www.adityaravishankar.com/2011/10/html5-game-development-tutorial-breakout-part-ii-%e2%80%93-animation-and-keyboard-input/" title="HTML5 Game Development Tutorial: Breakout Part II – Animation and Keyboard Input">part II of this series</a>, we will use <strong>setInterval()</strong> to add some animation and capture <strong>keydown</strong> and <strong>keyup</strong> events to respond to user input.</p>
<p>You can download the finished source code for this tutorial below.<br />
<a href="http://www.adityaravishankar.com/downloads/breakout1.html.zip">HTML5 Game Development Tutorial: Breakout Part I  - Source Code</a>
<p style="color:brown;">Note: If you found this article useful enough to download the code above, then please do take the time to share this article with others using the Like/Tweet button.
</p></p>
<p>Continue to part 2 of this series. <a href="http://www.adityaravishankar.com/2011/10/html5-game-development-tutorial-breakout-part-ii-%e2%80%93-animation-and-keyboard-input/" title="HTML5 Game Development Tutorial: Breakout Part II – Animation and Keyboard Input">HTML5 Game Development Tutorial: Breakout Part II – Animation and Keyboard Input</a></p>

<p><a href="http://feedads.g.doubleclick.net/~a/pm3Oe4NluU4Iyh_xwNCBulJjTKs/0/da"><img src="http://feedads.g.doubleclick.net/~a/pm3Oe4NluU4Iyh_xwNCBulJjTKs/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/pm3Oe4NluU4Iyh_xwNCBulJjTKs/1/da"><img src="http://feedads.g.doubleclick.net/~a/pm3Oe4NluU4Iyh_xwNCBulJjTKs/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/AdityaRaviShankar/~4/OJmBeE04EwQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.adityaravishankar.com/2011/10/html5-game-development-tutorial-breakout-i-introduction-to-canvas/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.adityaravishankar.com/2011/10/html5-game-development-tutorial-breakout-i-introduction-to-canvas/</feedburner:origLink></item>
		<item>
		<title>Javascript Game Development with HTML5 Canvas – Breakout released</title>
		<link>http://feedproxy.google.com/~r/AdityaRaviShankar/~3/GVYpf5XhJcI/</link>
		<comments>http://www.adityaravishankar.com/2011/10/javascript-game-development-html5/#comments</comments>
		<pubDate>Sun, 09 Oct 2011 21:09:04 +0000</pubDate>
		<dc:creator>Adi</dc:creator>
				<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[game development]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.adityaravishankar.com/?p=370</guid>
		<description><![CDATA[I started working on my version of the Breakout game as a simple exercise to learn Javascript game programming, using HTML5 Canvas for animation. The way I see it, Breakout is the &#8220;Hello World&#8221; of Game development. Pretty much everyone &#8230; <a href="http://www.adityaravishankar.com/2011/10/javascript-game-development-html5/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.adityaravishankar.com/games/breakout"><img src="http://www.adityaravishankar.com/wp-content/uploads/2011/10/Breakout-v0.6-Screenshot-240x300.png" alt="Breakout v0.6 Screenshot" title="Breakout v0.6 Screenshot" width="240" height="300" class="alignright size-medium wp-image-504" /></a>I started working on <a title="Breakout Game" href="http://www.adityaravishankar.com/games/breakout">my version of the Breakout game</a> as a simple exercise to learn Javascript game programming, using HTML5 Canvas for animation.</p>
<p>The way I see it, Breakout is the &#8220;Hello World&#8221; of Game development. Pretty much everyone does it when they want to learn the basics. It is probably one of the simplest games to develop though you can choose to make this exercise as easy or as challenging as you like.</p>
<p>In the process of building even the simplest versions of this game you learn most of the essential fundamentals for building any Javascript game, namely -</p>
<ol>
<li>Drawing primitive objects on the screen using the Canvas element and its Context.  (<a href="http://www.adityaravishankar.com/2011/10/html5-game-development-tutorial/" title="HTML5 Game Development Tutorial: Breakout Part I – Introduction to Canvas">See Introduction to Canvas Tutorial</a>)</li>
<li>Moving objects by using clearRect() and redrawing the object. (<a href="http://www.adityaravishankar.com/2011/10/html5-game-development-tutorial-breakout-part-ii-%e2%80%93-animation-and-keyboard-input/" title="HTML5 Game Development Tutorial: Breakout Part II – Animation and Keyboard Input">See Animation and Keyboard Input Tutorial</a>)</li>
<li>Using setInterval() and setTimeout() to animate objects and run a game loop. (<a href="http://www.adityaravishankar.com/2011/10/html5-game-development-tutorial-breakout-part-ii-%e2%80%93-animation-and-keyboard-input/" title="HTML5 Game Development Tutorial: Breakout Part II – Animation and Keyboard Input">See Animation and Keyboard Input Tutorial</a>)</li>
<li>Capturing keypress, keydown and keyup events and responding to user input. (<a href="http://www.adityaravishankar.com/2011/10/html5-game-development-tutorial-breakout-part-ii-%e2%80%93-animation-and-keyboard-input/" title="HTML5 Game Development Tutorial: Breakout Part II – Animation and Keyboard Input">See Animation and Keyboard Input Tutorial</a>)</li>
<li>And finally rudimentary collision detection. (<a href="http://www.adityaravishankar.com/2011/10/html5-game-development-tutorial-breakout-part-iii-collisions-scoring-and-sound/" title="HTML5 Game Development Tutorial: Breakout Part III – Collisions, Scoring and Sound">See Collisions, Scoring and Sound Tutorial</a>)</li>
</ol>
<div> If you then decide to put in a little more effort, you also learn -</div>
<div>
<ol>
<li>Using HTML5 Audio for sound effects. (<a href="http://www.adityaravishankar.com/2011/10/html5-game-development-tutorial-breakout-part-iii-collisions-scoring-and-sound/" title="HTML5 Game Development Tutorial: Breakout Part III – Collisions, Scoring and Sound">See Collisions, Scoring and Sound Tutorial</a>)</li>
<li>Using bitmaps with drawImage() for better looking graphics.</li>
<li>Animating arrays of objects simultaneously, each with their own life cycle (like bullets)</li>
</ol>
</div>
<p>I expected this game to be a quick learning experience with limited playable value. I never expected the game to be as much fun as it is. Once I decided to put the game up on my website, the positive feedback that I got was  very heartening.</p>
<p>Based on feedback, I have decided to continue adding features and developing the game. If you are interested in game development and the making of this game, you can check out the <a href="http://www.adityaravishankar.com/2011/10/html5-game-development-tutorial/" title="HTML5 Game Development Tutorial: Breakout Part I – Introduction to Canvas">tutorial series on developing Breakout</a>.</p>
<p>The latest version of the game is available at the <a title="Breakout Game" href="http://www.adityaravishankar.com/games/breakout">Breakout Game page</a>. Please check it out and let me know whether you like it. I would appreciate any suggestions or constructive criticism (as a player or as a developer) &#8211; Any bugs or any playability issues that you find.</p>
<p>If you like the game, please do let me know, and do share it with all your friends. You can use the comments form on <a title="Breakout Game" href="http://www.adityaravishankar.com/games/breakout">the game page</a>.</p>
<p>&nbsp;</p>

<p><a href="http://feedads.g.doubleclick.net/~a/vnGBIKN7aYdM9oMat5YZO_3vQEI/0/da"><img src="http://feedads.g.doubleclick.net/~a/vnGBIKN7aYdM9oMat5YZO_3vQEI/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/vnGBIKN7aYdM9oMat5YZO_3vQEI/1/da"><img src="http://feedads.g.doubleclick.net/~a/vnGBIKN7aYdM9oMat5YZO_3vQEI/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/AdityaRaviShankar/~4/GVYpf5XhJcI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.adityaravishankar.com/2011/10/javascript-game-development-html5/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.adityaravishankar.com/2011/10/javascript-game-development-html5/</feedburner:origLink></item>
		<item>
		<title>NowJS and Node.js Tutorial – Creating a multi room chat client</title>
		<link>http://feedproxy.google.com/~r/AdityaRaviShankar/~3/04pfXRR8wdY/</link>
		<comments>http://www.adityaravishankar.com/2011/10/nowjs-node-js-tutorial-creating-multi-room-chat-server/#comments</comments>
		<pubDate>Sat, 01 Oct 2011 08:12:51 +0000</pubDate>
		<dc:creator>Adi</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[nodejs]]></category>
		<category><![CDATA[nowjs]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.adityaravishankar.com/?p=289</guid>
		<description><![CDATA[Node.js is a server side environment for Javascript. NowJS is a framework built on top of Node.js that connects the client side and server side Javascript effortlessly. The core of NowJS functionality lies in the now object.The now object is &#8230; <a href="http://www.adityaravishankar.com/2011/10/nowjs-node-js-tutorial-creating-multi-room-chat-server/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a title="Node.js" href="http://nodejs.org/">Node.js</a> is a server side environment for Javascript. <a title="NowJS" href="http://nowjs.com/">NowJS</a> is a framework built on top of Node.js that connects the client side and server side Javascript effortlessly.</p>
<blockquote><p>The core of NowJS functionality lies in the now object.The now object is special because it exists on the server and the client.</p>
<p>This means variables you set in the now object are automatically synced between client and server. Also server functions can be directly called on the client and client functions can be called directly from the server.</p>
<p>All in realtime.</p></blockquote>
<p>You can have a working HTTP server up and running in Node.JS with just a few lines of code. For example -</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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> http <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'http'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
http.<span style="color: #660066;">createServer</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; res.<span style="color: #660066;">writeHead</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'Content-Type'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'text/plain'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Hello World<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">8080</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>This little snippet of code will create an HTTP server, listen on port 8080, and send back &#8220;Hello World&#8221; for every request. That&#8217;s it. Nothing more needed.</p>
<p>Using NowJS, communication between the client and server side is just as simple.</p>
<p>Client Side:</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span><br />
now.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; now.<span style="color: #660066;">clientSideVariable</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'someValue'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; now.<span style="color: #660066;">serverSideFunction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; <br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></div></td></tr></tbody></table></div>
<p>In this code snippet, the client side sets a variable to &#8216;someValue&#8217; and calls serverSideFunction(), which is declared only on the server. </p>
<p>Server Side:</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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">everyone.<span style="color: #660066;">now</span>.<span style="color: #660066;">serverSideFunction</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">now</span>.<span style="color: #660066;">clientSideVariable</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>The server side is then able to access clientSideVariable, which is declared only on the client. </p>
<p>All the details such as establishing connections and communicating change of data between the server and client are handed automagically by the framework.</p>
<p>In fact writing code using this framework is so simple, the <a title="NowJS Hello World Example" href="http://nowjs.com/doc/example"> NowJS hello world example</a> is a working chat client and server written in under a dozen lines of code. Go check it out.</p>
<p>As a simple exercise to get comfortable with the NowJS API, we can modify the chat client example to support multiple chat rooms. Let&#8217;s take a look at how easy it is.</p>
<p><strong>Server Side (multiroom_server.js)</strong></p>
<p>1. The first thing we need to do is modify the distributeMessage() function to only send messages to users in the same chat room as the user.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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: #006600; font-style: italic;">// Send message to everyone in the users group</span><br />
everyone.<span style="color: #660066;">now</span>.<span style="color: #660066;">distributeMessage</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> group <span style="color: #339933;">=</span> nowjs.<span style="color: #660066;">getGroup</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">now</span>.<span style="color: #660066;">serverRoom</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; group.<span style="color: #660066;">now</span>.<span style="color: #660066;">receiveMessage</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">now</span>.<span style="color: #000066;">name</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'@'</span><span style="color: #339933;">+</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">now</span>.<span style="color: #660066;">serverRoom</span><span style="color: #339933;">,</span> message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>We store the name of the server room on the client side (<strong>this.now.serverRoom</strong>). When the client calls the distributeMessage() function we send the message to everyone in the same chat room by using <strong>getGroup()</strong> and using the <strong>group.now</strong> object instead of the <strong>everyone.now</strong> object. (<strong>everyone</strong> is just a group that contains all users connected to the server)</p>
<p>2. Next we need to handle the client changing chat rooms.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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">everyone.<span style="color: #660066;">now</span>.<span style="color: #660066;">changeRoom</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>newRoom<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> oldRoom <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">now</span>.<span style="color: #660066;">serverRoom</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//if old room is not null; then leave the old room</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>oldRoom<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> oldGroup <span style="color: #339933;">=</span> nowjs.<span style="color: #660066;">getGroup</span><span style="color: #009900;">&#40;</span>oldRoom<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; oldGroup.<span style="color: #660066;">removeUser</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">user</span>.<span style="color: #660066;">clientId</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// join the new room</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> newGroup <span style="color: #339933;">=</span> nowjs.<span style="color: #660066;">getGroup</span><span style="color: #009900;">&#40;</span>newRoom<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; newGroup.<span style="color: #660066;">addUser</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">user</span>.<span style="color: #660066;">clientId</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// update the client's serverRoom variable</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">now</span>.<span style="color: #660066;">serverRoom</span> <span style="color: #339933;">=</span> newRoom<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>The <strong>getGroup()</strong> method fetches the group object if it exists and creates a group if it doesn&#8217;t already exist. We use the groups <strong>addUser()</strong> and <strong>removeUser()</strong> methods to move the client from the old room to the new room.</p>
<p>Thats about it on the server side.</p>
<p><strong>Client Side (multiroom.html)</strong></p>
<p>3. First we add a drop down with the list of server rooms.</p>
<div class="codecolorer-container html4strict railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">select</span>&gt;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">selected</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;selected&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;room1&quot;</span>&gt;</span>Room 1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;room2&quot;</span>&gt;</span>Room 2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;room3&quot;</span>&gt;</span>Room 3<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">select</span>&gt;</span></div></td></tr></tbody></table></div>
<p>4. Next we call the server side changeRoom() function when the user first connects and whenever the drop down is changed.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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: #006600; font-style: italic;">// on establishing 'now' connection, set the server room </span><br />
now.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// By default pick the first chatroom</span><br />
&nbsp; &nbsp; now.<span style="color: #660066;">changeRoom</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#server-room'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// On change of drop down, clear text and change server room</span><br />
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#server-room'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#messages&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; now.<span style="color: #660066;">changeRoom</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#server-room'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>5. For extra credit, we can allow the server to dynamically provide the list of rooms when the client connects.</p>
<p>The changes required would be -</p>
<p>Server Side:</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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"><span style="color: #006600; font-style: italic;">// Declare a list of server rooms</span><br />
everyone.<span style="color: #660066;">now</span>.<span style="color: #660066;">serverRoomsList</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'room1'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'Room 1'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'room2'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'Room 2'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'room3'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'Room 3'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Client Side:</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><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 /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">now.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Pick up list of chatrooms from server and populate dropdown</span><br />
&nbsp; &nbsp; setServerRoomFromList<span style="color: #009900;">&#40;</span>now.<span style="color: #660066;">serverRoomsList</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// By default pick the first chatroom</span><br />
&nbsp; &nbsp; now.<span style="color: #660066;">changeRoom</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#server-room'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// populate the #server-room dropdown from a list</span><br />
<span style="color: #003366; font-weight: bold;">function</span> setServerRoomFromList<span style="color: #009900;">&#40;</span>roomList<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#server-room'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>roomList<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>key<span style="color: #339933;">,</span> value<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#server-room'</span><span style="color: #009900;">&#41;</span>.<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>.<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;value&quot;</span><span style="color: #339933;">,</span>key<span style="color: #009900;">&#41;</span>.<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</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>That&#8217;s really all you need to do. We now have a multi room chat client/server with<br />
i) A drop down on the client side with list of server rooms<br />
ii) On connection with server, client auto populates server room list and enters the first room<br />
iii) Changing drop down changes room<br />
iv) Entering new room clears the screen for the user</p>
<p>This is obviously a fairly barebones version (like the original example), but it serves as a proof of concept of all the essential elements of NowJS communication.</p>
<p>The finished code has some extra comments and a few more features. You can download the complete source code below. </p>
<p><a href="http://www.adityaravishankar.com/downloads/NodeJS_NowJS_MultiRoom.zip">Node.JS and NowJS Multi Room Chat Server - Source Code</a>
<p style="color:brown;">Note: If you found this article useful enough to download the code above, then please do take the time to share this article with others using the Like/Tweet button.
</p></p>

<p><a href="http://feedads.g.doubleclick.net/~a/eSSic5uxloBhQEkXlBQFthwoHMo/0/da"><img src="http://feedads.g.doubleclick.net/~a/eSSic5uxloBhQEkXlBQFthwoHMo/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/eSSic5uxloBhQEkXlBQFthwoHMo/1/da"><img src="http://feedads.g.doubleclick.net/~a/eSSic5uxloBhQEkXlBQFthwoHMo/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/AdityaRaviShankar/~4/04pfXRR8wdY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.adityaravishankar.com/2011/10/nowjs-node-js-tutorial-creating-multi-room-chat-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.adityaravishankar.com/2011/10/nowjs-node-js-tutorial-creating-multi-room-chat-server/</feedburner:origLink></item>
		<item>
		<title>Learn how to play pool well in under 30 minutes</title>
		<link>http://feedproxy.google.com/~r/AdityaRaviShankar/~3/diLhqg_rnQg/</link>
		<comments>http://www.adityaravishankar.com/2011/09/learn-how-to-play-pool-well-in-under-30-minutes/#comments</comments>
		<pubDate>Wed, 28 Sep 2011 17:36:34 +0000</pubDate>
		<dc:creator>Adi</dc:creator>
				<category><![CDATA[Billiards]]></category>
		<category><![CDATA[billiards]]></category>
		<category><![CDATA[drills]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[pool]]></category>

		<guid isPermaLink="false">http://www.adityaravishankar.com/?p=225</guid>
		<description><![CDATA[Amazon.com WidgetsEver wished you could &#8220;not suck&#8221; at playing pool? Not be considered the worst player in the group? Maybe even win when playing with your &#8220;pool player&#8221; friends and surprise everyone? Well, I am here to tell you that &#8230; <a href="http://www.adityaravishankar.com/2011/09/learn-how-to-play-pool-well-in-under-30-minutes/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><OBJECT class='alignright' classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab" id="Player_e9b08b52-dabc-4fca-a768-1304862ea15a"  WIDTH="250px" HEIGHT="250px"> <PARAM NAME="movie" VALUE="http://ws.amazon.com/widgets/q?ServiceVersion=20070822&#038;MarketPlace=US&#038;ID=V20070822%2FUS%2Fwwwadityaravi-20%2F8003%2Fe9b08b52-dabc-4fca-a768-1304862ea15a&#038;Operation=GetDisplayTemplate"><PARAM NAME="quality" VALUE="high"><PARAM NAME="bgcolor" VALUE="#FFFFFF"><PARAM NAME="allowscriptaccess" VALUE="always"><PARAM NAME="WMODE" VALUE="transparent"><embed src="http://ws.amazon.com/widgets/q?ServiceVersion=20070822&#038;MarketPlace=US&#038;ID=V20070822%2FUS%2Fwwwadityaravi-20%2F8003%2Fe9b08b52-dabc-4fca-a768-1304862ea15a&#038;Operation=GetDisplayTemplate" id="Player_e9b08b52-dabc-4fca-a768-1304862ea15a" quality="high" bgcolor="#ffffff" name="Player_e9b08b52-dabc-4fca-a768-1304862ea15a" allowscriptaccess="always"  WMODE="transparent"  type="application/x-shockwave-flash" align="middle" height="250px" width="250px"></embed></OBJECT> <NOSCRIPT><A HREF="http://ws.amazon.com/widgets/q?ServiceVersion=20070822&#038;MarketPlace=US&#038;ID=V20070822%2FUS%2Fwwwadityaravi-20%2F8003%2Fe9b08b52-dabc-4fca-a768-1304862ea15a&#038;Operation=NoScript">Amazon.com Widgets</A></NOSCRIPT>Ever wished you could &#8220;not suck&#8221; at playing pool? Not be considered the worst player in the group? Maybe even win when playing with your &#8220;pool player&#8221; friends and surprise everyone? Well, I am here to tell you that its not that hard.</p>
<p>It takes surprisingly little effort to beat the average pool player. Most players (outside of pool leagues) have no technique, no consistency and rely on luck or the inexperience of their opponents to win. All it takes to beat them are some basic drills and techniques to improve your consistency.</p>
<p>As someone who loves both to play pool and teach others, I came up with a series of drills that I use to teach complete beginners <a title="Aiming without Aiming – How to shoot pool like a pro" href="http://www.adityaravishankar.com/2009/07/aiming-without-aiming-how-to-shoot-pool-like-a-pro/">how to aim and shoot subconsciously</a>. I taught a friend these basics a while ago and she made some amazing cut shots and ran two-three balls at a time during a game about fifteen minutes after going through the drills. Considering it was the second or third time in her life she had held a cue-stick, it was amazing improvement.</p>
<p>Its no substitute for years of practice, but these basics should give any beginner a jump start and have them playing very confidently in less than 30 minutes, easily beating most untrained or amateur players.</p>
<p>These are the essential basics that any beginner needs to go through to become a good intermediate level player</p>
<p><strong>1. Learning to Stand and Hold the Cue Properly &#8211; The Stance, The Grip and The Bridge</strong></p>
<p>Beginners tend to stand very awkwardly when they try to shoot pool. Most stand in a way where they have no balance and will fall over if pushed. Some can barely hold the cue steadily because their whole body is shaking from the strain of trying to maintain their weird posture.</p>
<p>Simple Advice? Get comfortable when you stand, hold the cue stick gently, and stay relaxed when you shoot. Check out this video for a quick explanation</p>
<p><iframe width="584" height="329" src="http://www.youtube.com/embed/-RKoaE60hko?fs=1&#038;feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>When I am teaching, I first get students to learn a good stance and bridge and make sure their cue-ing is straight (using <a title="The Bottle – Improve your billiards stroke phenomenally without leaving your home" href="http://www.adityaravishankar.com/2009/07/the-bottle-improve-your-billiards-stroke-phenomenally-without-leaving-your-home/">the bottle drill</a> if a bottle if available).</p>
<p>They do not get to touch the cue ball or make a shot until they can maintain their balance in their stance and have a clean, straight stroke. Sometimes I even try to gently push them to test if they are in a stable stance. Ideally you should be so comfortable, you can stay in this position indefinitely.</p>
<p><strong>2. Learning to hit the cue-ball straight and smooth &#8211; The Stroke</strong></p>
<p>The next essential element after you learn to stand, is developing a good stroke. Bad stroke is the biggest reason for unpredictability in a persons game. Ever had days when you can make difficult shots, and other days when you can&#8217;t even make easy shots? Or find that the cue ball isn&#8217;t going where it should? Bad stroke is usually the culprit.</p>
<p><a title="The Bottle – Improve your billiards stroke phenomenally without leaving your home" href="http://www.adityaravishankar.com/2009/07/the-bottle-improve-your-billiards-stroke-phenomenally-without-leaving-your-home/">The bottle drill</a> is probably the easiest way to fix a person&#8217;s stroke and can teach a beginner in minutes what some players with years of experience don&#8217;t realize.</p>
<p>This includes not moving their head during the shot, holding the cue stick gently, and a good clean follow through with no jerkiness in the cue action.</p>
<p>For a detailed explanation, you can read my article on <a title="Table length draw – How to develop the perfect pool stroke" href="http://www.adityaravishankar.com/2009/07/table-length-draw-how-to-develop-the-perfect-pool-stroke/">the essential elements of of a good billiards stroke</a> where I go over each of these elements in detail.</p>
<p>Simple Advice? Hold the cue softly. Dont move any part of the body other than the forearm while shooting. Follow through. And get up only after the cue ball has stopped moving. Check out this video for a quick explanation</p>
<p><iframe width="584" height="329" src="http://www.youtube.com/embed/nmRfZV15h5A?fs=1&#038;feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>When I am teaching, I have the student practice hitting a cue ball from one side of the table into the opposite corner pocket to improve their confidence. If you can hit the cue ball into the opposite corner consistently (the longest possible shot on the table), then your stroke is steady enough.</p>
<p><strong>3. Learning where to hit the cue ball &#8211; An Aiming System</strong></p>
<p>Most beginners don&#8217;t realize that learning to hit straight in step two is the hardest part of learning pool. If you have learned to accurately hit where you aim, then all you need is someone to show you where to aim the cue ball.</p>
<p>Ever had a friend place a finger on the table to show you the point to aim and you found that by aiming there you made the shot easily? Well, <a title="Aiming without Aiming Part II – How I really aim a billiards shot" href="http://www.adityaravishankar.com/2010/02/aiming-without-aiming-part-ii-how-i-really-aim-a-billiards-shot/">the ghost ball system</a> is a simple little trick to find that point without needing someone else.</p>
<p><a href="http://www.adityaravishankar.com/2010/02/aiming-without-aiming-part-ii-how-i-really-aim-a-billiards-shot/"><img class="alignright size-thumbnail wp-image-77" title="Ghost Ball Aiming Method" src="http://www.adityaravishankar.com/wp-content/uploads/2010/02/Ghost-Ball-Method-150x150.png" alt="" width="150" height="150" /></a>You can read my article on <a title="Aiming without Aiming Part II – How I really aim a billiards shot" href="http://www.adityaravishankar.com/2010/02/aiming-without-aiming-part-ii-how-i-really-aim-a-billiards-shot/">the ghost ball system</a> for a detailed explanation of how it works. For longer distance cut shots, I recommend using the <a title="Aiming without Aiming Part III – A system for making long cut shots" href="http://www.adityaravishankar.com/2010/06/aiming-without-aiming-part-iii-a-system-for-making-long-cut-shots/">parallel line aiming system</a>.</p>
<p>Simple Advice? Imagine a ghost ball pushing the target ball into the pocket from the opposite side and aim at the center of the ghost ball. Check out this video for a quick explanation.</p>
<p><iframe width="584" height="329" src="http://www.youtube.com/embed/QbRPSV71HiY?fs=1&#038;feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>When I am teaching students to aim, I start by giving them progressively increasing cut shots. I start with a straight shot, then an easy cut shot, then a tougher one until they are comfortable with most cut shots.</p>
<p>When aiming, I usually place a second object ball as a ghost ball and ask them to use it to line up. Once they do so, and are down on the shot, I remove the ghost ball and let them shoot. After doing this enough number of times, I make them repeat the shot without a ghost ball. In case they start missing, I bring back the ghost ball and let them shoot a few more shots.</p>
<p>I then try to give them the gist of the <a title="Aiming without Aiming – How to shoot pool like a pro" href="http://www.adityaravishankar.com/2009/07/aiming-without-aiming-how-to-shoot-pool-like-a-pro/">aiming without aiming</a> concept of just getting down on the shot and trusting their subconscious mind. Surprisingly, complete beginners are able to pick up the system a lot faster than people who have been shooting for a while.</p>
<p><strong>4. Learning to control the cue ball for the next shot &#8211; Position Play</strong></p>
<p>Ever seen a professional player playing? He not only makes the shot, but the cue ball then rolls around the table right next to the next shot. To be able to do this, you need to understand the elements of positioning the ball and shot selection.</p>
<p>i) Controlling the Ball &#8211; Obviously, for a quick crash course, I ignore the basic elements of position play. For people who have the time, or come back for a second lesson, we go over the stop shot, the follow shot and the draw shot. Check out this video for a quick explanation of follow, draw and stop shots.</p>
<p><iframe width="584" height="329" src="http://www.youtube.com/embed/Pnr8C4tBCbo?fs=1&#038;feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>ii) Positioning the Ball &#8211; Once you can control the cue ball, you need to start using the 90 and 30 degree rule and apply them for simple position play. Then comes learning to use follow, draw and side english to control where the cue ball goes after each shot. I have a separate workout to explain position play.</p>
<p>iii) Shot Selection &#8211; The last piece of the puzzle is learning to choose which ball to aim at so you are left with another shot after making the current ball. This is how you can make/run more than one ball at a time. After going over position play, I usually just play while I discuss and illustrate shot selection while using position play.</p>
<p>Thats it!!!</p>
<p>Mastering these fundamentals takes any person from barely able to hold the cue to playing at an APA 2-3 level in under an hour. What separates an APA 2-3 from an APA 4-5 is consistency.</p>
<p><OBJECT class='alignleft' classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab" id="Player_e9b08b52-dabc-4fca-a768-1304862ea15a"  WIDTH="250px" HEIGHT="250px"> <PARAM NAME="movie" VALUE="http://ws.amazon.com/widgets/q?ServiceVersion=20070822&#038;MarketPlace=US&#038;ID=V20070822%2FUS%2Fwwwadityaravi-20%2F8003%2Fe9b08b52-dabc-4fca-a768-1304862ea15a&#038;Operation=GetDisplayTemplate"><PARAM NAME="quality" VALUE="high"><PARAM NAME="bgcolor" VALUE="#FFFFFF"><PARAM NAME="allowscriptaccess" VALUE="always"><PARAM NAME="WMODE" VALUE="transparent"><embed src="http://ws.amazon.com/widgets/q?ServiceVersion=20070822&#038;MarketPlace=US&#038;ID=V20070822%2FUS%2Fwwwadityaravi-20%2F8003%2Fe9b08b52-dabc-4fca-a768-1304862ea15a&#038;Operation=GetDisplayTemplate" id="Player_e9b08b52-dabc-4fca-a768-1304862ea15a" quality="high" bgcolor="#ffffff" name="Player_e9b08b52-dabc-4fca-a768-1304862ea15a" allowscriptaccess="always"  WMODE="transparent"  type="application/x-shockwave-flash" align="middle" height="250px" width="250px"></embed></OBJECT> <NOSCRIPT><A HREF="http://ws.amazon.com/widgets/q?ServiceVersion=20070822&#038;MarketPlace=US&#038;ID=V20070822%2FUS%2Fwwwadityaravi-20%2F8003%2Fe9b08b52-dabc-4fca-a768-1304862ea15a&#038;Operation=NoScript">Amazon.com Widgets</A></NOSCRIPT>Of course, There is a LOT more to pool than just aiming or running tables. The fact is, even after learning to aim well, there is still so much to learn and do. Better cue ball control, better position play, better safety play, better kick shots, better bank shots, learning breakout shots. Once you have mastered the basics, I recommend more <a title="10 Minute Pool Warmup – How to get into the zone" href="http://www.adityaravishankar.com/2009/07/10-minute-pool-warmup-how-to-get-into-the-zone/">advanced warmup drills</a> to fine tune your stroke and position play.</p>
<p>At the professional level, its no longer about just shooting skill, but also the mental game of billiards. Once you get good at aiming, bigger things start mattering – such as <a title="Playing under pressure – Using your emotions to get more focus" href="http://www.adityaravishankar.com/2009/07/playing-under-pressure-using-your-emotions-to-get-more-focus/">controlling your own emotions</a>, playing against more experienced players with defensive play or learning to face new experiences and situations that you haven’t faced before.</p>
<p>How far you decide to go and where you decide to stop learning depends entirely on your own journey and motivation behind playing pool.</p>
<p>&#8212;</p>
<p>If you have any questions or are interested in more details about these steps, please leave a comment below. Based on feedback, I can put together a more detailed guide.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/2UGYop3lzOtIK3IEEMr3lRYmkBk/0/da"><img src="http://feedads.g.doubleclick.net/~a/2UGYop3lzOtIK3IEEMr3lRYmkBk/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/2UGYop3lzOtIK3IEEMr3lRYmkBk/1/da"><img src="http://feedads.g.doubleclick.net/~a/2UGYop3lzOtIK3IEEMr3lRYmkBk/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/AdityaRaviShankar/~4/diLhqg_rnQg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.adityaravishankar.com/2011/09/learn-how-to-play-pool-well-in-under-30-minutes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.adityaravishankar.com/2011/09/learn-how-to-play-pool-well-in-under-30-minutes/</feedburner:origLink></item>
		<item>
		<title>Aiming without Aiming Part III – A system for making long cut shots</title>
		<link>http://feedproxy.google.com/~r/AdityaRaviShankar/~3/dO-N1ORAyBY/</link>
		<comments>http://www.adityaravishankar.com/2010/06/aiming-without-aiming-part-iii-a-system-for-making-long-cut-shots/#comments</comments>
		<pubDate>Tue, 22 Jun 2010 08:43:53 +0000</pubDate>
		<dc:creator>Adi</dc:creator>
				<category><![CDATA[Billiards]]></category>
		<category><![CDATA[aiming]]></category>
		<category><![CDATA[billiards]]></category>
		<category><![CDATA[pool]]></category>

		<guid isPermaLink="false">http://dev.adityaravishankar.com/?p=50</guid>
		<description><![CDATA[Amazon.com WidgetsI recently moved to India where snooker tables are a lot more common than pool tables. The pockets are very tight (1.3 balls at the most) and the rails are incredibly unforgiving. Also the tables are much larger than &#8230; <a href="http://www.adityaravishankar.com/2010/06/aiming-without-aiming-part-iii-a-system-for-making-long-cut-shots/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><OBJECT class='alignright' classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab" id="Player_e9b08b52-dabc-4fca-a768-1304862ea15a"  WIDTH="250px" HEIGHT="250px"> <PARAM NAME="movie" VALUE="http://ws.amazon.com/widgets/q?ServiceVersion=20070822&#038;MarketPlace=US&#038;ID=V20070822%2FUS%2Fwwwadityaravi-20%2F8003%2Fe9b08b52-dabc-4fca-a768-1304862ea15a&#038;Operation=GetDisplayTemplate"><PARAM NAME="quality" VALUE="high"><PARAM NAME="bgcolor" VALUE="#FFFFFF"><PARAM NAME="allowscriptaccess" VALUE="always"><PARAM NAME="WMODE" VALUE="transparent"><embed src="http://ws.amazon.com/widgets/q?ServiceVersion=20070822&#038;MarketPlace=US&#038;ID=V20070822%2FUS%2Fwwwadityaravi-20%2F8003%2Fe9b08b52-dabc-4fca-a768-1304862ea15a&#038;Operation=GetDisplayTemplate" id="Player_e9b08b52-dabc-4fca-a768-1304862ea15a" quality="high" bgcolor="#ffffff" name="Player_e9b08b52-dabc-4fca-a768-1304862ea15a" allowscriptaccess="always"  WMODE="transparent"  type="application/x-shockwave-flash" align="middle" height="250px" width="250px"></embed></OBJECT> <NOSCRIPT><A HREF="http://ws.amazon.com/widgets/q?ServiceVersion=20070822&#038;MarketPlace=US&#038;ID=V20070822%2FUS%2Fwwwadityaravi-20%2F8003%2Fe9b08b52-dabc-4fca-a768-1304862ea15a&#038;Operation=NoScript">Amazon.com Widgets</A></NOSCRIPT>I recently moved to India where snooker tables are a lot more common than pool tables. The pockets are very tight (1.3 balls at the most) and the rails are incredibly unforgiving. Also the tables are much larger than pool tables (either 10 feet or 12 feet long). As a result playing pool on snooker tables is more about shot making ability and less about position play that uses cheating of pockets.</p>
<p>Having played pool for so long, I saw that I played fairly well as long as I played within half a table (short to medium range game) however I found it incredibly difficult to keep up with others when it came to making cut shots 8 feet away. People with no positional ability could destroy me using just their shot making ability.</p>
<p><img class="alignright size-full wp-image-74" title="New Billiards Aiming System" src="http://www.adityaravishankar.com/wp-content/uploads/2010/06/New-Aiming-System.jpeg" alt="Billiards Parallel Line Aiming System" width="143" height="300" /></p>
<p>The <a href="http://www.adityaravishankar.com/2010/02/aiming-without-aiming-part-ii-how-i-really-aim-a-billiards-shot/" title="Aiming without Aiming Part II – How I really aim a billiards shot">ghost ball system</a> just fails at those kind of distances. It is incredibly hard to aim at the center of an imaginary ball 6-8 feet away and hit it perfectly. I found myself missing the pocket by as much as a foot unless I was concentrating a lot. It was also very tiring, both physically and mentally.</p>
<p>While looking around for ideas to improve my long distance shooting, I discovered the parallel line aiming system. It works beautifully for shots that are at the other end of the table. When combined with the ghost ball system, it also works really well for short distance shots.</p>
<p>This is how the parallel line system works.</p>
<ol>
<li>Draw a line from the center of the pocket to the center of the object ball and extend it to the opposite side. This point (A) is where the object ball needs to be hit by the cue ball.</li>
<li>Draw another line parallel to the first, passing through the cue ball and identify the point (B) on the cue ball that needs to hit the object ball</li>
<li>Align yourself along the line from B to A, and visualize the point B hitting the point A and pushing the object ball into the pocket.</li>
</ol>
<p><OBJECT class='alignleft' classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab" id="Player_e9b08b52-dabc-4fca-a768-1304862ea15a"  WIDTH="250px" HEIGHT="250px"> <PARAM NAME="movie" VALUE="http://ws.amazon.com/widgets/q?ServiceVersion=20070822&#038;MarketPlace=US&#038;ID=V20070822%2FUS%2Fwwwadityaravi-20%2F8003%2Fe9b08b52-dabc-4fca-a768-1304862ea15a&#038;Operation=GetDisplayTemplate"><PARAM NAME="quality" VALUE="high"><PARAM NAME="bgcolor" VALUE="#FFFFFF"><PARAM NAME="allowscriptaccess" VALUE="always"><PARAM NAME="WMODE" VALUE="transparent"><embed src="http://ws.amazon.com/widgets/q?ServiceVersion=20070822&#038;MarketPlace=US&#038;ID=V20070822%2FUS%2Fwwwadityaravi-20%2F8003%2Fe9b08b52-dabc-4fca-a768-1304862ea15a&#038;Operation=GetDisplayTemplate" id="Player_e9b08b52-dabc-4fca-a768-1304862ea15a" quality="high" bgcolor="#ffffff" name="Player_e9b08b52-dabc-4fca-a768-1304862ea15a" allowscriptaccess="always"  WMODE="transparent"  type="application/x-shockwave-flash" align="middle" height="250px" width="250px"></embed></OBJECT> <NOSCRIPT><A HREF="http://ws.amazon.com/widgets/q?ServiceVersion=20070822&#038;MarketPlace=US&#038;ID=V20070822%2FUS%2Fwwwadityaravi-20%2F8003%2Fe9b08b52-dabc-4fca-a768-1304862ea15a&#038;Operation=NoScript">Amazon.com Widgets</A></NOSCRIPT>I spent the last couple of weeks practicing with the new system (and also perfecting my stroke). Using precise points instead of imaginary ghost ball centers makes it easier for the subconscious mind to aim at the target. I found it took much less concentration to shoot using this system and within a few days I was able to <a href="http://www.adityaravishankar.com/2009/07/aiming-without-aiming-how-to-shoot-pool-like-a-pro/" title="Aiming without Aiming – How to shoot pool like a pro">aim and align shots subconsciously</a> again. This has made it much easier to keep up with others on the big table. Now that my aiming is more confident, I can use stroke and top/bottom english to position the ball around the table again and am able to run more balls.</p>
<p>If you are having trouble with the ghost ball system or are uncomfortable with long cut shots, then give this system a try.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/cilaHWJ1uDPX-X11VhIxJu6mO6Y/0/da"><img src="http://feedads.g.doubleclick.net/~a/cilaHWJ1uDPX-X11VhIxJu6mO6Y/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/cilaHWJ1uDPX-X11VhIxJu6mO6Y/1/da"><img src="http://feedads.g.doubleclick.net/~a/cilaHWJ1uDPX-X11VhIxJu6mO6Y/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/AdityaRaviShankar/~4/dO-N1ORAyBY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.adityaravishankar.com/2010/06/aiming-without-aiming-part-iii-a-system-for-making-long-cut-shots/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.adityaravishankar.com/2010/06/aiming-without-aiming-part-iii-a-system-for-making-long-cut-shots/</feedburner:origLink></item>
		<item>
		<title>Projection as a Mirror – How to eliminate anger, jealousy and resentment from your life</title>
		<link>http://feedproxy.google.com/~r/AdityaRaviShankar/~3/sTnTTWCcjwQ/</link>
		<comments>http://www.adityaravishankar.com/2010/04/projection-as-a-mirror-how-to-eliminate-anger-jealousy-and-resentment-from-your-life/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 08:51:38 +0000</pubDate>
		<dc:creator>Adi</dc:creator>
				<category><![CDATA[Self Actualization]]></category>
		<category><![CDATA[happiness]]></category>
		<category><![CDATA[self improvement]]></category>

		<guid isPermaLink="false">http://dev.adityaravishankar.com/?p=63</guid>
		<description><![CDATA[Carl Jung and Sigmund Freud talked about the concept of projection and the shadow self – All of us tend to project or see in others, qualities that we ourselves possess. The things we admire and respect most in others &#8230; <a href="http://www.adityaravishankar.com/2010/04/projection-as-a-mirror-how-to-eliminate-anger-jealousy-and-resentment-from-your-life/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Carl Jung and Sigmund Freud talked about the concept of projection and the shadow self – All of us tend to project or see in others, qualities that we ourselves possess.</p>
<p>The things we admire and respect most in others are usually our own strengths. Sometimes these are strengths that we don’t even realize we have, or qualities that we have that aren’t developed fully. When we see these qualities we sometimes say things like “He is so amazing. I wish I could be like him”. What we don’t realize is that we have within ourselves the same ability or potential for it, otherwise we wouldn’t even notice it in the other person. Excitement, happiness, or admiration are usually signs that our subconscious mind realizes that we can learn a lot from this person and develop our abilities.</p>
<p>Similarly what we dislike the most about others are usually our own weaknesses. Often these are weaknesses that we are afraid to admit to ourselves or don’t even realize we have. We tend to get angry and resentful at others when we see these qualities. Sometimes the person we project them on may not even have the qualities we attribute to them. For example, a selfish person believes that any person he talks to is selfish too. Often the anger we express at this person is usually resentment at ourselves (sometimes without our being aware of it).</p>
<p>Whenever we are exposed to qualities that remind our subconscious mind of our own, it usually pushes buttons and triggers strong emotions. Any time we feel strong emotions like anger, it is usually a sign that projection is at work.</p>
<p>When I first learned about projection, it was through a simple exercise. Take some time to try it out.</p>
<p>Think about any person (it can be more than one) that you really admire and write down five qualities that you like the most about them.<br />
Now think about someone you really dislike, and write down five qualities that you hate the most about them.<br />
Done? Now take a look at these qualities carefully.<br />
I’d like you to open your mind and consider the possibility, that these good qualities are your own strengths that you have not yet developed fully, and the bad ones are your own weaknesses that you deny or still haven’t admitted to yourself. Even if it seems a little hard to accept, take some time to think about what it would mean if it were true.<br />
I found the results of the exercise incredibly enlightening. I decided to modify it slightly and apply the concept to my own personal growth.</p>
<p>Since then, anytime I find strong emotions being triggered, I try to figure out what strength or weakness of my own I am projecting. For example, if I get angry at someone for being pushy, I consider the possibility that I can be pushy and subconsciously resent it about myself. If I admire someone for being extremely talented at something, I realize that I too have the potential to be that good if I give myself time and learn from that person.</p>
<p>The best thing about the exercise is once you become aware of the quality and acknowledge it, you no longer have to do anything else. Just becoming aware of it helps it auto correct.</p>
<p>Over the last few years, I spent time for introspection after any argument to try and discover why it happened and what it taught me about myself. I learned to be grateful for people who pushed my buttons because they helped me to learn more about myself and help me grow. I realized that as I discovered these buttons and became aware of them, they stopped becoming buttons and no longer affected me. I found myself getting angry less often and stayed calm and happy more of the time.</p>
<p>This one idea has helped me in my personal and emotional development more than anything else I know. If you find that you have a lot of anger and resentment and would like to bring more peace and happiness into your life, I believe you should give this a shot. It may change your life.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/sjLopr4yABG_6bvUTO72spU8rVo/0/da"><img src="http://feedads.g.doubleclick.net/~a/sjLopr4yABG_6bvUTO72spU8rVo/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/sjLopr4yABG_6bvUTO72spU8rVo/1/da"><img src="http://feedads.g.doubleclick.net/~a/sjLopr4yABG_6bvUTO72spU8rVo/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/AdityaRaviShankar/~4/sTnTTWCcjwQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.adityaravishankar.com/2010/04/projection-as-a-mirror-how-to-eliminate-anger-jealousy-and-resentment-from-your-life/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.adityaravishankar.com/2010/04/projection-as-a-mirror-how-to-eliminate-anger-jealousy-and-resentment-from-your-life/</feedburner:origLink></item>
	</channel>
</rss>

