FlyingObject类

//飞行物是airplane,bee,bullet,hero的父类
public class FlyingObject {
protected BufferedImage image;//图片
protected int width;//图片的宽
protected int height;//图片的高
protected int x;//图片在面板中显示的x坐标
protected int y;//图片在面板中显示的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 Award {
//接口中定义的属性为常量
public static final int DOUBLE_FIRE=0;//火力值   
public static final int LIFE=1;//命
//获取奖励机制 返回0代表火力值,返回1代表命
public abstract int getType();//抽象方法
}

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;
}

}

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)
}

}

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;
}
}

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开始
}
}

ShootGame类

public class ShootGame extends JPanel{//ShootGame is a JPanel  ShootGame是一个面板
public static final int WIDTH=400;//窗口宽400,static final修饰表示常量
public static final int HEIGHT=654;//窗口高654,static final修饰表示常量
public static BufferedImage background;//BufferedImage存储图片  背景图
public static BufferedImage start;//开始图
public static BufferedImage gameover;//结束图
public static BufferedImage pause;//暂停图
public static BufferedImage airplane;//小敌机
public static BufferedImage bee;//小蜜蜂
public static BufferedImage bullet;//子弹
public static BufferedImage hero0;//英雄机0
public static BufferedImage hero1;//英雄机1

private Hero hero=new Hero();//创建一个英雄机
private FlyingObject[] flyings={};//存储小蜜蜂或者小敌机的数组
private Bullet[] bullets={};//存储子弹的数组

static{//静态代码块,用于初始化图片资源
try {
background=ImageIO.read(ShootGame.class.getResource("background.png"));//读取背景图
start=ImageIO.read(ShootGame.class.getResource("start.png"));//读取开始图片
gameover=ImageIO.read(ShootGame.class.getResource("gameover.png"));//读取结束图
pause=ImageIO.read(ShootGame.class.getResource("pause.png"));//读取暂停图
airplane=ImageIO.read(ShootGame.class.getResource("airplane.png"));//读取小敌机图
bee=ImageIO.read(ShootGame.class.getResource("bee.png"));//读取小蜜蜂图
bullet=ImageIO.read(ShootGame.class.getResource("bullet.png"));//读取子弹图
hero0=ImageIO.read(ShootGame.class.getResource("hero0.png"));//读取英雄机0图
hero1=ImageIO.read(ShootGame.class.getResource("hero1.png"));//读取英雄机1图
} catch (IOException e) {
e.printStackTrace();
}
}

public ShootGame(){
flyings=new FlyingObject[2];
flyings[0]=new Airplane();//0位置放一个小敌机
flyings[1]=new Bee();//1位置放一个小蜜蜂
bullets=new Bullet[1];
bullets[0]=new Bullet(80,100);//0位置放一个子弹,坐标为(80,100)
}
//在ShootGame里画图片
@Override
public void paint(Graphics g) {//g理解画笔
g.drawImage(background,0,0,null);//通过g画背景图   background背景图  0:x坐标为0  0:y坐标为0  null空值无影响
paintHero(g);//画英雄机
paintFlyingEnemy(g);//画敌人(小敌机和小蜜蜂)
paintBullets(g);//画子弹
}
//画英雄机
private void paintHero(Graphics g) {//g是画笔
g.drawImage(hero.image, hero.x, hero.y,null);//画英雄机对象
}
//画敌人(小敌机和小蜜蜂)
private void paintFlyingEnemy(Graphics g) {
for(int i=0;i<flyings.length;i++){//遍历flyings数组里面的每一个元素(即是小蜜蜂或小敌机)
FlyingObject f=flyings[i];//获取每一个元素(敌人)
g.drawImage(f.image,f.x,f.y,null);//画每一个敌人
}
}
//画子弹
private void paintBullets(Graphics g) {
for(int i=0;i<bullets.length;i++){//遍历子弹数组
Bullet b=bullets[i];//获取每一个子弹
g.drawImage(b.image, b.x, b.y, null);//画每一个子弹
}
}
public static void main(String[] args) {
JFrame frame=new JFrame("Fly");//创建窗体(相框)并设置标题Fly
// JPanel panel=new JPanel();//底板(相片)
// panel.setBackground(Color.RED);//设置底板(相片)背景色为红色
// frame.add(panel);//将底板(相片)添加到窗体(相框)中
ShootGame game=new ShootGame();//底板(相片)
frame.add(game);//将底板(相片)添加到窗体(相框)中
frame.setSize(WIDTH,HEIGHT);//设置窗体大小宽400,高654
frame.setLocationRelativeTo(null);//设置相对位置为居中的
frame.setAlwaysOnTop(true);//设置总是在最上面
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置默认关闭操作
frame.setVisible(true);//设置可见,即显示窗体,尽快调用paint方法
}


}