?? bomb.java
字號(hào):
package boat;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/**
* 炸彈類
*/
public class Bomb {
/**炸彈圖片*/
private Image bombImage;
/**x坐標(biāo)*/
private int x;
/**y坐標(biāo)*/
private int y;
/**是否可見*/
public static boolean isDisplay = false;
/**屏幕高度*/
private int height;
/**圖片高度*/
private int imageHeight;
/**圖片寬度*/
private int imageWidth;
/**移動(dòng)單位*/
private int dy = 2;
/**
* 構(gòu)造方法
* @param bombImage Image 炸彈圖片
* @param x int x坐標(biāo)
* @param y int y坐標(biāo)
* @param height 屏幕高度
*/
public Bomb(Image bombImage, int x, int y, int height) {
this.bombImage = bombImage;
this.x = x;
this.y = y;
this.height = height;
//初始化圖片高度和寬度
this.imageHeight = bombImage.getHeight();
this.imageWidth = bombImage.getWidth();
}
/**
* 繪制方法
* @param g Graphics 畫筆
*/
public void paint(Graphics g) {
g.drawImage(bombImage, x, y, Graphics.TOP | Graphics.LEFT);
}
/**
* 移動(dòng)方法
*/
public void move() {
if (y < height) {
y += dy;
} else {
//使炸彈不可見
isDisplay = false;
}
}
/**
* 是否和潛艇發(fā)生碰撞
* @param submarine Submarine 潛艇對(duì)象
* @return boolean true代表碰撞,false代表未發(fā)生碰撞
*/
public boolean collidesWith(Submarine submarine) {
//炸彈中心點(diǎn)的坐標(biāo)
int bx = x + imageWidth / 2;
int by = y + imageHeight / 2;
//潛艇中心點(diǎn)的坐標(biāo)
int sx = submarine.getX() + submarine.getImageWidth() / 2;
int sy = submarine.getY() + submarine.getImageHeight() / 2;
//判斷中心點(diǎn)之間的距離
if ((Math.abs(bx - sx) < (imageWidth + submarine.getImageWidth()) / 2)
&&
(Math.abs(by - sy) < (imageHeight + submarine.getImageHeight()) / 2)) {
return true;
} else {
return false;
}
}
/**
* 碰撞處理
* @param submarine Submarine 潛艇對(duì)象
*/
public void handlecollidesWith(Submarine submarine) {
//設(shè)置炸彈不可見
isDisplay = false;
//潛艇爆炸
submarine.setIsBoom(true);
}
/**
* 設(shè)置x坐標(biāo)
* @param x int x坐標(biāo)
*/
public void setX(int x) {
this.x = x;
}
/**
* 設(shè)置y坐標(biāo)
* @param y int y坐標(biāo)
*/
public void setY(int y) {
this.y = y;
}
/**
* 獲得炸彈圖片寬度
* @return int 炸彈圖片寬度
*/
public int getImageWidth() {
return imageWidth;
}
/**
* 獲得炸彈圖片高度
* @return int 炸彈圖片高度
*/
public int getImageHeight() {
return imageHeight;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -