import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class Simple extends GameCanvas implements Runnable {
private volatile boolean mTrucking;
private long mFrameDelay;
private int x, y;
private int mState;
public Simple() {
super(true);
x = getWidth() / 2;
y = getHeight() / 2;
mState = 0;
mFrameDelay = 20;
}
public void start() {
mTrucking = true;
Thread t = new Thread(this);
t.start();
}
public void stop() {
mTrucking = false;
}
public void run() {
Graphics g = getGraphics();
while (mTrucking == true) {
tick();
input();
draw(g);
try {
Thread.sleep(mFrameDelay);
} catch (InterruptedException ie) {
stop();
}
}
}
private void draw(Graphics g) {
g.setColor(0xffFEE);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0x000000);
g.drawLine(x, y, x - 5 + mState, y - 5);
g.drawLine(x, y, x + 5, y - 5 + mState);
g.drawLine(x, y, x + 5 - mState, y + 5);
g.drawLine(x, y, x - 5, y + 5 - mState);
g.drawString("游戏",0, 0, Graphics.TOP|Graphics.LEFT);
g.drawString("游戏",0, 15, Graphics.TOP|Graphics.LEFT);
flushGraphics();
}
private void tick() {
mState = (mState + 1) % 20;
}
private void input() {
int keyStates = getKeyStates();
if ((keyStates & LEFT_PRESSED) != 0) {
x = Math.max(0, x - 2);
}
if ((keyStates & RIGHT_PRESSED) != 0) {
x = Math.min(getWidth(), x + 2);
}
if ((keyStates & UP_PRESSED) != 0) {
y = Math.max(0, y - 2);
}
if ((keyStates & DOWN_PRESSED) != 0) {
y = Math.min(getHeight(), y + 2);
}
}
}