Don's Dugout: Technical stuff

A single thread, executing at regular intervals, controls everything that happens after initialization.

I use several off-screen graphics buffers, but only two of them are conceptually important. One holds the background, including all currently stationary objects. The other acts as intermediate storage and gets the background and the moving characters drawn onto it, and is then displayed.

During each animation cycle (roughly every 0.1 seconds with the default setting), basically the following things get done, and in this order:

  1. The logical positions and states of the player and the critters are updated. Tunnel segments are carved out of the first graphics buffer. If a money bag is picked up, here is where it gets erased.
  2. The first graphics buffer, still with only stationary objects on it, gets copied to the second one.
  3. The score is updated if necessary. (This takes three drawImage operations and I consequently spread it out over three game cycles to balance the workload of the machine and make the game run more smoothly.)
  4. All moving characters are drawn, in their corresponding positions, on the second graphics buffer.
  5. If the player stands just below a rock, it gets removed from the first graphics buffer and a "live" rock object gets activated in its place. This saves one drawImage operation, because I don't have to draw a new "live" rock on the second buffer after deleting the old "dead" one from the first buffer. The "live" rock is supposed to be in that same position for a moment anyway.
  6. The thread calls the update routine, which draws the second graphics buffer on the screen.

Some might wonder why the objects and tunnels look so randomly thrown together. That's because they are randomly thrown together. The reason they appear in identical positions each time you run the game is that I use a home-made deterministic pseudo-random number generator along with the standard one. But the clouds, the ground layers and the critter movements are based on "real" random numbers, so they will be different.