|
|||||||||||||||||||||||||||||||||||||||||
| ISBN: 3826617800 ISBN: 3826617800 ISBN: 3826617800 ISBN: 3826617800 | |||||||||||||||||||||||||||||||||||||||||
|
|
Wir empfehlen: | ||||||||||||||||||||||||||||||||||||||||
Our first gameThe game ideaIn our game, the player has to shot 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 10 lifes. If the player lost all of his lifes, 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 score and lifes of the player. There are also two methods to add score (addScore(int plus)), loose lifes (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 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() calles the looseLife() method of the player object and the number of lifes is decreased by one. One important thing before we startIt is essential, if you want to understand what I'm doing, that you download the sourcecode and read in it while I'm explaning things. I won't explain every step in detail, instead you should read the sourcecode, try to understand these things I'm not talking about with help of the other chapters, and learn things by yourself that way. Randomize the movement direction of the two ballsAs I said in the second chapter, we will move our ball in this game not only in the x - direction but in the y - direction, too. To do this we have to add a y_speed vector to the applet as well as the x_speed vector you already know. Our ball will have a x_pos variable which will be changed with every call of move() by adding the value of x_speed to it (remenber: x_speed can be negative) and our ball will also have a y_pos variable which will be changed by adding y_speed. In our game the value of y_speed shall be not changing. The first ball will be moving up (y_speed = -1) and the other ball will be movind down (y_speed = 1). Be careful, in Jave the y - coordinates get bigger when moving down!! The value of x_speed will be chosen at random everytime a ball left the applet. Random number generatorFirst of all we need a random generator in Java. We have to import the class java.util.*; into the class Ball and declare the following instance variable afterwards:
Now we can generate a random integer number by calling rnd.nextInt(). Because we want to have values between -3 and +3 in our game, we have to calculate this random number mod 4 afterwards. The move() - method of the ball objectFirst of all we have to declare four instance variables (x_speed, y_speed, x_pos, y_pos) and initialize them in the constructor of our ball object. Everytime move() is called we add the value of x_speed to the x_pos variable and the value of y_speed to the y_pos variable. Everytime the ball is hit or has left the applet, we initialize x_speed with a random generated value, y_speed will be constant. Here comes the code:
{
pos_x += x_speed; pos_y += y_speed; /* Call isOut() and test if ball is still within the applet area */ isOut(); The method isOut()This method tests if a ball has left the applet. If one of the four if - statements is true, the position of the ball will be reset, a audio file will be played, the player looses one life and x_speed will get a new value (per random). How to hit a ballThis game was the first one I've ever programmed and this problem of how to hit a ball by clicking on it caused me a lot of headache. Now I will try to explain my solution to you: Our ball has a x and a y position. The mouseDown - method in the main class, which calls userHit (int x_mouse, int y_mouse) gives the coordinates, where the user clicked on the applet, to the method userHit(). Now, our method has to decide if a ball has been hit or not. But how can we do this?
double x = maus_x - pos_x; double y = maus_y - pos_y; Now we use Pythagoras (c = Math.sqrt a² + b²) to get the length of this vector, which is the distance between the mouse click and the position of the ball:
double distance = Math.sqrt ((x*x) + (y*y)); In the last step of the method we are testing, if the distance, we calculated in the step before is smaller than a certain value (for example the radius of the ball). I have choosen 15 as a good value although our ball has only the radius 10. But that has no special reason, it just seemed to fit best!
if (distance < 15) {
return true; else return false; Now we are able to hit the ball by clicking on it with the mouse pointer. The player- object: Count score and loose lifesTo count the score and loose lifes, we added the methods looseLife() and addScore (int plus) to our player class. Everytime a ball leaves the applet, isOut() calles player.looseLife(), that way the player looses one life. If the player hits a ball, the method userHit() calls player.addScore (10* Math.abs(x_speed) + 10) and adds the score to the score in the player object (the faster the ball has been, the more points the player gets). The methods addScore and looseLife are really simple. Change the mouse pointer to a crosshair cursorIn this game it would look much better to have a crosshair mouse pointer than a normal mouse pointer. To get such a cursor, we have to add three lines of code to the Main class of our applet: First of all a instance variable of the class cursor:
Cursor c; Now we add the following lines to the init() method:
c = new Cursor (Cursor.CROSSHAIR_CURSOR); // set this cursor as the standard cursor of the applet this.setCursor (c); You can find other mouse pointers in the Java API Start the game with a double click on it and finish it if player lost all his lifes:Now we have almost done it, there is just one thing left to do: We want to start the game when the player clicked two times on the applet (double click) and not earlier. The advantage of this is not only that the player can decide when to start the game but in games where we need the keyboard, we can get the keyboard focus for this applet very easy this way. And of course the game shall be finished if the player lost all of his lifes! As a first step we add a boolean instance variable called isStoped to our Main class. If the value of isStoped is true, our game is not running, if the value is false, the game is running! Now we add a test to the run - method of the main class, which tests if the game is running (isStoped = false) and if the player has more than 0 lifes. If these two things are alright, the two balls are moved.
while (true) {
{
blueball.move(); ... In the next step we'll add a second test to the paint() - method of the applet. As long as the player has lifes left, this method paints the two balls, the score and the lifes of the player. If the game is stoped it paints also the information on the screen, that the game can be started with a double click. If the player lost all of his lifes, the paint method writes out the final score and the information, that the game can be restarted by double clicking on the applet. The paint method looks like this:
public void paint (Graphics g) {
if (player.getLives() >= 0) {
... // If game is stopped if (isStoped) {
// Player has no lifes left else if (player.getLives() < 0) {
// for details see sourcecode! We have almost made it, but...! At the moment we can't switch between the two game states (game stopped, game running) because we have no way to control the value of isStoped. To get this control, we have to add the following code to the mouseDown - method:
public void mouseDown (Event e, int x, int y) {
if (!isStoped) {
if (redball.userHit (x, y)) {
hitnoise.play(); // reset ball redball.ballWasHit (); // Test if blue ball has been hit if (blueball.userHit (x, y)) {
hitnoise.play(); // reset ball blueball.ballWasHit (); else {
shotnoise.play(); // handle mouse events if game is stopped else if (isStoped && e.clickCount == 2) {
isStoped = false; init (); return true; That's it!! Well, you've made it. I hope you understood everything and also these parts, I didn't explain in detail.
The next chapters will tell you something about very special solutions to problems I once had. SourceCode download (*.zip - file) Nächstes KapitelAI in a Pong game |
|
||||||||||||||||||||||||||||||||||||||||
|
Back to the topic sites: StudyPaper.com/Startseite/Computer/Informatik/Programmieren/Java StudyPaper.com/Startseite/Computer/Spiele External Links to this site are permitted without prior consent. | |||||||||||||||||||||||||||||||||||||||||
| Home | deutsch | Set bookmark | Send a friend a link | Copyright © | Impressum | |||||||||||||||||||||||||||||||||||||||||