Java Reference
In-Depth Information
However, there's always some point in the user interface code that triggers behavior in the logic
code. These points are usually where the code handles input from the user. If you click a button on a
mouse, the input does go through the user interface layer (via the mouse handler), but that click almost
certainly triggers some kind of game logic as well. When you read the code for video games (you can find
code for lots of games on the web, by the way), watch for those entry points to the game logic. They are
key spots in any computer game.
The Game Logic
“Game logic” refers to the way the game responds to the user's input and possibly to other conditions
(such as timers or network events). Here are some examples of game logic that you may have seen in
various games:
The player scores points after hitting a target.
The game is over when the timer runs down.
Jumping on a particular spot opens a secret door.
All games have at least a little game logic. The game (usually) needs some kind of ending condition
(the game-over condition), and most games have some way of keeping score. Not all games need these
things, but most do. For instance, the second example (a very simple shooting gallery game) in this
chapter has no ending condition. You can keep shooting things as long as you like.
The Game Loop
As you may recall from Chapter 10, “Animation,” the number of times a program redraws the screen is
its frame rate, and the number of times a program processes all of its inputs and produces all of its
outputs is its hertz. The game loop dictates both of those measures of speed. The second sample in this
chapter (the shooting gallery game) runs through its game loop 50 times a second, so both its frame rate
and its hertz are 50.
So what does a game loop do? That can vary from game to game, but there are some basics that any
game loop has to do:
Process the user input and all other inputs
Process the game logic
Redraw the playing field
The TargetClick Game
TargetClick is very much a video game version of the mechanical classic, Whac-a-Mole. A spot shows up,
and you have to click it before it disappears. The more spots you click, the bigger your score. You can
(indirectly) control the length of the game by setting the number of successful clicks needed to win. If
you want a more difficult game, expand the game's window (making the spots farther apart). Figure 12-1
shows the TargetClick game in progress.
Search WWH ::




Custom Search