飞机大战第一天
1.抽象类:飞行物
public abstract class FlyingObject{ protected BufferedImage image; protected int width; //宽 protected int height; //高 protected int x; //x坐标
protected int y;
//y坐标}
2.接口:敌人
public interface Enemy { /** 得分 */ public int getScore(); }
3.接口:奖励
/** 奖励 */ public interface Award { public int DOUBLE_FIRE = 0; //火力值 public int LIFE = 1; //命 /** 获取奖励类型(上面的0或1) */ public int getType(); }
4.敌机,飞行物,也是敌人
public class Airplane extends FlyingObject implements Enemy { 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:负的敌机的高 } /** 重写getScore()得分 */ public int getScore(){ return 5; //打掉一个敌机得5分 }}
5.小蜜蜂:飞行物同时是也是奖励
/** 小蜜蜂: 是飞行物, */ public class Bee extends FlyingObject implements Award { private int xSpeed = 1; //x坐标移动速度 private int ySpeed = 2; //y坐标移动速度 private int awardType; //奖励的类型(0或1) /** 构造方法 */ public Bee(){ image = ShootGame.bee; //图片 width = image.getWidth(); //宽 height = image.getHeight(); //高 Random rand = new Random(); //随机数对象 x = rand.nextInt(ShootGame.WIDTH-this.width); //x:0到(窗口宽-小蜜蜂宽)之间的随机数 y = -this.height; //y:负的小蜜蜂的高 awardType = rand.nextInt(2); //0和1之间的随机数 } /** 获取奖励类型 */ public int getType(){ return awardType; //返回奖励类型 } }
6.子弹:同样也是飞行物
/** 子弹: 是飞行物 */ 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; //x:随英雄机 this.y = y; //y:随英雄机 }
7.英雄机:飞行物
/** 英雄机: 是飞行物 */ public class Hero extends FlyingObject { private int doubleFire; //火力值 private int life; //命 private BufferedImage[] images; //可切换的图片数组 private int index; //协助图片切换 /** 构造方法 */ public Hero(){ image = ShootGame.hero0; //图片 width = image.getWidth(); //宽 height = image.getHeight(); //高 x = 150; //x:固定的值 y = 400; //y:固定的值 doubleFire = 10000; //默认为0(单倍火力) life = 3; //默认3条命 images = new BufferedImage[]{ShootGame.hero0,ShootGame.hero1}; //两张图片切换 index = 0; //协助图片切换 } }
8.主类:窗口
//主窗口类 public class ShootGame extends JPanel { public static final int WIDTH = 400; //窗口宽 public static final int HEIGHT = 654; //窗口高 public static BufferedImage background; //背景图 public static BufferedImage start; //启动图 public static BufferedImage pause; //暂停图 public static BufferedImage gameover; //游戏结束图 public static BufferedImage airplane; //敌机 public static BufferedImage bee; //小蜜蜂 public static BufferedImage bullet; //子弹 public static BufferedImage hero0; //英雄机0 public static BufferedImage hero1; //英雄机1 static{ //初始化静态图片 try{ background = ImageIO.read(ShootGame.class.getResource("background.png")); start = ImageIO.read(ShootGame.class.getResource("start.png")); pause = ImageIO.read(ShootGame.class.getResource("pause.png")); gameover = ImageIO.read(ShootGame.class.getResource("gameover.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")); hero1 = ImageIO.read(ShootGame.class.getResource("hero1.png")); }catch(Exception e){ e.printStackTrace(); } } private Hero hero = new Hero(); //一个英雄机 private FlyingObject[] flyings = {}; //一堆敌人(敌机+小蜜蜂) private Bullet[] bullets = {}; //一堆子弹 public static final int START = 0; //启动状态 public static final int RUNNING = 1; //运行状态 public static final int PAUSE = 2; //暂停状态 public static final int GAME_OVER = 3; //游戏结束状态 private int state = START; //当前状态(默认启动状态) public static void main(String[] args) { JFrame frame = new JFrame("Fly"); //创建窗口对象 ShootGame game = new ShootGame(); //创建面板对象 frame.add(game); //将面板添加到窗口中 frame.setSize(WIDTH, HEIGHT); //设置窗口大小 frame.setAlwaysOnTop(true); //设置总是在最上面 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置窗口默认关闭操作(关闭窗口时退出程序) frame.setLocationRelativeTo(null); //设置居中显示 frame.setVisible(true); //1)设置窗口可见 2)尽快调用paint()方法 game.action(); //启动程序的执行 } /** 重写paint() g:画笔*/ public void paint(Graphics g){ g.drawImage(background,0,0,null); //画背景图 paintHero(g); //画英雄机对象 paintFlyingObjects(g); //画敌人(敌机+小蜜蜂)对象 paintBullets(g); //画子弹对象 paintScoreAndLife(g); //画分和画命 paintState(g); //画状态 } /** 画英雄机对象 */ public void paintHero(Graphics g){ g.drawImage(hero.image,hero.x,hero.y,null); //画英雄机对象 } /** 画敌人(敌机+小蜜蜂)对象 */ public void paintFlyingObjects(Graphics g){ for(int i=0;i<flyings.length;i++){ //遍历所有敌人(敌机+小蜜蜂) FlyingObject f = flyings[i]; //获取每一个敌人(敌机+小蜜蜂) g.drawImage(f.image,f.x,f.y,null); //画敌人(敌机+小蜜蜂)对象 } } /** 画子弹对象 */ public 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); //画子弹对象 } } }
9.敌人入场的实现步骤:
1)main(){ game.action(); } 2)action(){ ... run(){ //10毫秒定时执行 enteredAction(); //敌人入场 repaint(); } } 3)int index = 0; enteredAction(){ //10毫秒 index++; if(index%40==0){ //40*10毫秒 FlyingObject one = nextOne(); //创建一个敌人对象 flyings = Arrays.copyOf(flyings,flyings.length+1); //扩容 flyings[flyings.length-1] = one; //将敌人对象添加到敌人数组中 } } 4)nextOne(){ 生成0到19间的随机数 为0时return new Bee(); 否则return new Airplane(); }