Does this image seem familiar to you? If it does you are right, it is Sid Meier’s Railroad Tycoon from 1990. Or isn’t it? In fact it is not. Currently I am trying to recode this game in Java.
After reading Andrew Davisons Killer Game Programming I feel well enough equipped to realize such a simple 2D game. I finally worked out a setup that combines the OO principle with classic game programming. In games everything is event driven, this may be user events like clicking somewhere or generated events like a clock tick. This basically allows separation of display and game logic through event messaging. The main problem here is that most events do update the visual representation.
The class diagram shows the essential parts. The base is a JFrame with the game render loop implemented as a Runnable and kicked as its own thread (in startGame()). The calcSizes calculates the actual space that can be used for drawing. The actual drawing is done in an off screen image that is swaped in printScreen(). The different test methods handle user related events. gameUpdate() drives the game related events (like clock tick, random events,…). The gameRender puts the off screen image together by calling its children (views). A view represents the whole rendereable space. There may be several views, but only one is active (enabled) at the time (and thusly rendered). Each view may consist of different parts that represent different controls in the view. Each such part handles its rendering itself and is called from the view.