Use MIDP2.0 to develop games (2) use Sprite

Source: Internet
Author: User

Sprite, Genie, as the name suggests, is dedicated to represent the animated roles in the game, such as airplanes and tanks. In MIDP1.0, We have to write a dedicated class to implement Sprite. Fortunately, MIDP2.0 provides strong support for Sprite to create static, dynamic, non-transparent and transparent Sprite, next we are going to add a Sprite based on the previous GameCanvas and let it work.

Sprite's main constructor methods include:

Sprite (Image): Construct a Sprite with a single pattern;

Sprite (Image, int width, int height): Construct an animated Sprite. The image is divided into N frames according to the specified size. The Sprite can be moved through setFrame (int index. We used a png image with a transparent background to create the Sprite of the tank:

(Note that this image is in JPG format. You need to use software such as Photoshop to process it in png format with a transparent background. The size is 64x16)

Create the following project and directory in Eclipse:

Below is the SpriteTankGameCanvas. java:

Package tank. midp. core;
Import javax. microedition. lcdui .*;
Import javax. microedition. lcdui. game .*;

Public classTankgamecanvasExtends GameCanvas implements Runnable {
// Control direction:
Private static int INDEX_OF_UP = 0;
Private static int INDEX_OF_DOWN = 1;
Private static int INDEX_OF_LEFT = 3;
Private static int INDEX_OF_RIGHT = 2;

Private boolean isPlay; // Game Loop runs when isPlay is true
Private long delay; // To give thread consistency
Private int currentX, currentY; // To hold current position of the 'X'
Private int width; // To hold screen width
Private int height; // To hold screen height

Private Sprite spriteTank; // our sprite!

// Constructor and initialization
Public TankGameCanvas (){
Super (true );
Width = getWidth ();
Height = getHeight ();
CurrentX = width/2;
CurrentY = height/2;
Delay = 20;
// Init sprite:
Try {
Image image = Image. createImage ("/res/img/player1.png"); // note the path
SpriteTank = new Sprite (image, 16, 16); // The size is 16x16
}
Catch (exception e) {e. printstacktrace ();}
}

// Automatically start thread for game loop
Public void start (){
Isplay = true;
New thread (this). Start ();
}

Public void stop () {isplay = false ;}

// Main game loop
Public void run (){
Graphics G = getgraphics ();
While (isplay ){
Input ();
Drawscreen (g );
Try {
Thread. Sleep (Delay );
}
Catch (interruptedexception IE ){}
}
}

// Method to handle user inputs
Private void input (){
Int keystates = getkeystates ();
// Left
If (keystates & left_pressed )! = 0 ){
CurrentX = Math. max (0, currentX-1 );
SpriteTank. setFrame (INDEX_OF_LEFT );
}
// Right
If (keyStates & RIGHT_PRESSED )! = 0 ){
If (currentX + 5 <width)
CurrentX = Math. min (width, currentX + 1 );
SpriteTank. setFrame (INDEX_OF_RIGHT );
}
// Up
If (keyStates & UP_PRESSED )! = 0 ){
CurrentY = Math. max (0, currentY-1 );
SpriteTank. setFrame (INDEX_OF_UP );
}
// Down
If (keyStates & DOWN_PRESSED )! = 0 ){
If (currentY + 10 CurrentY = Math. min (height, currentY + 1 );
SpriteTank. setFrame (INDEX_OF_DOWN );
}
}
// Method to display graphics
Private void drawscreen (Graphics g ){
G. setcolor (0); // black
G. fillrect (0, 0, getwidth (), getheight ());

// It is very easy to draw a Sprite:
Spritetank. setposition (currentx, currenty );
Spritetank. Paint (g );

Flushgraphics ();
}
}

The running figure is as follows:

Notes: The class of MIDP can be run only after preverify. Therefore, the executable class is in the verified directory instead of the bin directory. Before running, copy the entire res directory to verified/classes.

:~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.