?? gamecanvasdemo.java
字號:
/*
* GameCanvasDemo.java
*
* Created on 2005年5月2日, 下午12:14
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
/**
*
* @author Liu Bin
* @version
*/
public class GameCanvasDemo extends MIDlet implements CommandListener {
Display display = null;
private Command cmdExit = new Command("Exit", Command.STOP, 1);
private GameFrameCanvas can = null;
public GameCanvasDemo() {
display = Display.getDisplay(this);
can = new GameFrameCanvas(false);
can.addCommand(cmdExit);
can.setCommandListener(this);
}
public void startApp() throws MIDletStateChangeException {
display.setCurrent(can);
can.startgame();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
can.gameover();
}
/**
* 命令按鈕事件
*/
public void commandAction(Command c, Displayable d) {
if (c == cmdExit) {
notifyDestroyed();
}
}
}
class GameFrameCanvas extends GameCanvas implements Runnable{
//游戲結束標志
private boolean gameover = false;
//游戲背景圖像
private Image imgMissile, imgBG;
//后臺緩沖屏幕寬和高
private int canW, canH;
//導彈圖像的寬,高,寬度的一半和高度的一半
private int imgW, imgH, imgHalfW, imgHalfH;
//導彈的移動方向,缺省為向右
private int direction = RIGHT;
//導彈的移動速度,缺省值為1
private int speed = 1;
//導彈的當前位置
private int curX,curY;
public GameFrameCanvas(boolean suppressKeyEvents) {
super(suppressKeyEvents);
//裝載游戲圖像
try {
imgMissile = Image.createImage("/Res/Missile.png");
out("裝載導彈圖像...成功");
imgBG = Image.createImage("/Res/BG.png");
out("裝載背景圖像...成功");
} catch (Exception e) {
out("裝載游戲圖像異常:" + e.toString());
}
//計算游戲中使用的一些值
canW = getWidth();
canH = getHeight();
out("畫布的寬度:" + canW);
out("畫布的高度:" + canH);
imgW = imgMissile.getWidth();
imgH = imgMissile.getHeight();
imgHalfW = imgW / 2;
imgHalfH = imgH / 2;
curX = canW/2;
curY = canH/2;
}
//清除屏幕
private void clearScreen(Graphics g) {
g.setColor(0xFFFFFF);
g.fillRect(0, 0, canW, canH);
g.setColor(0x000000);
//繪制背景圖像
if (imgBG != null) {
g.drawImage(imgBG, 0, 0, Graphics.TOP | Graphics.LEFT);
}
}
//當用戶按下0-9鍵時調整火箭的運行速度
protected void keyReleased(int keyCode) {
//有些手機實現(xiàn)中數(shù)字鍵2,4,6,8可能會被映射為up, left, right and down鍵,
//因此這里通過鍵值調整運行速度
if (keyCode >= KEY_NUM1 && keyCode <= KEY_NUM9) {
speed = (keyCode - KEY_NUM0)*5;
out("游戲速度調整為:" + speed);
}
}
//輸出調試信息
private void out(String msg) {
System.out.println(msg);
}
public void run() {
//獲得后臺緩沖屏幕
Graphics g = getGraphics();
while (!gameover) {
clearScreen(g);
//根據(jù)按鈕移動導彈
int keyState = getKeyStates();
if ((keyState & LEFT_PRESSED) != 0) {
direction = LEFT;
move(-speed, 0);
} else if ((keyState & RIGHT_PRESSED) != 0) {
direction = RIGHT;
move(speed, 0);
} else if ((keyState & UP_PRESSED) != 0) {
direction = UP;
move(0, -speed);
} else if ((keyState & DOWN_PRESSED) != 0) {
direction = DOWN;
move(0, speed);
}
//繪制游戲屏幕
gameRender(g);
//如果有精靈,這里繪制精靈
//sprite.paint(g);
//繪制后臺緩沖屏幕
flushGraphics();
try {
Thread.sleep(100); //暫停一段時間
} catch(InterruptedException ex){
}
}
}
//移動導彈
private void move(int dx, int dy) {
curX += dx;
curY += dy;
}
private void gameRender(Graphics g) {
int x=0,y =0;
int transform = Sprite.TRANS_NONE;
//繪制不同方向的導彈
if (imgMissile != null) {
switch (direction) {
case LEFT: {
x = curX - imgHalfW;
y = curY - imgHalfH;
transform = Sprite.TRANS_ROT180;
break;
}
case RIGHT: {
transform = Sprite.TRANS_NONE;
x = curX - imgHalfW;
y = curY - imgHalfH;
break;
}
case UP: {
transform = Sprite.TRANS_ROT270;
x = curX - imgHalfH;
y = curY - imgHalfW;
break;
}
case DOWN: {
transform = Sprite.TRANS_ROT90;
x = curX - imgHalfH;
y = curY - imgHalfW;
break;
}
}
g.drawRegion(imgMissile, 0, 0, imgW, imgH, transform,
x, y, Graphics.TOP | Graphics.LEFT);
}
}
//結束游戲
public void gameover() {
gameover = true;
}
//開始游戲
public void startgame() {
//初始化...
gameover = false;
//開始游戲線程
new Thread(this).start();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -