Game Development Reference
In-Depth Information
Listing 6-8. Stain.java
package
com.badlogic.androidgames.mrnom;
public class
Stain {
public static final int
TYPE_1
= 0;
public static final int
TYPE_2
= 1;
public static final int
TYPE_3
= 2;
public int
x, y;
public int
type;
public
Stain(
int
x,
int
y,
int
type) {
this
.x = x;
this
.y = y;
this
.type = type;
}
}
The
Stain
class defines three public static constants that encode the type of a stain. Each
Stain
instance has three members, x and y coordinates in Mr. Nom's world, and a type, which is one
of the constants that were defined previously. To make our code simple, we don't include getters
and setters, as is common practice. We finish the class off with a nice constructor that allows us
to instantiate a
Stain
instance easily.
One thing to notice is the lack of any connection to graphics, sound, or other classes. The
Stain
class stands on its own, proudly encoding the attributes of a stain in Mr. Nom's world.
The Snake and SnakePart Classes
Mr. Nom is like a moving chain, composed of interconnected parts that will move along when we
pick one part and drag it somewhere. Each part occupies a single cell in Mr. Nom's world, much
like a stain. In our model, we do not distinguish between the head and tail parts, so we can have
a single class that represents both types of parts of Mr. Nom. Listing 6-9 shows the
SnakePart
class, which is used to define both parts of Mr. Nom.
Listing 6-9. SnakePart.java
package
com.badlogic.androidgames.mrnom;
public class
SnakePart {
public int
x, y;
public
SnakePart(
int
x,
int
y) {
this
.x = x;
this
.y = y;
}
}