Java Lesson 9: Finish Up the Game


Lesson 9 Source Files
Resource Files

We now have constants. Java doesn’t have global constants like languages like C. Instead you have to define a static class with static members. You can then reference those members without having to instantiate the static class. This allows us to use constants to define state values rather than having to remember that the title screen is state 1.

Since we now have an easy way to define states some files have been renamed to indicate they define a state. wolf5k.java is now state_wolf5k.java, etc. You’ll also find that there is no longer a thread running the game. Instead we’re using Java’s messaging system and the main loop is no longer a clearly defined loop. In the paint and update methods which were previously unused we call the run() method which does one game loop iteration as well as halts the main applet thread for a millisecond to let the CPU breathe. Once that method completes, the repaint() applet method is called which puts an event into Java which will trigger the paint or update method to be called. And that’s how our game loop is called until the applet exits. Of all the ways to get a language to loop, that is by far the most convoluted. But, with Java it actually gains us a few FPS and things just work more smoothly.

I say paint or update because that’s just how Java works. So, both those functions need to call the main method to render our game. There’s no way to determine which will be called on a repaint() event.

Now that we have a font that we can print to the screen we can finally display a timer. In doing so I discovered that the timer was running way too fast. Turns out I was calculating the time it took to render a frame the wrong way which sped up time. That is now corrected so 1 second of game time is 1 second of real time. Previously, one second of real time was several seconds of game time. We also now have a bunny counter so we can see how many of them are bouncing around. You’ve been able to shoot the bunnies. Now you can see how many you’ve killed.

There are four states currently. There’s the title screen, the game, the win and the lose. If you get the bunny counter down to 0 you win. If the bunny counter gets up to 100 you lose. How does the bunny counter go up you may ask. Bunnies multiply after 20 plus a random amount of seconds. After a bunny multiplies it will take longer and longer to multiply again until it reaches a certain age where it can’t multiply any longer. If you don’t shoot any bunnies the game will be over in a couple minutes which is pretty reasonable. Each bunny spawns one additional bunny at a time.

This tutorial would have been up sooner except for one minor detail: it was an applet that didn’t run in a browser. The reason it would not run in a browser was because it was loading resources using FILEs rather than URLs. That’s been fixed so now you can use the APPLET tag in an HTML document to load up Bunnies.

And so now you have a complete game in Java. In Lesson 10 my goal is to improve the rendering so things look better. Right now we have no lighting at all. We’re just plotting pixels with no consideration for any light source.

The tutorials end at Lesson 10 but I have more coming.

  1. No comments yet.
(will not be published)
  1. No trackbacks yet.