?? mangamecanvas.java~175~
字號:
package man;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.game.GameCanvas;
/**
*
*/
public class ManGameCanvas extends GameCanvas
implements Runnable, CommandListener {
//--- 定義變量 ----------------------------------------------------------------
private ManMIDlet manMIDlet;
private Command Cmdstart, Cmdrestart; // 開始、重新開始按鍵
private int SWidth, SHeight; // 屏幕寬度和高度
private int KeyState; // 當前按鍵狀態
private Graphics ManGraphics; // 定義屏幕的畫布
private TiledLayer TLBackground; // 平鋪層對象
private Sprite SpPlane; // 飛機圖象
private ManBullets MBbullets; // 子彈圖象
private NumImgTools TimeFont; // 時間文字
public Sprite SpExplosion; // 飛機爆炸效果圖象
private Sprite SpBomb; // 炸彈爆炸效果圖片
private Image ImgBombIcon; // 炸彈圖標
private NumImgTools BombFont; // 炸彈個數文字
private int BombCount; // 炸彈個數
private boolean useBomb; // 是否使用炸彈
private Thread ThRunGame; // 線程
private boolean running; // 當前運行狀態
public boolean gameover; // 程序是否已經結束
private long GameStartTime; // 游戲開始時間
private int ExplosionFrame; // 飛機爆炸幀數控制累加器
private int BombFrame; // 炸彈爆炸幀數控制累加器
int a;
//--- 構造函數 ----------------------------------------------------------------
protected ManGameCanvas(ManMIDlet manMIDlet) {
super(false); // 調用父類的構造方法,必須這樣,否則無法編譯
this.manMIDlet = manMIDlet;
// 添加按鍵,并開啟監聽
addCommand(new Command("退出", Command.EXIT, 1));
addCommand(Cmdstart = new Command("開始", Command.OK, 1));
setCommandListener(this);
SWidth = getWidth(); // 獲得設備屏幕寬度
SHeight = getHeight(); // 獲得設備屏幕高度
running = false; // 當前運行狀態
ThRunGame = null; // 線程初始化
ManGraphics = getGraphics(); // 獲得當前屏幕的畫布
//--- 初始化背景圖象 ---
Image img = ImageTools.getImage("/pic/back_water.png");
int backcols = SWidth / img.getWidth() + 1; // 計算橫向需要多少圖片
int backrows = SHeight / img.getHeight() + 1; // 計算縱向需要多少圖片
// 初始化平鋪層對象,將圖象平鋪顯示
TLBackground = new TiledLayer(backcols, backrows, img, img.getWidth(), img.getHeight());
// 循環平鋪操作,x,y為相對坐標,???setCell中的1不知是什么意思???
for (int i = 0; i < backcols * backrows; i++)
TLBackground.setCell(i % backcols, i / backcols, 1);
//--- 初始化小飛機圖象 ---
img = ImageTools.getImage("/pic/MyPlaneFrames.png");
SpPlane = new Sprite(img, 24, 24);
//--- 初始化記時數字圖象 ---
img = ImageTools.getImage("/pic/b_number.png");
TimeFont = new NumImgTools(ManGraphics, img, 10, 15);
//--- 初始化子彈圖象 ---
img = ImageTools.getImage("/pic/bullet.png");
MBbullets = new ManBullets(this, img, SWidth, SHeight);
//--- 初始化飛機爆炸效果圖片 ---
img = ImageTools.getImage("/pic/explosion.png");
SpExplosion = new Sprite(img, 32, 32);
//--- 初始化炸彈爆炸效果圖片 ---
img = ImageTools.getImage("/pic/bomb.png");
SpBomb = new Sprite(img, 65, 65);
//--- 初始化炸彈圖標 ---
ImgBombIcon = ImageTools.getImage("/pic/bomb_icon.png");
//--- 初始化記時數字圖象 ---
img = ImageTools.getImage("/pic/s_number.png");
BombFont = new NumImgTools(ManGraphics, img, 5, 7);
}
//--- 線程主運行 --------------------------------------------------------------
public void run(){
long st = 0, et = 0, diff = 0;
int GameTime = 0, TempTime = 0;
int rate = 50; // 16-17 幀/每妙
while (running) {
st = System.currentTimeMillis();
ManGraphics.setColor(0, 0, 0); // 清空屏幕
ManGraphics.fillRect(0, 0, SWidth, SHeight);
TLBackground.paint(ManGraphics); // 畫出背景圖象
if (!gameover) {
SpPlane.paint(ManGraphics); // 畫出飛機圖象
MBbullets.paint(ManGraphics, SpPlane.getX(), SpPlane.getY()); // 畫出子彈
GameKeyInput(); // 游戲操作
// 獲得游戲時間
TempTime = (int)(System.currentTimeMillis() - GameStartTime) / 1000;
if (GameTime != TempTime) {
GameTime = TempTime;
// 每隔20秒獎勵一個炸彈
if (GameTime % 20 == 0 && GameTime != 0) ++BombCount;
}
if (GameTime == 99) { // 若游戲時間達到99秒,強行退出游戲
ExplosionFrame = 3;
gameover = true;
}
if (useBomb) { // 若使用炸彈,顯示爆炸效果
if (BombFrame < 6) {
SpBomb.setFrame(++BombFrame);
SpBomb.paint(ManGraphics);
MBbullets.BombBullets(SpPlane.getX(), SpPlane.getY());
}
else useBomb = false;
}
}
// 如果游戲已經結束
else {
if (ExplosionFrame < 3) { // 顯示爆炸效果
SpExplosion.setFrame(++ExplosionFrame);
SpExplosion.paint(ManGraphics);
}
else { // 顯示評語
String GameOverStr;
if (GameTime < 10) GameOverStr = "你是在用腳玩嗎?";
else if (GameTime < 16) GameOverStr = "再來一次,你能做到!";
else if (GameTime < 20) GameOverStr = "同情你,再來一次!";
else if (GameTime < 25) GameOverStr = "非常好,你是一個真正的男人!";
else if (GameTime < 30) GameOverStr = "你是玩這個游戲的天才!";
else if (GameTime < 40) GameOverStr = "真不敢相信,這個游戲難道是你做的?";
else if (GameTime < 99) GameOverStr = "噢,我的上帝!你是人類嗎?";
else GameOverStr = "我很遺憾,你可以不用再完這個游戲了!";
ManGraphics.setColor(255,255,255);
ManGraphics.drawString(GameOverStr, 5, 25, ManGraphics.LEFT | ManGraphics.TOP);
}
}
// 畫出記時文字
TimeFont.drawNums(GameTime, (SWidth - 14 * 2) / 2, 10, 2);
// 畫出炸彈圖標
ManGraphics.drawImage(ImgBombIcon, 0, SHeight - 1, ManGraphics.BOTTOM | ManGraphics.LEFT);
// 畫出炸彈個數文字
BombFont.drawNum(BombCount, ImgBombIcon.getWidth() + 1, SHeight - 8);
ManGraphics.drawString(String.valueOf(a), 5, 50, ManGraphics.LEFT | ManGraphics.TOP);
flushGraphics(); // 刷新圖象顯示,沒這句不顯示圖片
et = System.currentTimeMillis();
// 不滿足幀數要求,暫停線程至要求幀數
diff = et - st;
if (diff < rate) {
try {
Thread.sleep(rate - diff);
}
catch (InterruptedException ex) {
System.out.println(ex);
}
}
}
}
//--- 監聽按鍵 ----------------------------------------------------------------
public void commandAction(Command command, Displayable displayable) {
// 退出操作
if (command.getCommandType() == Command.EXIT) {
if (running) // 結束線程
running = false;
manMIDlet.quitApp();
}
// 開始操作
else if (command == Cmdstart) {
if (!running) {
running = true;
GameInit(); // 游戲初始化
ThRunGame = new Thread(this); // 初始化線程
ThRunGame.start();
}
removeCommand(Cmdstart);
addCommand(Cmdrestart = new Command("重新開始", Command.OK, 1));
}
// 重新開始操作
else if (command == Cmdrestart) {
GameInit();
}
}
//--- 游戲初始化 --------------------------------------------------------------
private void GameInit() {
gameover = false;
// 初始化飛機圖象
SpPlane.setFrame(0);
SpPlane.setPosition((SWidth - SpPlane.getWidth()) / 2,
(SHeight - SpPlane.getHeight()) / 2);
MBbullets.InitBulletsLocus();
GameStartTime = System.currentTimeMillis();
ExplosionFrame = -1;
BombCount = 3;
useBomb = false;
}
//--- 游戲操作 ----------------------------------------------------------------
private void GameKeyInput() {
SpPlane.setFrame(0);
KeyState = getKeyStates(); // 獲得當前按鍵狀態
// 根據按鍵狀態,飛機圖象做相對移動,并判斷是否超出屏幕邊界
if ((KeyState & UP_PRESSED) != 0) { // 上
if (SpPlane.getY() - 3 < 0)
SpPlane.move(0, 0 - SpPlane.getY());
else
SpPlane.move(0, -3);
SpPlane.setFrame(0);
}
if ((KeyState & DOWN_PRESSED) != 0) { // 下
if (SpPlane.getY() + 3 + SpPlane.getHeight() > SHeight)
SpPlane.move(0, SHeight - SpPlane.getY() - SpPlane.getHeight());
else
SpPlane.move(0, 3);
SpPlane.setFrame(0);
}
if ((KeyState & LEFT_PRESSED) != 0) { // 左
if (SpPlane.getX() - 3 < 0)
SpPlane.move(0 - SpPlane.getX(), 0);
else
SpPlane.move(-3, 0);
SpPlane.setFrame(1);
}
if ((KeyState & RIGHT_PRESSED) != 0) { // 右
if (SpPlane.getX() + 3 + SpPlane.getWidth() > SWidth)
SpPlane.move(SWidth - SpPlane.getX() - SpPlane.getWidth(), 0);
else
SpPlane.move(3, 0);
SpPlane.setFrame(2);
}
if ((KeyState & 4096) != 0) { // 中
if (!useBomb && BombCount > 0) {
BombCount--;
SpBomb.setPosition(SpPlane.getX() - 20, SpPlane.getY() - 20);
useBomb = true;
BombFrame = -1;
}
}
}
protected void keyPressed(int key) {
a = key;
System.out.println(key);
//repaint();
}
/* public void paint(Graphics graphics) {
graphics.drawString(String.valueOf(a), 5, 100, ManGraphics.LEFT | ManGraphics.TOP);
}*/
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -