import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Font;
import java.util.Random;
public class Demo extends MIDlet
{
Display dis = null;
public Demo()
{
dis = Display.getDisplay(this);
}
protected void startApp()
{
dis.setCurrent(new Game(this));
}
protected void pauseApp() {}
protected void destroyApp(boolean arg0) {}
}
class Game extends Canvas implements Runnable
{
private boolean is_run = false;
private Demo demo = null;
private int i_runnum = 0;
private int i_score = 0;
private int i_level = 0;
private int i_time = 0;
private final int LCD_WIDTH = this.getWidth();
private final int LCD_HEIGHT = this.getHeight();
private final int LCD_CENTERH = LCD_WIDTH >> 1;
private final int LCD_CENTERV = LCD_HEIGHT >> 1;
private byte i_gamestate = 0;
private final byte GS_LOGO = 0;
private final byte GS_TITLE = 1;
private final byte GS_MENU = 2;
private final byte GS_GAME = 3;
private final byte GS_GAMELOSE = 4;
// private int i_menustate = 0;
// private final byte MS_MAIN = -1;
private final byte MS_START = 0;
// private final byte MS_CONTINUE = 1;
// private final byte MS_S = 2;
// private final byte MS_SET = 3;
private final byte MS_EXIT = 4;
private final byte KEY_UP = -1;
private final byte KEY_DOWN = -2;
private final byte KEY_LEFT = -3;
private final byte KEY_RIGHT = -4;
private final byte KEY_FREE = -5;
private final byte KEY_RIGHT_SOFT = -7;
private Block head = null, end = null, current = null;
private int foodx = 0, foody = 0;
private byte i_drection = 0;//方向
private Random ran = null;
private Font font = null;
private Font font1 = null;
private String[] s_menu = null;
private int i_menunum = 0;
Game(Demo demo)
{
i_gamestate = 0;
i_score = 0;
i_level = 1;
font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_LARGE);
font1 = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_ITALIC, Font.SIZE_LARGE);
s_menu = new String[]{"开始游戏","继续游戏","游戏说明","游戏设置","退出游戏"};
this.demo = demo;
new Thread(this).start();
}
final private void initGame()
{
ran = new Random();
while((foodx == 0 || foodx == 1 || foodx == 2 || foodx == 3) && foody == 0)
{
foodx = (ran.nextInt() >>> 1) % 20;
foody = (ran.nextInt() >>> 1) % 20;
}
head = null;
end = null;
current = null;
head = end = new Block(3, 0);
end.next = new Block(2, 0);
end.next.parent = end;
end = end.next;
end.next = new Block(1, 0);
end.next.parent = end;
end = end.next;
end.next = new Block(0, 0);
end.next.parent = end;
end = end.next;
i_drection = 0;
}
public void paint(Graphics g)
{
switch(i_gamestate)
{
case GS_LOGO:
paintLogo(g);
break;
case GS_TITLE:
paintTitle(g);
break;
case GS_MENU:
paintMenu(g);
break;
case GS_GAME:
paintGame(g);
break;
case GS_GAMELOSE:
paintGameLose(g);
break;
}
}
private void paintLogo(Graphics g)
{
g.setColor(-1);
g.fillRect(0, 0, LCD_WIDTH, LCD_HEIGHT);
g.setFont(font);
g.setColor(255, 0, 0);
if(i_runnum < 10)
{
g.drawString("SP", LCD_CENTERH, LCD_CENTERV, Graphics.BOTTOM | Graphics.HCENTER);
}
else if(i_runnum < 20)
{
g.drawString("CP", LCD_CENTERH, LCD_CENTERV, Graphics.BOTTOM | Graphics.HCENTER);
}
else
{
i_gamestate = GS_TITLE;
}
}
private void paintTitle(Graphics g)
{
g.setColor(-1);
g.fillRect(0, 0, LCD_WIDTH, LCD_HEIGHT);
g.setFont(font);
if(i_runnum % 2 == 0)
{
g.setColor(255, 0, 0);
}
else
{
g.setColor(0, 255, 255);
}
g.drawString("prass any key", LCD_CENTERH, LCD_CENTERV, Graphics.BOTTOM | Graphics.HCENTER);
}
private void paintMenu(Graphics g)
{
System.gc();
g.setColor(-1);
g.fillRect(0, 0, LCD_WIDTH, LCD_HEIGHT);
for(int i = 0; i < s_menu.length; i++)
{
if(i_menunum == i)
{
g.setColor(255, 0, 0);
g.setFont(font1);
g.drawString(s_menu[i], LCD_CENTERH - 5, 70 + i * 25 - 4, Graphics.BOTTOM | Graphics.HCENTER);
}
else
{
g.setColor(0, 255, 0);
g.setFont(font);
g.drawString(s_menu[i], LCD_CENTERH, 70 + i * 25, Graphics.BOTTOM | Graphics.HCENTER);
}
}
}
private void paintGame(Graphics g)
{
System.gc();
g.setColor(-1);
g.fillRect(0, 0, LCD_WIDTH, LCD_HEIGHT);
g.translate(20, 20);
g.setColor(150, 150, 150);
g.fillRect(0, 0, 200, 200);
g.setColor(0, 0, 255);
g.drawRect(-1, -1, 201, 201);
g.drawRect(-2, -2, 203, 203);
g.setColor(255, 0, 0);
g.fillRect(foodx * 10, foody * 10, 10, 10);
g.setColor(255, 255, 0);
g.fillRect(head.x * 10, head.y * 10, 10, 10);
g.setColor(0, 255, 0);
current = head.next;
while(current != null)
{
g.fillRect(current.x * 10, current.y * 10, 10, 10);
current = current.next;
}
g.translate(-20, -20);
g.setColor(0);
g.drawString("score:" + i_score, 60, 240, Graphics.TOP | Graphics.HCENTER);
g.drawString("level:" + i_level, 120, 240, Graphics.TOP | Graphics.HCENTER);
}
private void paintGameLose(Graphics g)
{
System.gc();
g.setColor(255, 0, 0);
g.setFont(font);
g.drawString("GAME OVER", LCD_CENTERH, LCD_CENTERV, Graphics.BOTTOM | Graphics.HCENTER);
g.drawString("MENU", LCD_WIDTH, LCD_HEIGHT, Graphics.RIGHT |Graphics.BOTTOM);
}
private void move()
{
head.parent = end;
end.next = head;
head = end;
end = end.parent;
end.next = null;
head.parent = null;
switch(i_drection)
{
case 0://Right
head.x = head.next.x + 1;
head.y = head.next.y;
break;
case 1://down
head.x = head.next.x;
head.y = head.next.y + 1;
break;
case 2://left
head.x = head.next.x - 1;
head.y = head.next.y;
break;
case 3://up
head.x = head.next.x;
head.y = head.next.y - 1;
break;
}
}
private void eat()
{
System.gc();
if(head.x == foodx && head.y == foody)
{
end.next = new Block(end.x, end.y);
end.next.parent = end;
end = end.next;
i_score += 10;
foodx = (ran.nextInt() >>> 1) % 20;
foody = (ran.nextInt() >>> 1) % 20;
current = head;
while(current != null)
{
if(current.x == foodx && current.y == foody)
{
foodx = (ran.nextInt() >>> 1) % 20;
foody = (ran.nextInt() >>> 1) % 20;
current = head;
}
else
{
current = current.next;
}
}
}
}
private void check()
{
if(head.x < 0 || head.x > 19 || head.y < 0 || head.y > 19)
{
i_gamestate = GS_GAMELOSE;
}
current = head.next;
while(current != null && (head.x != current.x || head.y != current.y))
{
current = current.next;
}
if(current != null)
{
i_gamestate = GS_GAMELOSE;
}
}
private void Score()
{
if(i_score >= 350)
{
i_level = 8;
i_time = 60;
}
else if(i_score >= 300)
{
i_level = 7;
i_time = 80;
}
else if(i_score >= 250)
{
i_level = 6;
i_time = 100;
}
else if(i_score >= 200)
{
i_level = 5;
i_time = 120;
}
else if(i_score >= 150)
{
i_level = 4;
i_time = 140;
}
else if(i_score >= 100)
{
i_level = 3;
i_time = 160;
}
else if(i_score >= 50)
{
i_level = 2;
i_time = 180;
}
else
{
i_level = 1;
i_time = 200;
}
}
public void run()
{
is_run = true;
while(is_run)
{
if(++i_runnum > 1000000)
{
i_runnum = 0;
}
if(i_gamestate == GS_GAME)
{
move();
eat();
check();
}
Score();
repaint();
serviceRepaints();
try
{
Thread.sleep(i_time);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
protected void keyPressed(int key)
{
switch(i_gamestate)
{
case GS_LOGO:
break;
case GS_TITLE:
i_gamestate = GS_MENU;
break;
case GS_MENU:
switch(key)
{
case KEY_UP:
i_menunum = i_menunum == 0 ? 4 : --i_menunum;
break;
case KEY_DOWN:
i_menunum = i_menunum == 4 ? 0 : ++i_menunum;
break;
case KEY_FREE:
switch(i_menunum)
{
// case MS_MAIN:
// break;
case MS_START:
initGame();
i_gamestate = GS_GAME;
break;
// case MS_CONTINUE:
// break;
// case MS_S:
// break;
// case MS_SET:
// break;
case MS_EXIT:
is_run = false;
demo.notifyDestroyed();
break;
}
break;
}
break;
case GS_GAME:
switch(key)
{
case KEY_UP:
if(i_drection != 1)
{
i_drection = 3;
}
break;
case KEY_DOWN:
if(i_drection != 3)
{
i_drection = 1;
}
break;
case KEY_LEFT:
if(i_drection != 0)
{
i_drection = 2;
}
break;
case KEY_RIGHT:
if(i_drection != 2)
{
i_drection = 0;
}
break;
}
break;
case GS_GAMELOSE:
if(key == KEY_RIGHT_SOFT)
{
i_gamestate = GS_MENU;
}
break;
}
}
}
class Block
{
public Block parent = null;
public Block next = null;
public int x = 0, y = 0;
public Block(int x, int y)
{
this.x = x;
this.y = y;
}
}