| ★ wanayoo — archive 1999 http://javaboutique.webdeveloper.com/tutorials/Java_Game_Programming/DasErsteSpielEng.html | Nouvelle recherche | Portail wanayoo |
![]() | |||||||||||||||||||||||||||
Tutorials : The Java Game Development Tutorial :
Our first gameThe game ideaIn 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 methodsThe Main classThis 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!
The class PlayerThis 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 BallThis class is the most complicated one in the game. It implements all of the important methods for a ball object including the following functions:
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 startIt 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!
How to Add Java Applets to Your SiteNew on the Java Boutique:
New Article:
New Tutorial: Elsewhere on internet.com:
WebDeveloper Java
WDVL Java
ScriptSearch Java |
| ||||||||||||||||||||||||||
