JavaReference.com AllDevNet.com"
Welcome Guest     Login     Register    
 [ Home > Articles > Java Game Programming, Part II ]  Submit Article   
Java Game Programming, Part II
[ by Vijay Kukreja ]
Print   E-Mail 
About the Author
Vijay is an Information Systems Architect at CVS Pharmacy in Rhode Island. His primary focus is J2EE technology based solution Design and Development. Along with a combination of other enterprise technologies he believes that J2EE will become a primary driving force in the delivery of enterprise level systems. Vijay has participated in numerous consulting projects over the past six years. He has a Masters in Computer Science and a Sun Certified Java Programmer and Architect. His projects involve UML, Java, JSP/Servlets, EJB's, CORBA, XML, JMS, MQSeries & various tools. He has provided onsite J2EE and/or XML development at high-tech companies located in and around Massachusetts, CT and RI. He has also published articles on Java Programming, C++ and other technologies, in various online magazines and with other consultants and is a frequent writer on Java and related technologies. He can be reached at vijay (at) javareference (dot) com
More articles by Vijay Kukreja 
Advertisement

In my previous article, we discussed the details on prerequisites for Game programming in Java with some light on hardware and software details. Also we spoke on a perspective of game programming and how to go about doing it with Java. There was a small piece of code, which I shall explain here and take it further ahead.

Analyzing the code for the Simple Brick game.

The first few lines between /* and */ are comments, which I recommend to all those beginning development on Java. The package structure is defined and the necessary import files are mentioned after the comment.

//our main class for the application
public class  BricksGame extends Frame implements Runnable

This piece of code creates our main class BricksGame, which inherits all the features, provided by the Frame class in the Java API. Also BricksGame will be using Threads, so we make it a point that it implements the Runnable interface from the Java API.

There is a constructor and the main method is the entry point into the application.

Consider :

protected void processWindowEvent(WindowEvent e) 

    super.processWindowEvent(e); 
    if (e.getID() == WindowEvent.WINDOW_CLOSING) 
    { 
      System.exit(0); 
      //close the window and exit out of the program.
    }
}

This method is declared for usage when the user clicks on the close frame button on the top right corner, that event should be handled and because we have added System.exit(0); call it would close the complete application.

Now going ahead from the compilation and execution, once you have seen the frame being displayed we need to get the basic entities ready.

A simple design helps. Lets identify three basic entities we will need. The plot of the game is that there would be a set of bricks stuck to the top of the window. There will be a ball, which bounces within the applications frame. Then there will be a bat which can be moved left or right so the user can place it below the ball and send it back to hit a brick. A simple game strategy to begin with.

So the ball is one entity, the brick is one and the bat is one.

Lets begin by writing the basic class for brick.

/*
* A simple brick game in Java.
* Program Name: Basic entity Brick defined here.
* File Name : BrickEntity
* Author: Vijay Kukreja
* Version : 1.0
*/ 
  
package JavaGames;
  
//Lets keep all our files under one package
//there is nothing to import for creating the brick entity  
//our brick entity class
public class  BrickEntity
{
    //Some attributes of Brick
    private int top=0;
    private int left=0;
    private int height=0;
    private int width=0;
    //some get/set methods for brick
    
    public int getTop()
    {
        return top;
    }
    
    public int getLeft()
    {
        return left;
    }
    
    public int getHeight()
    {
        return height;
    }
    
    public int getWidth()
    {
        return width;
    }
    
    //setter methods
    public void setTop(int t)
    {
        top=t;
    }
    public void setLeft(int l)
    {
        left=l;
    }
    public void setHeight(int h)
    {
        height=h;
    }
    public void setWidth(int w)
    {
        width=w;
    }
  
//end of BrickEntity

I have not used some predefined Java class like Rectangle or Dimension, because I wanted to open up to those beginning programming that these are the contents of the brick. Once you are comfortable with the API and know about existing classes you can use them.

So one could write the BrickEntity class as :

public class BrickEntity extends Rectangle
{
}

This would allow the usage of existing features of a rectangle. But one has to understand the API and what is already available to him. Those who are comfortable I would recommend to them to go ahead and make changes as they wish.

In the next article I’ll continue creating the other entities and put them to use inside our main game class. Till then you can already start on writing the other entities like ball and bat...


Print   E-Mail 
  Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. and its subsidiaries in the U.S. and other countries.