Our first game

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

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!