?? boatgamecanvas.java
字號:
package boat;
import javax.microedition.lcdui.*;
/**
* 游戲界面
*/
public class BoatGameCanvas extends Canvas implements Runnable {
/**背景圖片*/
private Image bgImage;
/**軍艦圖片數組,下標0代表向右,1代表向左*/
private Image boatImage[] = new Image[2];
/**軍艦對象*/
Boat boat;
/**潛艇對象數組*/
Submarine[] submarine = new Submarine[2];
/**第一組潛艇圖片*/
private Image[] sImage1 = new Image[2];
/**第二組潛艇圖片*/
private Image[] sImage2 = new Image[2];
/**爆炸圖片*/
private Image[] boomImage = new Image[3];
/**炸彈圖片*/
private Image bombImage;
/**炸彈對象*/
private Bomb bomb;
//雙緩沖
/**次畫面*/
private Image offScreen;
/**次畫筆*/
private Graphics offG;
/**屏幕寬度*/
private int width;
/**屏幕高度*/
private int height;
/**是否運行*/
private boolean isRunning = true;
/**是否連續移動*/
private boolean isRepeated = false;
/**軍艦移動方向*/
private int boatDirection;
/**游戲得分*/
private int score = 0;
public BoatGameCanvas() {
try {
//導入背景圖片
bgImage = Image.createImage("/res/bac.png");
//導入軍艦圖片
boatImage[0] = Image.createImage("/res/ship0.png");
boatImage[1] = Image.createImage("/res/ship1.png");
//導入炸彈圖片
bombImage = Image.createImage("/res/boom.png");
//導入潛艇圖片
sImage1[0] = Image.createImage("/res/q1.png");
sImage1[1] = Image.createImage("/res/h2.png");
sImage2[0] = Image.createImage("/res/r1.png");
sImage2[1] = Image.createImage("/res/q2.png");
//導入炸彈圖片
boomImage[0] = Image.createImage("/res/b.png");
boomImage[1] = Image.createImage("/res/b1.png");
boomImage[2] = Image.createImage("/res/b2.png");
} catch (Exception e) {
e.printStackTrace();
}
//獲得屏幕寬度和高度
width = this.getWidth();
height = this.getHeight();
//創建次畫面和次畫筆
offScreen = Image.createImage(width, height);
offG = offScreen.getGraphics();
//創建軍艦對象
boat = new Boat(boatImage, width);
//創建潛艇對象
init(0);
init(1);
//創建炸彈對象
bomb = new Bomb(bombImage, 0, 0, height);
//創建線程
Thread t = new Thread(this);
//啟動線程
t.start();
}
private void init(int num) {
if (num == 0) {
submarine[0] = new Submarine(sImage1, width, height, 0, boomImage);
}
if (num == 1) {
submarine[1] = new Submarine(sImage2, width, height, 1, boomImage);
}
}
/**
* 繪制方法
* @param g 系統畫筆
*/
protected void paint(Graphics g) {
//清屏
offG.setColor(0xffffff);
offG.fillRect(0, 0, width, height);
offG.setColor(0);
//繪制背景圖片
offG.drawImage(bgImage, 0, 0, Graphics.TOP | Graphics.LEFT);
//繪制軍艦
boat.paint(offG);
//繪制潛艇
submarine[0].paint(offG);
submarine[1].paint(offG);
//繪制炸彈
if (bomb.isDisplay) {
bomb.paint(offG);
}
//繪制得分
offG.drawString("得分:" + score,4,4,Graphics.TOP | Graphics.LEFT);
//將次畫面貼到主畫面
g.drawImage(offScreen, 0, 0, Graphics.TOP | Graphics.LEFT);
}
public void keyPressed(int keyCode) {
//轉換為游戲動作
int action = this.getGameAction(keyCode);
switch (action) {
case Canvas.LEFT: //向左移動軍艦
isRepeated = true;
boatDirection = Boat.BOAT_MOVE_LEFT;
break;
case Canvas.RIGHT:
isRepeated = true;
boatDirection = Boat.BOAT_MOVE_RIGHT;
break;
case Canvas.FIRE:
//判斷是否可以發射
if (!bomb.isDisplay) {
bomb.isDisplay = true;
//設置坐標
bomb.setX(boat.x + boat.getBoatImageWidth() / 2 -
bomb.getImageWidth() / 2);
bomb.setY(boat.Y + boat.getBoatImageHeight() -
bomb.getImageHeight());
}
break;
}
//重新繪制
repaint();
}
public void keyReleased(int keyCode) {
//轉換為游戲動作
int action = this.getGameAction(keyCode);
switch (action) {
case Canvas.LEFT: //向左移動軍艦
isRepeated = false;
break;
case Canvas.RIGHT:
isRepeated = false;
break;
}
}
/**
* 運行方法
*/
public void run() {
while (isRunning) {
//暫停
try {
Thread.sleep(100);
} catch (Exception e) {}
//移動軍艦
if (isRepeated) {
boat.move(boatDirection);
}
//未爆炸則移動潛艇
if (!submarine[0].isIsBoom()) {
submarine[0].move();
} else { //播放爆炸效果
submarine[0].boom();
}
if (!submarine[1].isIsBoom()) {
submarine[1].move();
} else { //播放爆炸效果
submarine[1].boom();
}
//移動炸彈
if (bomb.isDisplay) {
bomb.move();
//判別是否碰撞
if (bomb.collidesWith(submarine[0])) {
bomb.handlecollidesWith(submarine[0]);
//分數增加
score += 10;
}
if (bomb.collidesWith(submarine[1])) {
bomb.handlecollidesWith(submarine[1]);
//分數增加
score += 10;
}
}
//垃圾回收
System.gc();
//重繪
repaint();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -