FileName: Snakemidlet. java
Package snake;
Import javax.microedition.midlet.*;
Import javax.microedition.lcdui.*;
public class Snakemidlet extends MIDlet {
Snakecanvas displayable = new Snakecanvas ();
Public Snakemidlet () {
Display.getdisplay (This). Setcurrent (displayable);
}
public void StartApp () {}
public void Pauseapp () {}
public void Destroyapp (Boolean unconditional) {}
}
FileName: Snakecanvas.java
Package snake;
Import java.util.*;
Import javax.microedition.lcdui.*;
/**
* Snake Game
*/
public class Snakecanvas extends Canvas implements runnable{
/** storage of snake node coordinates, where the second dimension subscript 0 represents the X coordinate, the second dimension subscript is 1 of the y-coordinate.
Int[][] snake = new int[200][2];
/** number of nodes already in use *
int snakenum;
/** Greedy snake Movement direction, 0 represents upward, 1 represents downward, 2 represents to the left, 3 represents to the 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 Area Width * *
int width;
/** Game Area Height *
int height;
/** Snake Body Unit Width * *
Private final byte snakewidth = 4;
/** is in a paused state, true represents a pause * *
Boolean ispaused = false;
/** is in a running state, true represents the run/
Boolean isrun = true;
/** time interval * *
Private final int sleep_time = 300;
X-coordinate of/** food
int Foodx;
Y-coordinate of/** food
int foody;
Flashing control of/** food
Boolean B = true;
/**random Object * *
Random Random = new Random ();
Public Snakecanvas () {
Class
Init ();
width = This.getwidth ();
Height = this.getheight ();
Start thread
New Thread (This). Start ();
}
/**
* Initialize start data
*/
private void init () {
Initialize number of nodes
Snakenum = 7;
Initializing node data
for (int i = 0;i < snakenum;i++) {
SNAKE[I][0] = 100-snakewidth * i;
SNAKE[I][1] = 40;
}
Initialize the direction of movement
Direction = Direction_right;
Initialize food coordinates
FOODX = 100;
foody = 100;
}
protected void Paint (Graphics g) {
Clear Screen
G.setcolor (0XFFFFFF);
G.fillrect (0,0,width,height);
G.setcolor (0);
//Draw snake body
for (int i = 0;i < snakenum;i++) {
g.fillrect (snake[i][0] , snake[i][1],snakewidth,snakewidth);
}
//Draw Food
if (b) {
g.fillrect (foodx,foody,snakewidth,snakewidth);
}
}
private void Move (int direction) {
Snake Body Moving
for (int i = snakenum-1;i > 0;i--) {
Snake[i][0] = snake[i-1][0];
SNAKE[I][1] = snake[i-1][1];
}
First cell move
switch (direction) {
Case DIRECTION_UP:
SNAKE[0][1] = snake[0][1]-snakewidth;
Break
Case Direction_down:
SNAKE[0][1] = snake[0][1] + snakewidth;
Break
Case Direction_left:
Snake[0][0] = snake[0][0]-snakewidth;
Break
Case Direction_right:
Snake[0][0] = snake[0][0] + snakewidth;
Break
}
}
/**
* Eat food, grow on its own
*/
private void Eatfood () {
Determine if the snake head overlaps with food
if (snake[0][0] = = Foodx && snake[0][1] = = foody) {
snakenum++;
Generatefood ();
}
}
/**
* Produce Food
* Description: Food coordinates must be in the screen, and can not coincide with the snake body
*/
private void Generatefood () {
while (true) {
Foodx = Math.Abs (Random.nextint ()% (Width-snakewidth + 1))
/snakewidth * SNAKEWIDTH;
Foody = Math.Abs (Random.nextint ()% (Height-snakewidth + 1))
/snakewidth * SNAKEWIDTH;
Boolean B = true;
for (int i = 0;i < snakenum;i++) {
if (Foodx = = Snake[i][0] && snake[i][1] = = foody) {
b = false;
Break
}
}
if (b) {
Break
}
}
}
/**
* Judge whether the game is over
* End Condition:
* 1, Snake head beyond the boundary
* 2, the snake head touches itself
*/
Private Boolean isgameover () {
Boundary discrimination
if (Snake[0][0] < 0 | | snake[0][0] > (width-snakewidth) | |
Snake[0][1] < 0 | | Snake[0][1] > (height-snakewidth)) {
return true;
}
Meet their own
for (int i = 4;i < snakenum;i++) {
if (snake[0][0] = = Snake[i][0]
&& snake[0][1] = = Snake[i][1]) {
return true;
}
}
return false;
}
/**
* Event Handling
*/
public void keypressed (int keycode) {
int action = this.getgameaction (KeyCode);
Change 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
}
}
/**
* Threading Method
* Use precise delay
*/
public void Run () {
try{
while (Isrun) {
Start time
Long start = System.currenttimemillis ();
if (!ispaused) {
Eat food
Eatfood ();
Move
Move (direction);
End Game
if (Isgameover ()) {
Break
}
Control Flicker
b =!b;
}
Re-draw
Repaint ();
Long end = System.currenttimemillis ();
Delay
if (End-start < Sleep_time) {
Thread.Sleep (Sleep_time-(End-start));
}
}
}catch (Exception e) {}
}
}
Advertising: If you think the article is helpful to you, please vote for me in MVB, thank you. Vote address: Http://www.csdn.net/community2006/vote/index.rails?id=1