JavaReference.com AllDevNet.com"
Welcome Guest     Login     Register    
 [ Home > Articles > Java Game Programming, Part I ]  Submit Article   
Java Game Programming, Part I
[ 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

Introduction

A good part of software sold on the Internet is computer games. Computer games have always been on the move. With the advent of more powerful hardware and supporting devices, games have really taken on the world. Playing games is addictive, but writing games is both addictive and fun. Just like a thriller novel or movie, games can be full of action, raising your adrenalin levels.

Prerequisites

Even though this is not rocket science, one does have to have a preliminary understanding of the Java language in order to use this article effectively. Since we assume this, we also assume that the developer using the code in this article is able to compile and execute the programs given here without help. Therefore, no instructions are provided for these steps.

Hardware and software required

  • Pretty much any processor and OS capable of running Java. Note that games will run slower on slow machines, so the faster your processor, the better. Normally games do eat up significant CPU time.

  • The Java 2 SDK for your OS, preferably >= 1.3.0.

Since our platform of choice is Java, our games are capable of running on multiple platforms like Windows, UNIX flavors, Apple Macintosh etc. 

Games aren't as difficult to play as they are to program. The game player rarely notices the work involved in creating a simple game like Tetris. Im sure you will discover that not only is game programming fun, its also a great way to learn a few things.


The game

Lets build a simple game of bricks in Java and discuss other issues. In this article, we will make the first window for the game. Here goes.


/* A simple brick game in Java.
Program Name: Bricks a simple a Java Game.
File Name : BricksGame
Author: Vijay Kukreja
Date: 15-June-2001
Version : 1.0
*/ 

//Lets keep all our files under one package
package JavaGames; 

//lets import the necessary classes to which will create the GUI
import java.awt.*; 
import java.awt.event.*; 
import java.awt.Color; 

//our main class for the application
public class  BricksGame extends Frame implements Runnable
{
    //Some variable declarations
    int iFramex=0; 
    int iFramey=0; 
    int iFrameh=500; 
    int iFramew=800; 
    
    //the main method the entry point into our game program.
    public static void main(String[] args)  
    { 
        System.out.println("Welcome to the Bricks Game!"); 
        BricksGame m_bricksGame=new BricksGame(); 
    } 

    //the constructor where we’ll initialize our environment and create the frame
    public BricksGame() 
    {
        this.setBounds(m_iFramex,m,_iFramey,m_iFramew,m_iFrameh); 
        this.setTitle("Welcome to Bricks Game"); 
        this.setVisible(true); 
    } 
    
    //In case someone wants to close the program from the Frame we need to
    //process the window closing event and handle it appropriately
    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.
        } 
    } 
}



Save this file and name it “BricksGame.java”.


Lets compile this file now.

Prompt> javac –d . BricksGame.java


Execute our little program now.


Prompt> java -classpath .;.JavaGames JavaGames.BricksGame


You should see something like this.


Game Window


In the next article, we’ll add in the basic element, the Bricks, then a ball which bounces around the screen and finally a bat, which can be used to redirect the ball at the bricks and score points.


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.