2. Improve peripheral tools (images, GameObject, and Font)
Although we have the support of midp2.0, sometimes we still need some auxiliary tools for our convenience. This is perhaps the most interesting before a real game design.
1. First, it is an ImageTools tool class that provides a method to help call the Image
Public class ImageTools {
Protected ImageTools (){
}
Public static Image getImage (String str ){
Image img = null;
Try {
Img = Image. createImage (str );
}
Catch (Exception ex ){
System. out. println (ex );
}
Finally {
Return img;
}
}
}
2. GameObject: provides a common game object.
With the Sprite class, why do we need GameObject? In fact, we generally regard Sprite as an advanced Image. A Sprite is often called by multiple game objects, and GameObject is actually a Sprite state class. GameObject provides a simple concept of lifecycle and animation update speed;
Public class GameObject {
Public Sprite sprite; // built-in Sprite
Public boolean alive; // survival tag
Private int lifecount = 0; // life cycle counter
Public int lifetime = 0; // life cycle, in bytes
Public int speed = 0; // The animation refresh speed. (0 to infinity. 0 indicates that each record is followed by a new screen)
Private int animcount = 0; // The animation counter is updated.
Public GameObject (Image img, int width, int height ){
Sprite = new Sprite (img, width, height );
Reset ();
}
Public void move (int dx, int dy) {// relative movement
Sprite. move (dx, dy );
}
Public void moveto (int x, int y) {// absolutely move
Sprite. setPosition (x, y );
}
Public void update () {// update Status, animation lifecycle update, and lifecycle update
If (! Alive)
Return;
If (++ animcount> speed ){
Animcount = 0;
Sprite. nextFrame ();
If (lifetime! = 0 & ++ lifecount> lifetime)
Alive = false;
}
}
Public void paint (Graphics g) {// Paint
If (! Alive)
Return;
Sprite. paint (g );
}
Public void reset () {// reset
Alive = true;