★ wanayoo — archive 1999 http://javaboutique.webdeveloper.com/tutorials/Java_Game_Programming/DasErsteSpielEng.htmlNouvelle recherche | Portail wanayoo
Computer Digital Expo


Find a Web Host With:
CGI Access
DB Support
NT Servers
UNIX Servers
Telnet Access

advanced search
Internet News
Internet Investing
IT
Windows Technology
Linux/Open Source
Developer
Interactive Marketing
xSP Resources
Small Business
Wireless Internet
Downloads
Internet Resources
Internet Lists
International
EarthWeb
Career Resources

Search internet.com
Advertise
Corporate Info
Newsletters
E-mail Offers
Be a Commerce Partner
Web Hosting
Internet Jobs
Online Credit Reports
Software Licenses
Build an Online Store
Best Deals on PDAs!
Shop Digital Cameras
Find a Consultant
Find a Web Host
Submit Your Site
FlashKit
Gif.com
HierMenusCentral
JavaBoutique
JavaScript.com
JavaScriptSource
WebDev Jobs
JustSMIL
ScriptSearch
StreamingMediaWorld
Web Hosts
WebDevelopersJournal WDVL
WebDeveloper.com
WebReference.com
XMLFiles.com
CGI
HTML
Java
JavaScript
Web Design and Promotion
Web Site Development
E-mail Address:


Can you receive
HTML e-mail?

Yes   No

Zip Code:


Tutorials : The Java Game Development Tutorial :

Contents
Introduction
Basic structure of an applet
Move a ball
Double buffering
Ball bouncing and change the direction
Using sound in applets
Using pictures in applets
Mouse events
Keyboard events
The first complete game
Artificial intelligence for a pong like game
Generation of random 2D landscapes

Our first game

The game idea

In our game, the player has to shoot at two balls, (a red and a blue one), which are flying around in our applet. The direction and speed will be chosen at random; the user can hit a ball by clicking on it with the mouse pointer. If a ball reaches a border of the applet without being hit, the player looses one of his 10 lives. If the player loses all of his lives, the game is over.

Outline of classes and methods

The Main class

This class implements all methods concerning the animation of objects (balls) and "management" of the game. These are init(), start(), stop(), paint(), update(), run(), and mouseDown(), to handle mouse click events. The class also manages all objects, (two ball objects, the player object), and the thread in which the game is running!

  1. init(): Initializes all objects, gets audio data, sets mouse pointer and gets the game speed from a parameter in the HTML - file.
  2. start(): starts thread
  3. stop(): stops thread
  4. run(): moves the balls
  5. paint(...): calls paint methods of the both balls, paints score and life information to the screen. If the game is over it writes out an information text about how well the player has been playing (for example: "You are the champion")
  6. update(...): Implements double buffering
  7. mouseDown (...): Listens to mouse events. If the game is running it is watching out for "player hits ball"; if the game is stopped, (start or over), then it listens for a double click and starts the game (again).
The class Player

This class is very simple. It has two instance variables to store, the score and the lives of the player. There are also two methods to add score, (addScore(int plus)), loose lives, (looseLife) and two methods to transmit these values to the Main class (getScore(), getLifes()). As you see, a pretty simple class.

The class Ball

This class is the most complicated one in the game. It implements all of the important methods for a ball object including the following functions:

  1. Ball(...): Constructor, gets all important attributes of a ball object (color, speed, y - direction, ...) and initializes variables in the ball object.
  2. move(): Method moves the ball and tests by calling isOut() if ball is out which means, player loses one life.
  3. ballWasHit(): This method is called if the ball was hit by the player. Afterwards the new direction of the ball is set at random, and the ball is set back to the start position.
  4. userHit (int x, int y): This method tests with the help of vectors, if ball was hit or not (details later).
  5. isOut(): this method tests if the ball is out. The player loses one life if this is true.
  6. DrawBall(Graphics g): This method draws the ball and is called from the paint method of the Main - class.
How the methods work together:

Everytime we call the run - method in our Main class, we call the move - methods of the ball. This method moves the ball and tests if the ball left the applet area by calling isOut(). If the ball has left the applet area, isOut() calls the looseLife() method of the player object and the number of lives is decreased by one.

Everytime the user clicks in the applet area, the mouseDown method in the Main class is called. This method calls userHit(), which is testing if the ball was hit by the user and returns true if this is the case and returns false if the ball was not hit. If true is returned by userHit() ballWasHit() is called. This method adds the score to the player score, and resets the ball... .

One important thing before we start

It is essential, if you want to understand what I'm doing, that you download the sourcecode and read it while I'm explaining things. I won't explain every step in detail, instead you should read the sourcecode, try to understand those things I'm not talking about with help of the other chapters, and learn things by yourself that way.

Even more important might be that you begin to write your own little programs very soon and try to solve problems on your own. Because every game needs its own, very special solutions and mostly there will be no one to help you to find these. Use this little game and all the other applets and sourcecodes on these pages as blueprints and "dictionarys" which help you to implement your own games. You'll learn most if you try it by yourself!


Applet Index
(sorted alphabetically)

A B C D E F G H I J K
L M N O P Q R S T U
V W X Y Z #s
The Java Source
(applets w/source code)

A B C D E F G H I J K
L M N O P Q R S T U
V W X Y Z

How to Add Java Applets to Your Site

New on the Java Boutique:

New Article:
A Barebones Guide to Usability Testing
At first glance, usability testing may seem like unnecessary, or additional work. In todays budget slashed world, it is not uncommon for projects not to have the luxury of this valuable phase. This is a dangerous path to tread. Let's look at how we can incorporate usability testing into our design process.
... [more articles]

New Tutorial:
Java Certification Path API
Certificate based security forms the basis for SSL (Secure socket layer) transactions? Every year billions of transactions involving trillions of dollars are done safely using the SSL protocol. This week we'll look at the role of the Java certification path API and see how it provides a mechanism to implement certification path functionality.
... [more Tutorials]

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

internet.com logo






Developer News

Apache Updates Popular HTTP Server

Is Adobe Targeting Microsoft's InfoPath?

Oracle Takes on Collaboration Market



Database Journal Launched!
Jupiter Media has announced the launch of Database Journal. DBJ offers SQL courses and other database related resources for beginner to expert developers.



Scrolling Text
This is a suite of three applets that displays scrolling text using an array of images. The detailed description of array of each applet is provided.
Download of the Week
DS PageFlipText
This applet displays a page-flip effect between texts. Over the animation you can also insert an image and a scroll-text.

JB User Poll
Which version of Java do you use the most?
The JavaBoutique Top 15:
1. ChompText
2. PulseText
3. DJPopMenu
4. NavBar
5. RushHour
6. PingPong
7. 3DMaze
8. AScroll2
9. Viewer
10. AnimLetters_anim
11. Encyclo
12. Distorter
13. BMI
14. MusicKeyboard
15. AdvPano
Want more? Check out our Top 100!

Refer-It
Affiliate Program and Referral Directory.
New on internet.com
Cisco Exploit Is Active
Security researchers have increased the threat level on a vulnerability affecting Cisco routers and switches after an exploit was posted to a popular mailing list.

The 3G Tokyo Time Machine
What will a 3G wireless future be like? Step into DoCoMo's time machine and find out.

SBA's Whistling Dixie
Small businesses throughout the South are extended the opportunity to meet one-on-one with government agencies and large corporations to obtain near-term contract opportunities for their products and services.


The Enterprise IM Strategies & Solutions Event

Jupitermedia is publisher of the internet.com and EarthWeb.com networks.
Copyright 2003 Jupitermedia Corporation All Rights Reserved.

Legal Notices,  Licensing, Reprints, & Permissions,  Privacy Policy.
http://www.earthweb.com/
http://www.internet.com/