Java Fun and Games: Java visits the arcade

Develop arcade games with JGame

In the 1980s, people flocked to arcades to play Pacman, Space Invaders, Frogger, Donkey Kong, and other games. These classics are still fun to play: If you've never played one of these arcade games, or if you just want to take a trip down memory lane, check out Classic '80s Games in Resources.

Do you ever dream of developing a Java-based arcade game that is similar to the classics? If so, you can turn this dream into reality with JGame, a Java game engine created by Boris van Schooten. This article introduces you to JGame, its features, directories and files, and example games. We also explore JGame's architecture—its engine, game objects, and timers.

Note: You can now build and run the applets presented in Java Fun and Games using DevSquare, an online development tool. Read the user guide available in Resources to get started.

Introducing JGame

JGame is a small 2D Java game engine whose high-level framework—based on sprites with automatic animation and collision detection, and a tile-based background with easy sprite-tile interaction facilities—simplifies classic-style arcade game development. Games run as applications or applets, scaling to any window size—they even run full-screen. (To learn more about sprites and tiles, visit Wikipedia.)

This article focuses on JGame version 1.2. Although this version has a few deficiencies (lack of sound support and the inability to create games with scrolling backgrounds are two examples—these problems will most likely be addressed in a future version), version 1.2 offers several features that facilitate arcade game development and deployment:

  • Built-in animated "sprite" engine with easy animation definition
  • Tile-based background handling with decorative backdrop
  • Automatic collision detection with sprites and background tiles, and easy background tile interaction
  • Ability to load sprites, tiles, and color fonts directly from sprite sheets
  • Images and animations defined in a text file
  • A state machine model for in-game sequences
  • A standard game state machine and some standard game objects
  • Debugging facilities, which include visualizing bounding boxes and printing debug messages next to an object on the play field
  • Optimized and accelerated graphics for various displays, without requiring extra packages; works well on remote X11 displays
  • Ability to program game at one window size; game can be scaled to any desired window size (even full-screen) when run
  • Tested on various platforms; can be easily run as an applet or as an application (and from a jar file)

Engine installation

You must install JGame before you can work with this software. Surf on over to JGame's Webpage (see Resources for a link) and select a distribution file—jgame-20061023.tar.gz or, for version 1.2. After downloading and unarchiving the distribution file, I recommend moving the jgame home directory to your root directory (for convenience).

JGame comes with precompiled Java 1.4 classfiles. If you plan to use JGame with Java 1.4 or higher, there is nothing left to do—JGame is installed. However, if you plan to use JGame with Java 1.2 and/or 1.3, you must recompile JGame according to the instructions found in a file named makefile—one of the various directories and files located in the jgame home directory, a list of which follows below:

  • examples contains source files and precompiled Java 1.4 classfiles for this package
  • gfx contains GIF, PCX, and PNG image files for the example games bundled with JGame
  • html contains HTML and related files for running the example games as applets
  • javadoc contains the Java documentation for JGame's two packages of classes
  • jgame contains source files and precompiled Java 1.4 classfiles for this package
  • CHANGES logs various changes (including bug fixes) applied to each JGame version
  • LICENSE identifies the JGame license, copyright, and warranty information
  • make.bat compiles source files in directories examples and jgame
  • make-docs.bat creates the packages documentation located in directory javadoc
  • make-jar.bat creates a jar file with all relevant JGame classfiles and resource files
  • Makefile shows how to compile source code, create a Java 1.2 version of JGame, and more
  • presents a Unix shell script for packaging JGame into a distribution file
  • manifest identifies the main class for the jar file
  • MANUAL provides insight into game programming with JGame
  • README provides a starting point for exploring JGame

Example games

JGame includes 11 example games that demonstrate JGame-based game development: NebulaAlpha, Insecticide, ChainReaction, SpaceRun, SpaceRun II, Munchies, WaterWorld, CavernsOfFire, MatrixMiner, PubMan, and DungeonsOfHack. Because these games exist in the examples package, you must include the examples. prefix when running the game as an application or as an applet.

You can run any example game as an application by making sure that jgame is the current directory, or by adding jgame's path to the CLASSPATH environment variable. For example, to run PubMan (a Pacman clone—you are chased by mugs of beer instead of ghosts) as an application, specify java examples.PubMan. By default, this game runs full-screen.

Instead of running full-screen, you can force most example games to display their output in a window, by specifying integer arguments on the command line. For example, to run PubMan in a 300-horizontal-by-300-vertical-pixel window, invoke java examples.PubMan 300 300. Figure 1 shows the resulting window.

Figure 1. PubMan's output has been scaled to 300 horizontal by 300 vertical pixels. The green region on the right is a scaling artifact.

You can also run an example game as an applet. The html directory contains several HTML files for each example; each HTML file runs the example at a specific window size (including full-screen). For example, this directory's applet-pubman-320x240.html file employs the following <applet> tag to run PubMan at a window size of 320 horizontal by 240 vertical pixels:

<applet code="examples.PubMan" width="320" height="240" codebase="..">
PubMan applet
1 2 Page 1
Page 1 of 2