import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.lang.Thread;
public class MainMid extends MIDlet {
MainPit myScreen;
boolean first = true;
public MainMid() {
myScreen = new MainPit(this);
}
protected void destroyApp(boolean unconditional) {
}
protected void pauseApp() {
}
protected void startApp()// J2ME程序的执行线程,自动执行响应按键。
{
Display.getDisplay(this).setCurrent(myScreen);// myScreen获得当前屏幕的显示权
if (first)// 限定执行线程只执行一次
{
try {
Thread myThread = new Thread(myScreen);// 开启一个线程用来实现游戏的逻辑与绘制
myThread.start();// 打开线程
} catch (Error e) {
destroyApp(false);
notifyDestroyed();
}
first = false;
}
}
// 游戏结束
public void exit()// 关闭执行线程,整体程序退出
{
destroyApp(true);
notifyDestroyed();
}
}
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 = nWidth;// getHeight();
cw = nWidth / mWidth;//列
ch = nHeight / mHeight;//行
try {
bufImg = Image.createImage(nWidth, nHeight);// 申请缓存空间
gb = bufImg.getGraphics();
} catch (Exception e) {
}
}
public void paint(Graphics g) {
g.setColor(0);
g.fillRect(0, 0, nWidth, getHeight());
g.drawImage(bufImg, 0, 0, 0);
g.setColor(0xff00);
g.drawString("" + ttime, 0, getHeight(), 36);
}
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=10, cy=0;
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 < mHeight; i++) {
for (int j =