// File name: snail kemidlet. Java
Package snake;
Import javax. microedition. MIDlet .*;
Import javax. microedition. lcdui .*;
Public class snkemidlet extends MIDlet {
Snakecanvas displayable = new snakecanvas ();
Public snkemidlet (){
Display. getdisplay (this). setcurrent (displayable );
}
Public void Startapp (){}
Public void pauseapp (){}
Public void destroyapp (Boolean unconditional ){}
}
// File name: snakecanvas. Java
Package snake;
Import java. util .*;
Import javax. microedition. lcdui .*;
/**
* Snake games
*/
Public class snkecanvas extends canvas implements runnable {
/** Store the coordinates of the greedy snake node. The two-dimensional subscript 0 represents the X coordinate, and the two-dimensional subscript 1 represents the Y coordinate */
Int [] [] Snake = new int [1, 200] [2];
/** Number of nodes in use */
Int snail kenum;
/** The direction of the snake movement. 0 indicates upward, 1 indicates downward, 2 indicates left, and 3 indicates right */
Int direction;
/* Move direction */
/** Up */
Private Final int direction_up = 0;
/** Down */
Private Final int direction_down = 1;
/** Left */
Private Final int direction_left = 2;
/** Right */
Private Final int direction_right = 3;
/** Game region width */
Int width;
/** Game region height */
Int height;
/** Snake body unit width */
Private Final byte snail kewidth = 4;
/** Whether it is paused. True indicates paused */
Boolean ispaused = false;
/** Whether it is running, true indicates running */
Boolean isrun = true;
/** Interval */
Private Final int sleep_time = 300;
/** X coordinate of food */
Int foodx;
/** Y coordinate of food */
Int foody;
/** Flash control of food */
Boolean B = true;
/** Random object */
Random random = new random ();
Public snkecanvas (){
// Initialization
Init ();
Width = This. getwidth ();
Height = This. getheight ();
// Start the thread
New thread (this). Start ();
}
/**
* Start data Initialization
*/
Private void Init (){
// Number of initialized nodes
Snail kenum = 7;
// Initialize node data
For (INT I = 0; I <snail kenum; I ++ ){
Snake [I] [0] = 100-Snail kewidth * I;
Snake [I] [1] = 40;
}
// Initialize the moving direction
Direction = direction_right;
// Initialize the food coordinates
Foodx = 100;
Foody = 100;
}
Protected void paint (Graphics g ){
// Clear screen
G. setcolor (0 xffffff );
G. fillrect (0, 0, width, height );
G. setcolor (0 );
// Draw a snake body
For (INT I = 0; I <snail kenum; I ++ ){
G. fillrect (snake [I] [0], snake [I] [1], snail kewidth, and snail kewidth );
}
// Draw food
If (B ){
G. fillrect (foodx, foody, snkewidth, and snkewidth );
}
}
Private void move (INT direction ){
// Snake body movement
For (INT I = snkenum-1; I> 0; I --){
Snake [I] [0] = snake [I-1] [0];
Snake [I] [1] = snake [I-1] [1];
}
// Move the first cell
Switch (Direction ){
Case direction_up:
Snake [0] [1] = snake [0] [1]-Snail kewidth;
Break;
Case direction_down:
Snake [0] [1] = snake [0] [1] + snail kewidth;
Break;
Case direction_left:
Snake [0] [0] = snake [0] [0]-Snail kewidth;
Break;
Case direction_right:
Snake [0] [0] = snake [0] [0] + snail kewidth;
Break;
}
}
/**
* Eating food increases by itself
*/
Private void eatfood (){
// Determine whether the snake head overlaps with the food
If (snake [0] [0] = foodx & snake [0] [1] = foody ){
Snail kenum ++;
Generatefood ();
}
}
/**
* Produce food
* Note: The coordinates of food must be in the screen and cannot overlap with the snake body.
*/
Private void generatefood (){
While (true ){
Foodx = math. Abs (random. nextint () % (width-snkewidth + 1 ))
/Snail kewidth * snail kewidth;
Foody = math. Abs (random. nextint () % (height-snkewidth + 1 ))
/Snail kewidth * snail kewidth;
Boolean B = true;
For (INT I = 0; I <snail kenum; I ++ ){
If (foodx = snake [I] [0] & snake [I] [1] = foody ){
B = false;
Break;
}
}
If (B ){
Break;
}
}
}
/**
* Determine whether the game is over
* End condition:
* 1. The snake header exceeds the boundary.
* 2. The snake head hits itself
*/
Private Boolean isgameover (){
// Boundary Identification
If (snake [0] [0] <0 | snake [0] [0]> (width-snkewidth) |
Snake [0] [1] <0 | snake [0] [1]> (height-Snail kewidth )){
Return true;
}
// Touch yourself
For (INT I = 4; I <snail kenum; I ++ ){
If (snake [0] [0] = snake [I] [0]
& Amp; snake [0] [1] = snake [I] [1]) {
Return true;
}
}
Return false;
}
/**
* Event processing
*/
Public void keypressed (INT keycode ){
Int action = This. getgameaction (keycode );
// Change the direction
Switch (Action ){
Case up:
If (direction! = Direction_down ){
Direction = direction_up;
}
Break;
Case down:
If (direction! = Direction_up ){
Direction = direction_down;
}
Break;
Case left:
If (direction! = Direction_right ){
Direction = direction_left;
}
Break;
Case right:
If (direction! = Direction_left ){
Direction = direction_right;
}
Break;
Case fire:
// Pause and continue
Ispaused =! Ispaused;
Break;
}
}
/**
* Thread method
* Precise latency
*/
Public void run (){
Try {
While (isrun ){
// Start time
Long start = system. currenttimemillis ();
If (! Ispaused ){
// Eat food
Eatfood ();
// Move
Move (Direction );
// End the game
If (isgameover ()){
Break;
}
// Control flickering
B =! B;
}
// Redraw
Repaint ();
Long end = system. currenttimemillis ();
// Latency
If (end-start <sleep_time ){
Thread. Sleep (sleep_time-(end-Start ));
}
}
} Catch (exception e ){}
}
}
Advertisement: if the article is helpful to you, please vote for me in the MVB selection. Thank you! Voting address: http://www.csdn.net/community2006/vote/index.rails? Id = 1