Breaking News

Game Deveopment Tutorial with Java:part 3


                One of the important things in the game is the interactive sounds. Really it is so important thing which need a creative talent. I’m going now to speak about how to play sounds and with java? And what are the supported files types?
                From my experience with audio in java that there are several types which are wav (PCM) and au with this properties 8 Bit, 8000 khz, Mono files. And the MP3 requires extra packages of the JLayer you can find more details of the JLayer project herehttp://www.javazoom.net/javalayer/javalayer.html . for simplicity I will use wav and au. We have two ways to play sounds in java either in the applet or in the Desktop game (Frame).
                In case of the frame we will use the class java.audio.AudioPlayer with requires java.audio.AudioInputstream for example.
try{
      InputStream in = new FileInputStream("sound.wav");
      AudioStream as = new AudioStream(in);        
      AudioPlayer.player.start(as);           
}catch (Exception e) { }
                In case of the applet we will deal with interface called AudioClip and the Applet function getAudioClip(base,soundFileName) for example
AudioClip sound = getAudioClip (getCodeBase(), "sound.au");

                I wont write the example and this is good chance to implement this example simply you will use the previous example and you will play sound when the ball touches the edge and another one when the ball touches the ball.

Handle mouse events

                It is so important to make your game interactive with the player; this won’t happen even if there is a way to get input from the player. There are several ways to the player to play the game the most famous are Keyboard, mouse, joystick … .in this tutorial I’ll care about the mouse and the keyboard.
                 Okay, I’ll start with the mouse listeners but I’m not going to explain the Listeners and how are they working? Rather than using them in the game. If you are interested in knowing more about it:
Note that the difference between the MouseListener and the MouseAdapter is that the MouseAdapter implements the Interface MouseListener so you can override the method you want to add some behavior to it.(by the way this is simple implementation of the Adapter Design pattern)
                In the either the mouse listeners or mouse adapters you will find those functions.
mouseClicked
void mouseClicked(MouseEvent e)
Invoked when the mouse button has been clicked (pressed and released) on a component.
mousePressed
void mousePressed(MouseEvent e)
Invoked when a mouse button has been pressed on a component.
mouseReleased
void mouseReleased(MouseEvent e)
Invoked when a mouse button has been released on a component.
mouseEntered
void mouseEntered(MouseEvent e)
Invoked when the mouse enters a component.
mouseExited
void mouseExited(MouseEvent e)
Invoked when the mouse exits a component.

                Okay now lets apply this in our game, we simply will remove the ball which clicked with the mouse. so we will write our code in the mouseClicked.

public void mouseClicke(MouseEvent e) {
                                for (int i = 0; i < balls.size(); i++) {
                                                Ball ball = (Ball) balls.get(i);
                                                // check if the x position of the mouse inside the ball
                                                boolean withinX = e.getX() >= ball.getX() - ball.getRadius()
                                                                                && e.getX() <= ball.getX() + ball.getRadius();
                                                // check if the y position of the mouse inside the ball
                                                boolean withinY = e.getY() >= ball.getY() - ball.getRadius()
                                                                                && e.getY() <= ball.getY() + ball.getRadius();
                                                // check if the mouse postion inside the ball
                                                if (withinX && withinY) {
                                                                balls.remove(i--);
                                                }
                                }
                }

                Do not forget to add the listener to the applet
public void init() {
            setSize(appWidthappHeight);
            addMouseListener(this);
      }

Example 6

Handle key events

               
                The concept as it is in the mouse, but we here will deal with either KeyListener or KeyAdapter.
keyTyped
void keyTyped(KeyEvent e)
keyPressed
void keyPressed(KeyEvent e)
keyReleased
void keyReleased(KeyEvent e)
 
Okay now lets apply this in our game, we want to make the up key speed up all the balls, the down key will slow them, and enter key will change the directions of the balls. So we will implement the KeyListner
public void keyPressed(KeyEvent e) {
                                int speed = 0;
                                boolean changeDirection = false;
                                if(e.getKeyCode() == KeyEvent.VK_UP){
                                                speed++;
                                }else if(e.getKeyCode() == KeyEvent.VK_DOWN){
                                                speed--;
                                }else if(e.getKeyCode() == KeyEvent.VK_ENTER){
                                                changeDirection = true;
                                }
                                for (int i = 0; i < balls.size(); i++) {
                                                Ball ball = (Ball) balls.get(i);
                                                ball.setSpeed(ball.getSpeed()+speed);
                                                if(changeDirection) ball.goOtherDirection(0);
                                }                             
                }

And we will add the KeyListener to the Applet
public void init() {
            setSize(appWidthappHeight);
            addMouseListener(this);
            addKeyListener(this);
      }

You will find the code in Example 7

Animation Time line


                Okay in some games you see some fire, explosion … which like a movie repeat itself for numerous times. I have search more and more about something like that with java to add some sort of effects or something like that but I found that I waste my time. So see my simple idea for example to do an explosion. I used Gimp to draw this simple sequence of fire. And from my knowledge about videos that they are consist of frames which in our case will be an image.
               
                Simply I’ll implement the idea in a class called  Explosion this class will hold the seven frames (images) of the explosion –at this point no new thing-. What I’m going to do is that explosion should start when we click on the ball. So we need to know where the explosion will be displayed. So we will add to the Explosion class the point of the display. Another thing is the explosion area which will be determined from the ball radius. We have another situation which is that we could have more than one explosion. Okay lets see how does like the Explosion class.
package game;

import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;

public class Explosion {
      int width = 10;
      Point p = new Point(0, 0);
      int couter = 0;
      static Image[] frames = new Image[7];
      {
            Toolkit kit = Toolkit.getDefaultToolkit();
            for (int i = 0; i < frames.length; i++) {
                  frames[i] = kit.getImage(getClass().getResource(
                              "/game/e" + (i + 1) + ".png"));
            }
      }

      public static Image getFrame(int i) {
            return frames[i];
      }

      public Point getP() {
            return p;
      }

      public void setP(Point p) {
            this.p = p;
      }

      public int getCouter() {
            return couter;
      }

      public void setCouter(int couter) {
            this.couter = couter;
      }

      public int getWidth() {
            return width;
      }

      public void setWidth(int width) {
            this.width = width;
      }

}

                As you noticed that the Explosion has a counter which indicates what is the frame to be displayed? On the other hand we need a list of explosions to be drawn on then Animation class. Until now we have not drawn the frames so we will draw them in the paint of the Animation class.
I’ll add those lines to the Animation class
private ArrayList explosions = new ArrayList();
private AudioClip explosionSound = null;             
               
                After that I’ll load the sound file in the init method of the Animation class
explosionSound = getAudioClip(getClass().getResource("/game/explode.au"));
               
And we will modify the mouse press event to add new Explosion and play the sound file. Note that the display point will be the position of the ball minus the double radius. And also the explosion are will be about double the area of the ball.
public void mousePressed(MouseEvent e) {
                                for (int i = 0; i < balls.size(); i++) {
                                                Ball ball = (Ball) balls.get(i);
                                                // check if the x position of the mouse inside the ball
                                                boolean withinX = e.getX() >= ball.getX() - ball.getRadius()
                                                                                && e.getX() <= ball.getX() + ball.getRadius();
                                                // check if the y position of the mouse inside the ball
                                                boolean withinY = e.getY() >= ball.getY() - ball.getRadius()
                                                                                && e.getY() <= ball.getY() + ball.getRadius();
                                                // check if the mouse postion inside the ball
                                                if (withinX && withinY) {
                                                                explosionSound.play();
                                                                Explosion exp= new Explosion();
                                                                exp.setP(new Point(ball.getX()-2*ball.getRadius(),ball.getY()-2*ball.getRadius()));
                                                                exp.setWidth(ball.getRadius()*4);
                                                                explosions.add(exp);
                                                                balls.remove(i--);
                                                }
                                }

                }

                Now we will draw the explosions of the game by getting each frame of each explosion and go to the next frame until we reach the end after that we remove this explosion from the explosion list.
public void paint(Graphics g) {
                                for (int i = 0; i < balls.size(); i++) {
                                                Ball ball = (Ball) balls.get(i);
                                                ball.paint(g);
                                }
                                for (int i = 0; i < explosions.size(); i++) {
                                                Explosion exp = (Explosion) explosions.get(i);
                                                Image frame = Explosion.getFrame(exp.getCouter());
                                                g.drawImage(frame, exp.getP().x, exp.getP().y, exp.getWidth(),
                                                                                exp.getWidth(), this);
                                                exp.setCouter(exp.getCouter()+1);
                                                if(exp.getCouter()>=7){
                                                                explosions.remove(i--);                                                               
                                                }
                                               
                                }
                }

                Here are three screen shots of three different balls.
Figure 5
Figure 6
Figure 7
You will find the code in Example 8


First Game


WOW you are ready now to do your games .here you will find good and simple example of a game idea and simple implementation.

References


No comments