All source to this presentation is available at:
http://www.gamelizard.com/com/Java2DGameTutorial.zip

 

Java Game Programming

Why Program Games in Java?

- Active online community

- API's available for any purpose:

- Ability to reach people through web browser Applets

    Don't think that's important?  Consider this:  Runescape, an online
        MMORPG, runs in an Applet and has a larger market share than
        the following games:

 

Starting a Game: Tips and Tricks
int width = 200, height = 100;

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    This image, prior to Java 5, will not be accelerated

GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment()
     .getDefaultScreenDevice().getDefaultConfiguration();

BufferedImage image = gc.createCompatibleImage(width, height,
     Transparency.TRANSLUCENT);

 

    Use a timing method to keep the game running smooth, ie GAGETimer

    Example of a run loop:

//these variables will be used to help keep the framerate constant
AdvancedTimer at = new AdvancedTimer();
long tick = 0;
at.start();
long ticksPerFrame = AdvancedTimer.getTicksPerSecond()/game.getFPS();

//these variables will be used to keep an average value of the frames
//per second of this game
long lastTime = System.currentTimeMillis();
int paints = 0;

while (game.isRunning()) {

     /* fps calculation */
     if(System.currentTimeMillis()-lastTime > 1000) {
          lastTime += 1000;

          fpsString = Integer.toString(paints);
          paints = 0;
     }
     /* end fps calculation */

     //Get the Graphics2D context of the screen
     Graphics2D g2d = getScreenGraphics();

     //Draw our game to this Graphics2D context
     game.render(g2d);
     paints++;

     //Draw this associated image to the screen
     showGraphics();

     //Run the logic for the game
     game.run();

     //Wait for a little while so that we can ensure we've occupied
     //the right amount of time for this frame
     at.sleepUntil(tick + ticksPerFrame);

     //Doesn't account for drag
     tick = at.getClockTicks();

     //Does account for drag
     //tick += ticksPerFrame;
}

 

boolean keysPressed = new boolean[256];

public void keyPressed(KeyEvent e) {
     keysPressed[e.getKeyCode()] = true;
}
public void keyReleased(KeyEvent e) {
     keysPressed[e.getKeyCode()] = false;
}

    In your run loop:

private void checkKeys() {
     //horizontal motion
     if (keysPressed[KeyEvent.VK_LEFT])
          player.setDx(-Ship.VELOCITY);
     else if (keysPressed[KeyEvent.VK_RIGHT])
          player.setDx(Ship.VELOCITY);
     else
          player.setDx(0);

     //vertical motion
     if (keysPressed[KeyEvent.VK_UP])
          player.setDy(-Ship.VELOCITY);
     else if (keysPressed[KeyEvent.VK_DOWN])
          player.setDy(Ship.VELOCITY);
     else
          player.setDy(0);

     if (keysPressed[KeyEvent.VK_SPACE])
          if (player.canFire())
               playerShots.add(player.fire());
}

    - Specified in GameFrameLoader and GameAppletLoader

 

Games written in Java:

        Star Dodger

        T4XI

        Robotron 4096

        Ball 4k

        GameLizard

        Puppy Games