Start the game with a double click on it and finish it if the player loses all his lives:

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

Now we have almost done it, there is just one thing left to do: We want to start the game when the player clicks 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 easily this way. And of course the game shall be finished if the player loses all of his lives!

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 lives. If these two things are alright, the two balls are moved.

  •  
    •  
        redball.move();
        blueball.move();
    • if (player.getLifes() >= 0 && !isStoped)
      { }

      ...
  • // run - method
    while (true)
    {}

 

In the next step we'll add a second test to the paint() - method of the applet. As long as the player has lives left, this method paints the two balls, the score and the lives 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 loses all of his lives, 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:

    // paint() - method
    public void paint (Graphics g)
    {
    •  
      •  
          // paint information: "Start game with a double click"
      • // Paint the two balls, score ...

        ...

        // If game is stopped
        if (isStoped)
        {}
        // paint final score, set isStopped true...
        // for details see sourcecode!
    • // player has lives left
      if (player.getLifes() >= 0)
      {}
      // Player has no lives left
      else if (player.getLifes() < 0)
      {}
    }

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:

    // listen to mouse clicks
    public void mouseDown (Event e, int x, int y)
    {
    •  
      •  
          // play audio file
          hitnoise.play();

          // reset ball
          redball.ballWasHit ();
          // play audio file
          hitnoise.play();

          // reset ball
          blueball.ballWasHit ();
          // play normal shot audio file
          shotnoise.play();
      • // Test if red ball has been hit
        if (redball.userHit (x, y))
        {}
        // Test if blue ball has been hit
        if (blueball.userHit (x, y))
        {}
        else
        {}
        // reset all important values!
        isStoped = false;
        init ();
    • // handle mouse events when game is running
      if (!isStoped)
      {}
      // handle mouse events if game is stopped
      else if (isStoped && e.clickCount == 2)
      {}

      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.
Now feel free to program any game you want to program, use the power of Java, which makes almost everything possible, good luck in developing your own games and I hope, that I could help you a little bit with this tutorial! If you have problems, tutorials, games ... please send me a mail!

Now you can take a look at the game, download the sourcecode and have fun!

SourceCode download (*.zip - file)
Take a look at the applet

Nächstes Kapitel

AI in a Pong game

 

 1 
About | Sitemap | Contact