import java.lang.*;
import javax.microedition.lcdui.*;
import java.util.Random;
import javax.microedition.rms.*;
import java.io.*;
class MainPit extends Canvas implements Runnable
{
MainMid myMid;
//按键表
private static final byte KEY_NONE = 0;
private static final byte KEY_UP = -1;
private static final byte KEY_DOWN = -2;
private static final byte KEY_LEFT = -3;
private static final byte KEY_RIGHT = -4;
private static final byte KEY_FIRE = -5;
private static final byte KEY_GAM_LEFT = -6;
private static final byte KEY_GAM_RIGHT = -7;
private static final byte KEY_NUM5 = 53;
private int hangfire = 300;//延时大小
Graphics gb;
private Image bufImg;//缓存
//屏幕大小
private int nWidth;
private int nHeight;
public MainPit(MainMid mid)
{
myMid = mid;
nWidth = getWidth();//屏幕大小
nHeight = getHeight();
cw = nWidth / 10;
ch = nHeight / 10;
try{
bufImg = Image.createImage(nWidth,nHeight);//申请缓存空间
gb = bufImg.getGraphics();
}catch(Exception e){}
}
public void paint(Graphics g)
{
gb = g;
}
private void showBegin()
{
gb.setColor(0x0000ff00);
gb.fillArc(begin_x * cw, begin_y * ch, cw, ch, 0, 360);
}
int state = 0;
public void run()
{
while(true)
{
switch(state)
{
case 0:
showMap();
showCursor();
break;
case 1:
showMap();
showBegin();
showCursor();
break;
case 2:
showMap();
showBegin();
showfather(end_x, end_y);
showCursor();
break;
}
repaint();
serviceRepaints();
try{
Thread.sleep(hangfire);
System.gc();
Thread.yield();
}catch(Exception e){}
}
}
private int cx,cy;
private void showCursor()
{
gb.setColor(0x000000ff);
gb.drawRect(cx * cw+2,cy*ch+2,cw-4,ch-4);
}
private int cw,ch;
private void showMap()
{
clearScreen(0x00ffffff);
for(int i =0 ; i < 10; i++){
for(int j = 0; j < 10; j++){
if(moveSpace[j]==1){
gb.setColor(0x00000000);
gb.drawRect(j * cw, i * ch, cw, ch);
}
else{
gb.setColor(0x00000000);
gb.fillRect(j * cw, i * ch, cw, ch);
}
}
}
}
private void clearScreen(int c)//用颜色c刷新屏幕
{
gb.setColor(c);
gb.fillRect(0,0,nWidth,nHeight);
}
public void keyPressed(int keyCode)
{
switch(keyCode)
{
case KEY_UP:
if(cy>0)
cy--;
break;
case KEY_DOWN:
if(cy <9)
cy++;
break;
case KEY_LEFT:
if(cx > 0)
cx--;
break;
case KEY_RIGHT:
if(cx < 9)
cx++;
break;
case KEY_FIRE:
case KEY_GAM_LEFT:
case KEY_NUM5:
switch(state)
{
case 0:
begin_x = cx;
begin_y = cy;
state = 1;
break;
case 1:
end_x = cx;
end_y = cy;
AAsterisk();
state = 2;
break;
case 2:
state = 0;
break;
}
break;
case KEY_GAM_RIGHT:
myMid.exit();
break;
}
}
private int moveSpace[][] = {
{1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,