資源簡介
package cn.feike.shoot;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public abstract class FlyingObject {
protected double x;//物體的x坐標
protected double y;//物體的y坐標
protected double width;//物體的寬
protected double heigth;//物體的高
protected BufferedImage image;//當前正在顯示的圖片
protected int index = 0;//圖片數組下標序號,子類中使用
protected double step;//飛行物每次(1/24秒)移動的距離
protected int life;//命
protected int state;//飛行物的狀態
public static final int ACTIVE=0;//活著狀態
public static final int DEAD=1;//死亡狀態
public static final int REMOVE=2;//回收狀態
//默認構造器
public FlyingObject() {
life = 1;
state = ACTIVE;
}
//有參構造器
public FlyingObject(double width,double heigth){
this();//調用無參數的構造器,必須寫在第一行.
this.x = (int)(Math.random()*(480-width));
this.y = -heigth;
this.width = width;
this.heigth = heigth;
step = Math.random()*3+0.8;//初始化step為[0.8,3.8)之間的數
}
//重寫toString方法
public String toString() {
return x+","+y+","+width+","+heigth+","+image;
}
//重寫paint,方便子類對象的使用
public void paint(Graphics g) {
g.drawImage(image, (int)x, (int)y, null);//繪制圖片
}
//飛行物移動的move方法
/**
* 重構了move,方法實現播放銷毀動畫功能
*/
public void move(){
if(state == ACTIVE){
y += step;
return ;
}
if(state == DEAD){
//從子類對象中獲取下一張照片
BufferedImage img = nextImage();
if(img == null){
state = REMOVE;//沒有照片則回收
}else{
image = img;//否則把子類的圖片傳給image
}
//越界則銷毀
if(y>=825){
state = REMOVE;
}
}
}
/**
* 子類中必須有的方法,返回下一個要播放的照片引用,
* 如果返回null表示沒有可播放的照片了.
*/
protected abstract BufferedImage nextImage();
/**
* 飛行物被打了一下
*/
public void hit(){
if(life>0){
life--;
}
if(life==0){
state = DEAD;
}
}
/**
* 碰撞檢測的方法
* 檢測物體的位置是否在碰撞的范圍內.
* (子彈是否在飛行物的碰撞范圍內)
*/
public boolean duang(FlyingObject obj){
//this(x,y,w,h)
//obj(x,y,w,h)
double x1 = this.x - obj.width;
double x2 = this.x + this.width;
double y1 = this.y - obj.width;
double y2 = this.y + this.heigth;
return x1<obj.x&&obj;.x<x2&&y1;<obj.y&&obj;.y<y2;
}
/** 重構

代碼片段和文件信息
package?cn.feike.shoot;
import?java.awt.image.BufferedImage;
import?javax.imageio.ImageIO;
public?class?Airplane?extends?Flyingobject?{
private?static?BufferedImage[]?imgs;//?定義一個唯一的圖片素材
//?靜態代碼塊用于獲取圖片資源
static?{
imgs?=?new?BufferedImage[5];
try?{
for?(int?i?=?0;?i? String?png?=?“cn/feike/shoot/airplane“?+?i?+?“.png“;//獲取圖片路徑
imgs[i]?=?ImageIO.read(Airplane.class.getClassLoader().getResourceAsStream(png));
}
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
public?Airplane()?{
super(49?36);//?初始化小飛機
this.image?=?imgs[0];
}
/**
?*?播放下一張圖片
?*/
@Override
protected?BufferedImage?nextImage()?{
index++;
if(index?>=?imgs.length){
return?null;//如果下標大于等于數組長度無圖可播返回null.
}
return?imgs[index];
}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????301??2016-07-18?15:02??飛機大戰\.classpath
?????文件????????391??2016-07-18?15:02??飛機大戰\.project
?????文件????????598??2016-07-18?15:02??飛機大戰\.settings\org.eclipse.jdt.core.prefs
?????文件???????1540??2016-07-22?20:38??飛機大戰\bin\cn\feike\shoot\Airplane.class
?????文件???????3575??2016-07-19?09:58??飛機大戰\bin\cn\feike\shoot\airplane0.png
?????文件???????2619??2016-07-19?09:58??飛機大戰\bin\cn\feike\shoot\airplane1.png
?????文件???????2954??2016-07-19?09:58??飛機大戰\bin\cn\feike\shoot\airplane2.png
?????文件???????4047??2016-07-19?09:58??飛機大戰\bin\cn\feike\shoot\airplane3.png
?????文件???????1439??2016-07-19?09:58??飛機大戰\bin\cn\feike\shoot\airplane4.png
?????文件????????242??2016-07-22?20:38??飛機大戰\bin\cn\feike\shoot\Award.class
?????文件??????26709??2016-07-19?09:58??飛機大戰\bin\cn\feike\shoot\background.png
?????文件???????1969??2016-07-22?20:38??飛機大戰\bin\cn\feike\shoot\Bee.class
?????文件???????6405??2016-07-19?09:58??飛機大戰\bin\cn\feike\shoot\bee0.png
?????文件???????6416??2016-07-19?09:58??飛機大戰\bin\cn\feike\shoot\bee1.png
?????文件???????6428??2016-07-19?09:58??飛機大戰\bin\cn\feike\shoot\bee2.png
?????文件???????6404??2016-07-19?09:58??飛機大戰\bin\cn\feike\shoot\bee3.png
?????文件???????6367??2016-07-19?09:58??飛機大戰\bin\cn\feike\shoot\bee4.png
?????文件???????1566??2016-07-22?20:38??飛機大戰\bin\cn\feike\shoot\BigPlane.class
?????文件???????6762??2016-07-19?09:58??飛機大戰\bin\cn\feike\shoot\bigplane0.png
?????文件???????7932??2016-07-19?09:58??飛機大戰\bin\cn\feike\shoot\bigplane1.png
?????文件???????8682??2016-07-19?09:58??飛機大戰\bin\cn\feike\shoot\bigplane2.png
?????文件???????9831??2016-07-19?09:58??飛機大戰\bin\cn\feike\shoot\bigplane3.png
?????文件???????2672??2016-07-19?09:58??飛機大戰\bin\cn\feike\shoot\bigplane4.png
?????文件????????700??2016-07-22?20:38??飛機大戰\bin\cn\feike\shoot\BigPlaneAward.class
?????文件???????1464??2016-07-22?20:38??飛機大戰\bin\cn\feike\shoot\Bullet.class
?????文件????????408??2016-07-19?09:58??飛機大戰\bin\cn\feike\shoot\bullet.png
?????文件????????131??2016-07-22?20:38??飛機大戰\bin\cn\feike\shoot\Enemy.class
?????文件???????2772??2016-07-22?20:38??飛機大戰\bin\cn\feike\shoot\Flyingob
?????文件??????20220??2016-07-19?09:58??飛機大戰\bin\cn\feike\shoot\gameover.png
?????文件???????2455??2016-07-22?20:38??飛機大戰\bin\cn\feike\shoot\Hero.class
............此處省略114個文件信息
評論
共有 條評論