Midp Game Api: Khanh Le
Midp Game Api: Khanh Le
false
Sample Code: A Simple
Game
This example illustrates how the APIs discussed in this chapter can be
used to form a simple game. The user controls a car that is always
moving forward at a constant speed and can be turned 90 degrees
clockwise or counterclockwise. The object of the game is to drive
around the track and collect as many coins as possible without hitting
any of the walls.
The car is implemented using a Sprite that can be rotated in 90-
degree increments using the appropriate transform. Special frame
sequences are used to show just the first frame or to animate through
the explosion frames.
The track and the coins are implemented using TiledLayers. Most of
the cells in the tiled layers are left empty so that the dark gray
background is seen. A static tile is used to form the walls of the track,
and an animated tile is used to display the spinning coins.
Two separate TiledLayers are used to simplify the collision detection
code. Collision detection provides a simple way to determine whether
the car has collided with a non-empty cell in either of the TiledLayers.
If the collision was with the coins, some simple code determines which
cells containing the coins can be removed and the score can be
accurately updated.
Sample Code: A Simple
Game
Note that this code example is
simplified to demonstrate the
use of the game API; a real
application would require
additional code to
appropriately handle
application starting and
pausing, game restarts,
displaying the score, multiple
levels, and so on.
See SimpleGame project
Mobile Game Development Process