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

<channel>
	<title>Web Cash &#187; input</title>
	<atom:link href="http://www.earn-web-cash.com/tag/input/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.earn-web-cash.com</link>
	<description>Writing, Designing, and Making Money Online</description>
	<lastBuildDate>Sun, 04 Dec 2011 22:52:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>Javascript Game Spotlight: 3d Worm</title>
		<link>http://www.earn-web-cash.com/2008/02/23/javascript-3d-worm/</link>
		<comments>http://www.earn-web-cash.com/2008/02/23/javascript-3d-worm/#comments</comments>
		<pubDate>Sat, 23 Feb 2008 15:09:07 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Game Design]]></category>
		<category><![CDATA[Sundry Musings]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[input]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/23/javascript-3d-worm/</guid>
		<description><![CDATA[I was browsing the <a href="http://www.frihost.com" rel="nofollow">Frihost</a> forums this morning, and I stumbled on a cool Javascript game.

One of the Frihost users and his friend made a <a href="http://tekage.com.ar/uploaded/3Dworm/">3d Worm game</a>.  Although the directions are in Spanish (as is the source code), the game is pretty cool.  It's a good example of what you can accomplish with some Javascript/DOM trickery.]]></description>
			<content:encoded><![CDATA[<p><a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/3dworm.png' title='3d Worm Screenshot'><img class="alignright" src='http://www.earn-web-cash.com/wp-content/uploads/2008/02/3dworm.thumbnail.png' alt='Screenshot of a 3d Worm game written in Javascript.' /></a>I was browsing the <a href="http://www.frihost.com" rel="nofollow">Frihost</a> forums this morning, and I stumbled on a cool Javascript game.</p>
<p>One of the Frihost users and his friend made a <a href="http://tekage.com.ar/uploaded/3Dworm/">3d Worm game</a>.  Although the directions are in Spanish (as is the source code), the game is pretty cool.  It&#8217;s a good example of what you can accomplish with some Javascript/DOM trickery.<br />
<span id="more-153"></span><br />
Playing the game is simple enough.  You use the four arrow keys and the Q/A key to pick one of six directions to go in.</p>
<p>At first, it can be very difficult to gauge the &#8220;3d&#8221; position of the apple.  Once you learn to use the shadows to your advantage, the game becomes pretty easy.  It&#8217;s still fun, though, and a great example of how Javascript can be used to make an interface for a game.</p>
<p>You see a few cool things here.</p>
<p>For one, the script captures keyboard input to drive the action.  Rather than clicking on buttons/boxes on the screen, the user can hit the appropriate keys and the game responds.  This is important for making a game feel &#8220;real&#8221; and not a kind of mash-up.</p>
<p>A lot of the positioning is done with &#8220;position: absolute&#8221; and the &#8220;top&#8221;/&#8221;left&#8221; styles.  This let&#8217;s you use the screen as a kind of canvas &#8211; drawing the images where-ever you need them.  It frees you from the constraints of normal HTML positioning, where you need to float/margin/padding stuff to get it to line up where you want.</p>
<p>It&#8217;s possible to read source code written in another language.  I just find this interesting.  The source is written in Spanish &#8211; so most of the variable/function names don&#8217;t mean a lot to me.  However, the code is pretty straightforward so it&#8217;s still easy enough to follow what&#8217;s going on.</p>
<p>Well go play some Worm.  This has inspired me to go do some coding.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/23/javascript-3d-worm/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>JS Game: Making a Real Game Board from a Concept</title>
		<link>http://www.earn-web-cash.com/2008/02/20/js-game-board-complete/</link>
		<comments>http://www.earn-web-cash.com/2008/02/20/js-game-board-complete/#comments</comments>
		<pubDate>Thu, 21 Feb 2008 04:08:57 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Game Design]]></category>
		<category><![CDATA[HTML Tutorials]]></category>
		<category><![CDATA[JS Tutorials]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[input]]></category>
		<category><![CDATA[js]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/20/js-game-board-complete/</guid>
		<description><![CDATA[In a recent post, I sketched out how to use Javascript to build a gameboard for a browser-based game &#8211; specifically Tic Tac Toe. After a little tweaking, I&#8217;ve worked this into a functioning Tic Tac Toe board. In this post, we&#8217;ll explore the code used to set up this board. Once that&#8217;s done, the [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent post, I sketched out how to <a href="http://www.earn-web-cash.com/2008/02/19/js-online-game-board/">use Javascript to build a gameboard for a browser-based game</a> &#8211; specifically Tic Tac Toe.  After a little tweaking, I&#8217;ve worked this into a functioning Tic Tac Toe board.</p>
<p>In this post, we&#8217;ll explore the code used to set up this board.  Once that&#8217;s done, the next step will be to create a php script to evaluate some of the game&#8217;s logic.<br />
<span id="more-136"></span></p>
<h5>Recap: The Game Board Concept</h5>
<p>First, let&#8217;s take a step back and recap our Javascript game board concept.</p>
<p>The basic building block of our game board is a div element.  This div element is styled with a CSS stylesheet &#8211; 50px by 50px.  By using floats and clearss strategically, we can stack 9 of these div elements into a 3&#215;3 box.</p>
<p>Javascript is added to make these boxes functional.  An &#8220;onClick&#8221; handler is used to call a Javascript function that changes the background image of the div.  This effectively marks the square as either a &#8220;Circle&#8221; or &#8220;Cross&#8221; for Tic Tac Toe.</p>
<p>To alter the div &#8211; and also to add textual output &#8211; we&#8217;ll be using Javascript&#8217;s powerful DOM functions.  These allow us to grab a specific element by it&#8217;s ID and change it.</p>
<h5>Creating Graphics</h5>
<p><a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/tttcircle.png' title='Tic Tac Toe Circle Image'><img class="alignright" src='http://www.earn-web-cash.com/wp-content/uploads/2008/02/tttcircle.png' alt='Tic Tac Toe Circle Image' /></a>I created two images in Inkscape to use for this board.  Each is a 50&#215;50 png file &#8211; with a gray background and a black border.  The actual token (the circle or cross) is red and laid on top of the gray background.</p>
<p><a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/tttcross.png' title='Tic Tac Toe Cross Image'><img class="alignright" src='http://www.earn-web-cash.com/wp-content/uploads/2008/02/tttcross.png' alt='Tic Tac Toe Cross Image' /></a>I&#8217;m no great artist, but these should work for a functional demo.  We can worry about making nice circle and cross tokens later.  I think getting the game <strong>working</strong> is more important than making it pretty from the get-go.</p>
<h5>Turning Our .js Script into a Class</h5>
<p>In making this a functional board, I also converted the script into an actual class.  The script may end up being somewhat large and unwieldy &#8211; and an object oriented approach may help us keep it tidy and clean.  Or it may add a lot of overhead&#8230; but I like objects.</p>
<p>In our HTML file, we&#8217;ll create the object like this.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
var tictactoe = new game();
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>In the attached .js file, we actually define the <strong>game</strong> object.  Here&#8217;s part of the object definition.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> game<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">//  Array to hold the bgImgs</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">bgImgs</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">bgImgs</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'tttcircle.png'</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">bgImgs</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'tttcross.png'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">//  Player information			</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentPlayer</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;">players</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">players</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Player One&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">players</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Player Two&quot;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span></pre></div></div>

<p>This class constructor does some of the initialization for us.</p>
<p>First, it creates an array with background images.  At the moment we&#8217;re only using two images.  However, this technique would be useful if you had a more complex map &#8211; with 10-15 images you could lay over a div.</p>
<p>Second, we create some player information variables.  The &#8216;currentPlayer&#8217; property is going to track whether the &#8216;cross&#8217; or &#8216;circle&#8217; player is currently taking a turn.  The &#8216;players&#8217; array will just hold the names of those players for now.</p>
<h5>The Background Image Changing Function</h5>
<p>One of the major methods of this class will be changeBackground().  Just like in the previous example, this method will change the background style of a given div tag.  This way we can change it from an open square to a circle or cross token.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">changeBackground</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>boxId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> box <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'box-'</span> <span style="color: #339933;">+</span> boxId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    box.<span style="color: #660066;">style</span>.<span style="color: #660066;">background</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'transparent url('</span> <span style="color: #339933;">+</span> 
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">bgImgs</span><span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentPlayer</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">') top left no-repeat'</span><span style="color: #339933;">;</span>
&nbsp;
    box.<span style="color: #660066;">removeAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'onClick'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">changePlayer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>This should be pretty straightforward.  We&#8217;re storing the &#8216;div&#8217; element in the &#8216;box&#8217; variable.  We&#8217;re then setting the &#8216;background&#8217; style as we would in a css style.  Remember that we stored the background images in an array (this.bgImgs) and this.currentPlayer corresponds to a key in the this.bgImgs array (either 0 or 1).</p>
<p>The &#8216;box.removeAttribute()&#8217; method is removing &#8216;onClick&#8217; from that div.  We can&#8217;t use a square a second time, so we might as well eliminate the onClick handler altogether.</p>
<p>Finally, this.changePlayer() is calling a new method.  This is going to help us switch from Player One&#8217;s turn to Player Two&#8217;s turn.</p>
<h5>One Turn to Another &#8211; this.changePlayer()</h5>
<p>The last method we need to declare for this class at the moment is changePlayer.</p>
<p>This will toggle the active player &#8211; which in turn affects whether a circle or cross is placed on the board.  For some added effect, we&#8217;ll also create a new html element to display a message that says who&#8217;s turn it is.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">changePlayer</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>
    <span style="color: #006600; font-style: italic;">//  Switch the active player</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentPlayer</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentPlayer</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentPlayer</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">//  Get a reference to our 'message' element and create the message</span>
    <span style="color: #003366; font-weight: bold;">var</span> box <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'message'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> msg <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;It is &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">players</span><span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentPlayer</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;'s turn.&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> txt <span style="color: #339933;">=</span> document.<span style="color: #660066;">createTextNode</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">//  Erase any existing text</span>
    <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>box.<span style="color: #660066;">hasChildNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      box.<span style="color: #660066;">removeChild</span><span style="color: #009900;">&#40;</span>box.<span style="color: #660066;">lastChild</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">//  Add the text node (our message) to our element</span>
    box.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>txt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Again, this is pretty straightforward.  The DOM functions are amazingly simple &#8211; once you see how they work.</p>
<p>&#8216;box&#8217; is a reference to our element (id = message).  The msg variable is a temp variable I created to hold the string.  The &#8216;createTextNode&#8217; method creates a new block of text (with our msg) that we can then insert into an HTML element.</p>
<p>The while() loop is simply there to erase any old text.  As long as our &#8216;box&#8217; element has any child nodes inside of it (text or other HTML tags), the loop will execute and delete one of those child nodes each time.  This way we have a clean slate on which to write down who&#8217;s turn it is.</p>
<p><script type="text/javascript">var tictactoe = new game();</script></p>
<p style="float: left; margin-right: 1em;" class="message break" id="message">
<div class="break">
<div class="box" id="box-1" onClick="tictactoe.changeBackground('1');">1</div>
<div class="box" id="box-2" onClick="tictactoe.changeBackground('2');">2</div>
<div class="box" id="box-3" onClick="tictactoe.changeBackground('3');" style="margin-right: 1em;">3</div>
</div>
<div class="break">
<div class="box" id="box-4" onClick="tictactoe.changeBackground('4');">4</div>
<div class="box" id="box-5" onClick="tictactoe.changeBackground('5');">5</div>
<div class="box" id="box-6" onClick="tictactoe.changeBackground('6');" style="margin-right: 1em;">6</div>
</div>
<div class="break">
<div class="box" id="box-7" onClick="tictactoe.changeBackground('7');">7</div>
<div class="box" id="box-8" onClick="tictactoe.changeBackground('8');">8</div>
<div class="box" id="box-9" onClick="tictactoe.changeBackground('9');" style="margin-right: 1em;">9</div>
</div>
<h5>Seeing It In Action</h5>
<p>Well, that&#8217;s really all there is to it.  Have a look at this functioning demo.  If you had trouble piecing together the example code, you can download the source files (.js, .css, and two .png) from the list below.</p>
<p>Good luck, and be sure to check back soon.  Our next task is going to be a <strong>real</strong> challenge.  We need to add some <strong>logic</strong> to this game &#8211; evaluating victoary conditions and creating AI!</p>
<p>Source code files:</p>
<ul>
<li><a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/tictactoe-basic.js' title='Basic Tic-Tac-Toe JS File'>Basic Tic-Tac-Toe JS File</a></li>
<li><a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/tictactoe1.css' title='Tic-Tac-Toe CSS File (#2)'>Tic-Tac-Toe CSS File (#2)</a></li>
<li><a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/tttcross.png' title='Tic Tac Toe Cross Image'>Tic Tac Toe Cross Image</a></li>
<li><a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/tttcircle.png' title='Tic Tac Toe Circle Image'>Tic Tac Toe Circle Image</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/20/js-game-board-complete/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>JS Game: Creating a Simple Game Board in Javascript</title>
		<link>http://www.earn-web-cash.com/2008/02/19/js-online-game-board/</link>
		<comments>http://www.earn-web-cash.com/2008/02/19/js-online-game-board/#comments</comments>
		<pubDate>Tue, 19 Feb 2008 23:46:49 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Game Design]]></category>
		<category><![CDATA[HTML Tutorials]]></category>
		<category><![CDATA[JS Tutorials]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[input]]></category>
		<category><![CDATA[js]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/19/js-online-game-board/</guid>
		<description><![CDATA[A thorough knowledge of Javascript can give you tremendous control over what happens on a website. Combined with PHP through an AJAX request, you can make some pretty nifty browser-based games. This tutorial will focus on the first part of making a basic board game &#8211; creating a board. The end result will be a [...]]]></description>
			<content:encoded><![CDATA[<p>A thorough knowledge of Javascript can give you tremendous control over what happens on a website.  Combined with PHP through an AJAX request, you can make some pretty nifty browser-based games.</p>
<p>This tutorial will focus on the first part of making a basic board game &#8211; creating a board.  The end result will be a three by three game board suitable for use in developing a tic-tac-toe game.<br />
<span id="more-132"></span></p>
<h5>Game Development with JS, AJAX, and PHP</h5>
<p>How can we use JS and PHP &#8211; connected through AJAX &#8211; to develop an online game?</p>
<p>Javascript&#8217;s main use for us is to create an interface for the game.  Through Javascript we can capture user input &#8211; i.e. mouse clicks and text.  We can also alter the game&#8217;s output &#8211; changing the HTML of the page, adding images, and moving things around.  Javascript will provide a lot of the front end work.</p>
<p>PHP, on the other hand, is a more robust language for dealing with the logic of the game.  PHP could be useful for developing an AI and evaluating winning conditions.  It also offers a great way to store information for later use through flat files or database integration.</p>
<p>If we&#8217;re going to use both Javascript and PHP, we&#8217;ll need to use AJAX.  This is the glue that holds the whole thing together.  It will send input from the main Javascript to the PHP processing scripts.  The PHP script will then send info back to the Javascript and it will alter the page&#8217;s layout accordingly.</p>
<h5>Step One: Building a Game Board</h5>
<p>The first step in creating a graphical online game is to build a game board.  The most basic board I could think of &#8211; and therefore a good starting point &#8211; is a tic-tac-toe board.</p>
<p>First, we should define and create our basic building block.  A square.</p>
<p>In terms of HTML/CSS, our square will be a div tag.  We&#8217;ll be able to position and style it to our liking, and we can add javascript triggers like &#8220;onClick&#8221; to help run our Javascript.</p>
<div class="basicbox" id="box" onClick="makeBlack('box');"></div>
<p>Our first box is going to be a simple 50px x 50px square.  When you click it, it will change background colors to black.</p>
<p>Here&#8217;s the CSS we&#8217;ll use for the square.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.box</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">50px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">50px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">thin</span> <span style="color: #993333;">solid</span> <span style="color: #000000; font-weight: bold;">black</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Inside the html body we can create our box like this.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack();&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>Finally, we need to define our Javascript function makeBlack().  We&#8217;ll access the div element through it&#8217;s ID (&#8216;box&#8217;) and change it&#8217;s background style to be black.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> makeBlack<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> box <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'box'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  box.<span style="color: #660066;">style</span>.<span style="color: #660066;">background</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'black'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now we&#8217;ve got a building block!  We just successfully used Javascript and DOM to create a click-able box.  With some tweaking, we can turn this into a nifty 3&#215;3 grid for Tic Tac Toe.</p>
<h5>Step Two: Build a Grid</h5>
<p>For Tic Tac Toe, we&#8217;re going to need a 3&#215;3 grid.  We can modify our CSS a bit and create 9 separate divs to accomplish this.  Add the following lines to the CSS for .box, and create a new class .break.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.box</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.break</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">clear</span><span style="color: #00AA00;">:</span> bloth<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>This way our boxes line up in a row.  If we include a &#8220;break&#8221; class in our div, we can start a new line.  Therefore we should update the html like this.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box break&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;one&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('one');&quot;</span>&gt;</span>1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;two&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('two');&quot;</span>&gt;</span>2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;three&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('three');&quot;</span>&gt;</span>3<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box break&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;four&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('four');&quot;</span>&gt;</span>4<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;five&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('five');&quot;</span>&gt;</span>5<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;six&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('six');&quot;</span>&gt;</span>6<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box break&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;seven&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('seven');&quot;</span>&gt;</span>7<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;eight&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('eight');&quot;</span>&gt;</span>8<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;nine&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('nine');&quot;</span>&gt;</span>9<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>Notice that each box has it&#8217;s own unique ID &#8211; one to nine.  I also changed the makeBlack() function to include a parameter &#8211; an ID.  Let&#8217;s update the makeBlack() function to utilize this to change the specific box that we click on.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> makeBlack <span style="color: #009900;">&#40;</span>boxId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> box <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>boxId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  box.<span style="color: #660066;">style</span>.<span style="color: #660066;">background</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'black'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<div class="break">
<div class="box" id="one" onClick="makeBlack('one');">1</div>
<div class="box" id="two" onClick="makeBlack('two');">2</div>
<div class="box" id="three" style="margin-right: .5em;" onClick="makeBlack('three');">3</div>
</div>
<div class="break">
<div class="box" id="four" onClick="makeBlack('four');">4</div>
<div class="box" id="five" onClick="makeBlack('five');">5</div>
<div class="box" id="six" style="margin-right: .5em;" onClick="makeBlack('six');">6</div>
</div>
<div class="break">
<div class="box" id="seven" onClick="makeBlack('seven');">7</div>
<div class="box" id="eight" onClick="makeBlack('eight');">8</div>
<div class="box" id="nine" style="margin-right: .5em;" onClick="makeBlack('nine');">9</div>
</div>
<h5>Step Three: Use a Background Image Instead of a Background Color</h5>
<p>Turning a box to a black background color isn&#8217;t that impressive.  But it&#8217;s easy to use as a demonstration.</p>
<p>For a game, it would be much more useful to make an image appear in our div.  We can do that by editing the &#8220;background&#8221; style of the div in the makeBlack() function.  Note: You might also want to change the name of the function to something more descriptive, like changeBackground().</p>
<p>Here&#8217;s how to do it.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> changeBackground <span style="color: #009900;">&#40;</span>boxId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> box <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>boxId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  box.<span style="color: #660066;">style</span>.<span style="color: #660066;">background</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'transparent url(image.png) top left no-repeat'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The property &#8220;box.style.background&#8221; accesses the &#8220;background&#8221; css style for the &#8220;box&#8221; id.  Give it a value just like you would in a css stylesheet.  In this case, we&#8217;re giving it a background of &#8220;image.png.&#8221;  You might want to change this to a &#8220;circle.png&#8221; or a &#8220;cross.png&#8221; that you create for your game.</p>
<h5>Step Four: Changing the onClick Event</h5>
<p>The last thing we want to look at in this introduction is how to change the onClick event for our div.  You might want to get rid of it altogether &#8211; or maybe change it to a new function.  For example, after turning the box black, you might want to make the onClick call a new function &#8211; makeWhite().</p>
<p>We can do this through another DOM method, similar to the way we accessed the styles before.  Assuming we have the same variable box that refers to our div, here&#8217;s what we need to do in our function.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//  Get rid of onClick altogether</span>
box.<span style="color: #660066;">removeAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'onClick'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//  Change onClick to a new function</span>
box.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'onClick'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'makeWhite();'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h5>Where To Go From Here?</h5>
<p>Now that you&#8217;ve got a basic 3&#215;3 grid and you know how to make it respond to user input, you can get started on making a game.</p>
<p>Here are a few of the things you&#8217;ll want to think about next.</p>
<ul>
<li>I need some images to place in my boxes.</li>
<li>How Can I Alternate Players (and images)?</li>
<li>How Can I Determine Who Wins?</li>
<li>How Can I Make the Computer Play (AI!)?</li>
</ul>
<p>You can go ahead and get started on your own &#8211; but I&#8217;ll certainly return to this topic within the next week or two with some more examples.  After the next tutorial, we&#8217;ll have a working <a href="http://www.earn-web-cash.com/2008/02/20/js-game-board-complete/">Tic-Tac-Toe board driven solely by Javascript</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/19/js-online-game-board/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
		<item>
		<title>Use Attribute Selectors to Fine Tune Your CSS</title>
		<link>http://www.earn-web-cash.com/2008/01/31/css-attribute-selectors/</link>
		<comments>http://www.earn-web-cash.com/2008/01/31/css-attribute-selectors/#comments</comments>
		<pubDate>Fri, 01 Feb 2008 00:28:01 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[HTML Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[anchor]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[input]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/01/31/css-attribute-selectors/</guid>
		<description><![CDATA[Here's a quick tip for fine tuning your CSS declarations.  

You can use attribute selectors to style an html element that contains an attribute with the value that you declare.  This allows you to do a bunch of cool stuff - like style different "input" types differently, style anchor tags with titles differently, and et]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a quick tip for fine tuning your CSS declarations.  </p>
<p>You can use attribute selectors to style an html element that contains an attribute with the value that you declare.  This allows you to do a bunch of cool stuff &#8211; like style different &#8220;input&#8221; types differently, style anchor tags with titles differently, and etc.<br />
<span id="more-89"></span><br />
How does it work?</p>
<p>An attribute selector follows this basic formula&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #00AA00;">&#91;</span>attribute<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;value&quot;</span><span style="color: #00AA00;">&#93;</span> <span style="color: #00AA00;">&#123;</span> style information... <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Like an id or class selector, you can use it alone (as in the example above), or attach it to a specific html element.</p>
<p>How can we use it?  Here are a few examples.</p>
<h5>Style Links with Title Elements Differently</h5>
<p>You could use this to give users a hint that an anchor tag or link has a title element.  This way they know to hover over the link and read the tooltip.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">a<span style="color: #00AA00;">&#91;</span>title<span style="color: #00AA00;">&#93;</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">border-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">thin</span> <span style="color: #993333;">dotted</span> <span style="color: #000000; font-weight: bold;">blue</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>That declaration gives any anchor tag containing a &#8220;title&#8221; attribute a thin dotted blue underlining.  You may have to tweak it a bit to make sure the normal anchor-underlining doesn&#8217;t interfere with it.</p>
<h5>Style Particular Input Types on Forms</h5>
<p>You can use this to style Submit buttons, Text Inputs, etc. &#8211; independent of other input types.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span><span style="color: #00AA00;">&#93;</span> <span style="color: #00AA00;">&#123;</span>  <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">blue</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>This simple declaration gives all Submit buttons a blue background color.  Fairly silly, but the point is the declaration &#8211; not the actual styling.  You could do something similar with these declarations&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">input <span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;text&quot;</span><span style="color: #00AA00;">&#93;</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #808080; font-style: italic;">/* style information */</span> <span style="color: #00AA00;">&#125;</span>
input <span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;password&quot;</span><span style="color: #00AA00;">&#93;</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #808080; font-style: italic;">/* style information */</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<h5>Style Internal Links Different from External Links</h5>
<p>This is a really nifty trick&#8230; but unfortunately it requires a bit of CSS 3.  This will probably invalidate your CSS, and your mileage may vary.</p>
<p>It&#8217;s still interesting to try out.</p>
<p>You can use &#8220;^=&#8221; instead of &#8220;=&#8221; to signal that the browser should match any element with the given attribute that contains the quoted string.  It doesn&#8217;t have to be an exact match.  So you can use &#8220;^=&#8217;mydomain.com&#8217;&#8221; to match any links that point to the domain you&#8217;re working on.</p>
<p>Like so&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">a<span style="color: #00AA00;">&#91;</span>href<span style="color: #00AA00;">^=</span><span style="color: #ff0000;">&quot;earn-web-cash.com&quot;</span><span style="color: #00AA00;">&#93;</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #808080; font-style: italic;">/* style information */</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>You could use this to given internal links a slightly different color &#8211; some kind of queue to help the user differentiate between internal and external links.</p>
<p>For a longer list of ways to use this neat little trick, check out this post <a href="http://www.search-this.com/2007/11/14/hip-to-be-square/" title="Uses for CSS Attribute Selectors">Hip to Be Square</a>.  There&#8217;s also a list of the different ways to search for values (like ^=) &#8211; which will be valid in CSS 3.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/01/31/css-attribute-selectors/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

