importjava.awt.Image;importjava.util.ArrayList;importjava.util.Timer;importjavax.swing.ImageIcon;public classPlane {publicImage image;public int y = 300, x = 100, width = 50, height = 50;privateString imageName;private boolean flag = true;//true表示子弹往下走,false表示子弹往上走
private intscore;privateTimer timer;privatePlaneDemo pd;public voidsetPD(PlaneDemo pd) {this.pd =pd;
}public voidsetTimer(Timer timer) {this.timer =timer;
}public Plane(int x, int y, int width, intheight, String imageName,boolean flag, intscore) {this.x =x;this.y =y;this.width =width;this.height =height;this.imageName =imageName;this.flag =flag;this.score =score;
image= new ImageIcon(this.getClass().getResource(imageName))
.getImage();
}public void move(ArrayListlist) {if (imageName.equals("plane.jpg")) {if(flag) {
y+= 3;if (y >= 400)
list.remove(this);
}else{
y-= 3;if (y <= 40)
y= 300;
}
}else if (imageName.equals("bullet.png")) {if(flag)
y+= 3;elsey-= 4;if (y >= 400)
list.remove(this);else if (y + height <= 0)
list.remove(this);
}else if (imageName.equals("background.jpg")) {
y+= 2;if (y >= 0)
y= -226;
}
}/*** 检测当前飞机对象和其他的子弹或者飞机对象是否发生碰撞
*
*@paramlist就是存储了所有敌方飞机和子弹的对象
* (包括自己的)*/
public void collision(ArrayListlist) {for (int i = 1; i < list.size(); i++) {
Plane plane= list.get(i);//获取队列中的飞机或者子弹对象
if (plane != this && plane.flag != flag) {//检测不是自己本身//计算两个圆的距离
double distance = Math.sqrt((plane.x - this.x)* (plane.x - this.x) + (plane.y - this.y)* (plane.y - this.y));//判断两个图片是否发生了碰撞
if (distance <= plane.height / 2 + this.height / 2) {//销毁这两个图片
list.remove(i);
list.remove(this);//判断是否要增加这一局游戏的分数
if (plane.imageName.equals("bullet.png") && !plane.flag) {if (this.imageName.equals("plane.jpg")) {
PlaneDemo.count+= this.score;
System.out.println(">>>>>>>>>>>1");
}
}else if (this.imageName.equals("bullet.png")&& !this.flag) {if (plane.imageName.equals("plane.jpg")) {
PlaneDemo.count+=plane.score;
System.out.println(">>>>>>>>>>>2");
}
}//停止我方飞机的自动生成子弹的线程对象
if ((!plane.flag && plane.imageName.equals("plane.jpg"))) {
plane.timer.cancel();newSaveInfo(plane.pd);
}else if (!this.flag && this.imageName.equals("plane.jpg")) {
timer.cancel();new SaveInfo(this.pd);
}
}
}
}
}