search the site  

Enter search terms:


subscribe to newsletters   WebReference.com logo   WebReference.com
dev the Web
tip archive  •   about  •   contact  •   jobs  •   sitemap


Search
The Business Internet


[next]

Simple Game Programming In JavaScript

By Lisha Sterling

Digg This Add to del.icio.us

ASP 3.0/.NET Developer
Jupitermedia
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

In the previous article you learned how to create an animation with JavaScript using a timer mechanism, an image of a ball and CSS styles. This time we'll make some adjustments to the CSS and the document object model (DOM) to create a simple ping-pong game. We'll also be using keyboard input and events. With these tools you'll be able to create different games in JavaScript, such as Breakout, Tetris, Frogger or your dream game.

First, let's look at what our game will contain when we're done with this article:

Now, let's look at some basic things that won't be in our game (yet):

The first two of these are fairly simple to add, but would make this article too long. The third requires an AJAX version of this game which is also beyond the scope of this article. The fourth shortcoming concerns advanced event handling in JavaScript. We'll take a deeper look at the problems and the solution for multiple event handling in another article.

Let's start out by creating the HTML and drawing the playing court. Last time we used the entire window for our animation, and the ball bounced around against the inside boundaries of the browsers. This time we're going to use a div inside our browser window as our court. We'll draw a blue court of 400 x 750 pixels, with a dashed centerline. The outside boundaries of the court will be 4 pixels wide. The center line will be only 1 pixel wide.

HTML page:

Now, let's add in the ball and the paddles. The ball will start on the left side of the court, near the top, ready to be "served" by the left paddle. The two paddles will start out on their respective sides of the court, 15 pixels away from the edge of the div. You'll notice that the left position for the right paddle is 725 pixels, 25 pixels away from the edge of the court. The paddle is 10 pixels wide, though, so the right side of the paddle is 15 pixels away from the right edge of the court.

HTML changes:


[next]

Recent Articles

WebReference.com site name
How to Create an Ajax Autocomplete Text Field: Part 6
Software Engineering for Ajax
Perl Pragma Primer
internet.com site name
Using File Virtualization for Disaster Recovery
VoIP Security: SIP—Versatile but Vulnerable
Around the World in 80 Nodes

Access FREE Mainsoft Tools:
Whitepaper:
Integrate SharePoint into WebSphere Portal
Demo:
Integrate SharePoint and .NET into WebSphere Portal
Tutorial: Integrate SharePoint Data into WebSphere Portal & Build a Composite Application Across SharePoint, .NET, & Java Tutorial:
Create WebSphere Portal Applications using the Visual Studio IDE



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES