//飞行物是airplane,bee,bullet,hero的父类
public abstract class FlyingObject {
protected BufferedImage image;//图片
protected int width;//图片的宽
protected int height;//图片的高
protected int x;//图片在面板中显示的x坐标
protected int y;//图片在面板中显示的y坐标

//飞行物走一步
public abstract void step();//抽象方法只能放到抽象类或接口里
//判断飞行物是否越界
public abstract boolean outOfBounds();
//敌人被子弹撞上了
public boolean shootBy(Bullet b) {//b传过来的子弹
int x1=this.x;//x1:敌人的x坐标
int x2=this.x+this.width;//x2:敌人的坐标x+敌人的宽
int y1=this.y;//y1:敌人的y坐标
int y2=this.y+this.height;//y2:敌人的y+敌人的高
int x=b.x;//x:子弹的x
int y=b.y;//y:子弹的y

return x>x1&&x &&
  y>y1&&y }

}

Award接口

//小蜜蜂的奖励机制
public interface Award {
//接口中定义的属性为常量
public static final int DOUBLE_FIRE=0;//火力值   
public static final int LIFE=1;//命
//获取奖励机制 返回0代表火力值,返回1代表命
public abstract int getType();//抽象方法
}

Enemy接口

//小敌机的得分机制
public interface Enemy {
//得分
public int getScore();
}

Airplane类

//小敌机:是飞行物  也是敌人
public class Airplane extends FlyingObject implements Enemy {
// protected BufferedImage image;//图片
// protected int width;//图片的宽
// protected int height;//图片的高
// protected int x;//图片在面板中显示的x坐标
// protected int y;//图片在面板中显示的y坐标
private int speed=2;//小敌机的下落速度(走步步数,值越大走的越快)
public Airplane(){
image=ShootGame.airplane;//获取小敌机图片
width=image.getWidth();//获取图片的宽
height=image.getHeight();//获取图片的高
Random rand=new Random();//生成随机数
x=rand.nextInt(ShootGame.WIDTH-this.width);//x: 0到屏幕的宽-敌机图片的宽,在此范围内生成随机数
y=-this.height;//y:-敌机的高
//y=200;
}
//射击一个小敌机加5分
public int getScore() {
// TODO Auto-generated method stub
return 5;
}
@Override
public void step() {
y +=speed;//小敌机向下落

}
//重写outOfBounds(判断小敌机是够越界)
@Override
public boolean outOfBounds() {


return y>=ShootGame.HEIGHT;//小敌机的y坐标大于屏幕的高就越界
}

}


Bee类

//小蜜蜂:是飞行物  ,也是奖励
public class Bee extends FlyingObject implements Award{
private int xSpeed=1;//小蜜蜂x轴方向的速度
private int ySpeed=2;//小蜜蜂y轴方向的下落速度
private int awardType;//奖励类型(双倍火力或者加命)
//构造方法
public Bee(){
image=ShootGame.bee;//获取小蜜蜂图片
width=image.getWidth();//获取图片的宽
height=image.getHeight();//获取图片的高
Random rand=new Random();
x=rand.nextInt(ShootGame.WIDTH-this.width);//在0~(ShootGame.WIDTH-this.width)生成随机数
y=-this.height;
//y=200;
awardType=rand.nextInt(2);//生成0或1的随机数
}
//重写getType()
public int getType() {
// TODO Auto-generated method stub
return awardType;//返回奖励类型(0或1)
}
@Override
public void step() {
y +=ySpeed;//竖直方向移动
x +=xSpeed;//水平方向移动
if(x>=ShootGame.WIDTH-this.width){//如果到了右边界速度变成负的
xSpeed=-1;
}
if(x<=0){//如果到了做边界速度变成正的
xSpeed=1;
}

}
//重写outOfBounds(判断小蜜蜂是否越界)
@Override
public boolean outOfBounds() {
// TODO Auto-generated method stub
return y>=ShootGame.HEIGHT;//小蜜蜂的y坐标大于屏幕的高越界
}

}

Bullet类

//子弹:是飞行物
public class Bullet extends FlyingObject{
private int speed=3;//子弹从下往上速度
public Bullet(int x,int y){
image=ShootGame.bullet;//获取子弹的图片
width=image.getWidth();//获取图片的高
height=image.getHeight();//获取图片的宽
//根据英雄机的坐标来相应的计算子弹的坐标
this.x=x;
this.y=y;
}
@Override
public void step() {
y -=speed;//子弹向上移动

}
//重写outOfBounds(判断子弹是否越界)
@Override
public boolean outOfBounds() {
// TODO Auto-generated method stub
return y<=-this.height;//子弹的y<=负的子弹的高,即为越界
}
}

Hero类

//英雄机:也是飞行物
public class Hero extends FlyingObject{
// protected BufferedImage image;//图片
// protected int width;//图片的宽
// protected int height;//图片的高
// protected int x;//图片在面板中显示的x坐标
// protected int y;//图片在面板中显示的y坐标
private int life;//命
private int doubleFire;//火力值
private BufferedImage[] images;//英雄机的图片数组
private int index;//图片下标,协助图片切换
public Hero(){
image=ShootGame.hero0;//英雄机的图片
width=image.getWidth();//英雄机的宽
height=image.getHeight();//英雄机的高
//英雄机刚开始出现位置
x=150;
y=400;
life=3;//初始的命数为3
doubleFire=0;//初始的火力值为0(即:单倍火力)
images=new BufferedImage[]{ShootGame.hero0,ShootGame.hero1};//放两张英雄机图片
index=0;//图片下标默认从0开始
}
//英雄机发射子弹
public Bullet[] shoot() {
int xStep=this.width/4;//英雄机宽度的1/4
if(doubleFire>0){//双倍火力
Bullet[] bullets=new Bullet[2];
bullets[0]=new Bullet(this.x+1*xStep,this.y-20);//x:英雄机的x+1/4英雄机的宽  y:英雄机的y-20
bullets[1]=new Bullet(this.x+3*xStep,this.y-20);//x:英雄机的x+3/4英雄机的宽  y:英雄机的y-20
doubleFire -=2;//发射一次双倍火力则火力值减2
return bullets;//返回两个子弹
}else{//单倍子弹
Bullet[] bullets=new Bullet[1];//子弹数组,只有一个元素
bullets[0]=new Bullet(this.x+2*xStep,this.y-20);//x:英雄机的x+2/4英雄机的宽  y:英雄机的y-20
return bullets;//返回一个子弹
}

}
public void moveTo(int x, int y) {
//从英雄机的中心点移到英雄机的左上角
this.x=x-this.width/2;//鼠标的x-1/2英雄机的宽
this.y=y-this.height/2;//鼠标的y-1/2英雄机的高

}
@Override
public void step() {//10毫秒执行一次
image=images[index++/10%images.length];//降速10倍  100毫秒执行一次
/*
* 10 0%2 0 hero0
* 20 1%2 1 hero1
* 30 2%2 0 hero0
* 40 3%2 1 hero1
* 50 4%2 0 hero0
* 60 5%2 1 hero1
*/

/*
* 10  0/10%2  0 hero0
* 20  1/10%2  0 hero0
* 30  2/10%2  0 hero0
* 40  3/10%2  0 hero0
* 50  4/10%2  0 hero0
* 60  5/10%2  0 hero0
* 70  6/10%2  0 hero0
* 80  7/10%2  0 hero0
* 90  8/10%2  0 hero0
* 100 9/10%2  0 hero0
* 110 10/10%2 1 hero1
* 120 11/10%2 1 hero1
*/
}
@Override
public boolean outOfBounds() {
// TODO Auto-generated method stub
return false;
}
public void addLife() {
life++;//命数加1
}
public void addDoubleFire() {
doubleFire +=40;//火力值加40
}
public int getLife() {
// TODO Auto-generated method stub
return life;
}
public void setDoubleFire(int doubleFire) {//int doubleFire=0;
this.doubleFire=doubleFire;//火力值归0 单倍火力

}
//英雄机减命
public void subtractLife() {
life--;

}
public boolean hit(FlyingObject f) {//f代表小敌机或者小蜜蜂
int x1=f.x-this.width/2;//x1:敌人的x-1/2英雄机的宽
int x2=f.x+f.width+this.width/2;//x2:敌人的x+敌人的宽+1/2英雄机的宽
int y1=f.y-this.height/2;//y1:敌人的y-1/2英雄机的高
int y2=f.y+f.height+this.height/2;//y2:敌人的y+敌人的高+1/2英雄机的高
//英雄机的中心点
int x=this.x+this.width/2;//英雄机的x+1/2英雄机的宽
int y=this.y+this.height/2;//英雄机的y+1/2英雄机的高

return x>x1&&x &&
  y>y1&&y }

}

ShootGame类