Game Development Reference
In-Depth Information
Creating the Game Layout
We must create the user-defined layout using XML and the
Asteroids
class. Rename the
main.xml
file
created by the wizard as
asteroids.xml
and insert the code in Listing 4-4.
Listing 4-4.
The XML Layout of Asteroids
<?xml version="1.0" encoding="utf-8"?>
<ch04.game.Asteroids
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_asteroids"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF000000">
</ch04.game.Asteroids>
Note that
ch04.game.Asteroids
is a required Java class that extends
ArcadeGame
, which in turn
extends
LinearLayout
. A detailed list of files and resources is shown in the next section.
Looking at the Resources
Besides code, we need resources such as sounds, XML string messages, and a game icon (see Figure 4-4).
Here is a list of resources used by Asteroids and what they do:
Java classes
•
AsteroidsActivity
: This is the master class called by Android when the user
starts the game. It contains life cycle events such as
onCreate
,
onResume
, and
onStop
.
•
•
Asteroids
: This class contains all game code. It extends
ArcadeGame
.
ArcadeGame
: This is an abstract class with the game loop and game life cycle
events
initialize
,
updatePhysics
,
getScore
,
halt
, and
resume
.
•
•
Constants
: This class contains many game constants such as game delay,
frames per second, number of ships, and sprite speeds.
Polygon classes
•
•
PolygonSprite
: This class represents any game sprite. It has methods to
detect collisions using the classes
Polygon
and
Rectangle
.
Polygon
: This class represents an N-sided polygon. It code is taken from the
Java SE
java.awt.Polygon
class with most of the code stripped for simplicity.
•
Rectangle
: This is also a stripped version of the Java SE
java.awt.Rectangle
class. It describes a rectangle by X and Y coordinates, as well as width and
height. It can also check if the rectangle contains or is inside another.
•