Java Fun and Games: It's contest time

Create a tiny game and win an entertaining prize

I recently discovered an interesting game-programming contest with entertaining prizes, which I encourage Java Fun and Games readers to enter.

Java Unlimited is a free service that promotes Java games to players and programmers. Java games are promoted by providing a huge library of free and entertaining Java games, by facilitating game-programming contests, and by giving game authors (regardless of their participation in contests) access to a community that provides valuable feedback for their games.

Java 4K is Java Unlimited's first contest, described by the company as "the ultimate byte-squeezing Java challenge!" The idea: Build an entire game that fits into 4096 bytes. If your game satisfies the size limit and a few other rules, you could win a free copy of Tribal Trouble or six months of free playtime with Wurm Online.

The Java 4K Programming Contest page presents a complete set of rules and a link to a forum where you can ask questions. Because it is challenging to adapt your game to 4096 or fewer bytes, you will want to visit the 4KGamesDesign TWiki for tips on controlling game size.

Read on for a sample game that, one hopes, inspires you to create and submit your own contest entry.

Battle on the high seas

Sea battles are among my favorite computer games. I especially enjoy the challenge of destroying submarines that are also trying to destroy my ships. For this reason, I created a Sea Battle sample game in which a single ship and a single submarine attempt to destroy each other with depth charges and torpedoes. The figure below reveals this game's graphics.

Sea Battle
A ship and a submarine engage in battle

The figure reveals Sea Battle's static background and dynamic game object elements. The background consists of the moon, a black sky, bluish water, and brown undersea hilly terrain. The game object elements include a gray ship, a black submarine, red square torpedoes, and a yellow round depth charge. The only game object element not shown is a circular red explosion.

Although Sea Battle's graphics are primitive, they have been improved through antialiasing and transparency. I chose to anti-alias the graphic shapes of the moon, depth charge, and submarine to give them a rounded appearance. I also chose to paint the bottom two-thirds of this applet 75% blue and 25% dark gray so that underwater game objects (including the bottom of the ship) have a murky look.

Sea Battle is easy to play. When you start this applet, its title screen tells you to click a mouse button to play the game. During game play, press the left/right arrow keys to move the ship left or right; press the spacebar to fire a depth charge. When the depth charge hits the submarine or a torpedo hits the ship, you see an explosion and the game reverts to the title screen, which presents a win/lose message.

Architecture highlights

Sea Battle is described by a single source file— and its SeaBattle.html companion HTML file are located in this article's code archive. This heavily-commented source file (which is more than 900 lines long) organizes the applet's code into these seven classes:

  • SeaBattle is the main applet class. In addition to an assortment of field variables, this class provides methods for initializing the applet, starting/stopping the game loop, performing the game loop, rendering animation frames, and obtaining randomly selected integers.
  • DepthCharge describes the game object responsible for blowing up the submarine. This class maintains this game object's location, a bound (how far the depth charge can drop before disappearing), and its rectangular bounding box (for collision detection).
  • Explosion describes an expanding circular explosion. This class maintains the explosion's location and current circular width, and a flag that differentiates between a ship or submarine explosion (the submarine keeps moving but fires no torpedoes when the ship is hit).
  • Queue describes a queue datastructure for storing java.awt.event.KeyEvent objects. Objects that describe pressed left, right, and spacebar keys are stored (in first-in-first-out order) in the game's single queue object.
  • Ship describes the game's protagonist. Along with the ship's left and right boundaries, position, and bounding box, the class maintains the ship's velocity. When the ship hits a boundary, its velocity changes.
  • Sub describes the game's antagonist. It maintains the same information as the ship, but is more flexible. For example, the submarine can move to the left and right of the applet window's left and right edges. Also, the submarine's initial velocity is randomly chosen.
  • Torpedo describes the game object responsible for blowing up the ship. As with DepthCharge, this class maintains a torpedo's location, a bound (how far the torpedo can rise before disappearing), and its rectangular collision-detection bounding box.

The SeaBattle class includes 20 constant and non-constant private field variables that control the game. These variables influence the animation speed, record mouse button clicks and key-presses, buffer the next animation frame, identify the game thread and various game objects, and more.

The complete list of variables appears below:

1 2 Page 1
Page 1 of 2