有人知道怎么加飞机大战的背景音乐吗??

package xiaoxueqi;
import java.util.Random;
public class Airplane extends FlyingObject implements Enemy{
    private int speed = 4;   
    private int life;

    public Airplane(){
        life=3;
        this.image = ShootGame.airplane;
        width = image.getWidth();
        height = image.getHeight();
        y = -height;          
        Random rand = new Random();
        x = rand.nextInt(ShootGame.WIDTH - width);
    }

      public int getScore() {  
        return 10;
    }

       public  boolean outOfBounds() {   
        return y>ShootGame.HEIGHT;
    }

       public void step() {   
        y += speed;
    }


}

英雄机

import java.awt.image.BufferedImage;

public class Hero extends FlyingObject{
private BufferedImage[] images = {}; //英雄机图片数组
private int index = 0; //切换图片
private int doubleFire; //双倍火力
private int tripleFire;
private int life; //生命

public Hero(){
    life = 10;   //初始3条命
    doubleFire = 0; 
   tripleFire=0;//火力为0
    images = new BufferedImage[]{ShootGame.hero0, ShootGame.hero1}; //两张图片切换
    image = ShootGame.hero0;   
    width = image.getWidth();
    height = image.getHeight();
    x = 150;
    y = 400;
}

   public int isDoubleFire() {  //获取双倍火力
    return doubleFire;
}
   public void setDoubleFire(int doubleFire) {
       this.doubleFire = doubleFire;
   }
public int isTriple_Fire() {
    return tripleFire;
}
public void setTripleFire(int tripleFire) {//设置双倍火力
    this.tripleFire = tripleFire;
}
   public void addDoubleFire(){   //火力增加40
    doubleFire+=40;
}
   public void addTripleFire() {
       tripleFire+=60;
   }
   public void addLife(){  //增命
    life++;
}
 public void subtractLife(){   //减命
    life--;
}
 public int getLife(){  //得到命
    return life;
}
   public void moveTo(int x,int y){     //
    this.x = x - width/2;
    this.y = y - height/2;

}

public boolean outOfBounds() {
    return false;  
}

public Bullet[] shoot(){   
    int xStep = width/4;      
    int yStep = 20;  

    if(00) {
         Bullet[] bullets = new Bullet[6];
         bullets[0] = new Bullet(x+1*xStep,y-yStep);  //y-yStep
        bullets[1] = new Bullet(x+2*xStep,y-yStep);
        bullets[2] = new Bullet(x+3*xStep,y-yStep);
        bullets[3] = new Bullet(x+4*xStep,y-yStep);
        bullets[4] = new Bullet(x-1*xStep,y-yStep);
        bullets[5] = new Bullet(x,y-yStep);
        return bullets;
    }


    else{      
        Bullet[] bullets = new Bullet[1];
        bullets[0] = new Bullet(x+xStep,y-yStep);  
        return bullets;
    }
}

public void step() {
    if(images.length>0){
        image = images[index++/10%images.length];  //切换图片hero0,hero1
    }
}

  /** 碰撞算法 */
public boolean hit(FlyingObject other){

    int x1 = other.x - this.width/2;                 //x1:敌人的x-1/2英雄机的宽
    int x2 = other.x + this.width/2 + other.width;   //敌人的x+敌人的宽+1/2英雄机的宽
    int y1 = other.y - this.height/2;                //y1:敌人的y-1/2英雄机的高
    int y2 = other.y + this.height/2 + other.height; //y2:敌人的y+敌人的高+1/2英雄机的高

    int herox = this.x + this.width/2;               //x:英雄机的x+1/2英雄机的宽
    int heroy = this.y + this.height/2;              //y:英雄机的y+1/2英雄机的高

    return herox>x1 && heroxy1 && heroy

}

接口
public interface Enemy {
int getScore();

}

子弹
public class Bullet extends FlyingObject{
private int speed = 8; //移动的速度

    public Bullet(int x,int y){
        this.x = x;//子弹的x,y坐标随英雄机
        this.y = y;
        this.image = ShootGame.bullet;
    }

     public void step(){   
        y-=speed;
    }

    public boolean outOfBounds() {
        return y<-height;//子弹的y<=负的子弹的高,即为越界了
    }

}

奖励
public interface Award {
int DOUBLE_FIRE = 0;//双倍火力
int TRIPLE_FIRE=2;
int LIFE = 1; //1条命
int getType();
}

蜜蜂
import java.util.Random;
public class Bee extends FlyingObject implements Award{
private int xSpeed = 4; //x坐标移动速度
private int ySpeed = 4; //y坐标移动速度
private int awardType; //奖励类型

    public Bee(){//蜜蜂的图片,宽,高
        this.image = ShootGame.bee;
        width = image.getWidth();
        height = image.getHeight();
        y = -height;
        Random rand = new Random();
        x = rand.nextInt(ShootGame.WIDTH - width);
        awardType = rand.nextInt(3);   //随机数 0或1,2,
    }

        public int getType(){
        return awardType;
    }

     public boolean outOfBounds() {
        return y>ShootGame.HEIGHT;//小蜜蜂的y>=窗口的高,即为越界了
    }
      public void step() {      
        x += xSpeed;
        y += ySpeed;
        if(x > ShootGame.WIDTH-width){  //若x>=(窗口宽-蜜蜂宽),则xSpeed为-1,加-1即为向左
            xSpeed = -1;
        }
        if(x <= 0){ //若x<=0,则xSpeed为1,加1即为向右
            xSpeed = 1;
        }
      } 

}

飞行物
import java.awt.image.BufferedImage;
public abstract class FlyingObject {
protected int x; //x坐标
protected int y; //y坐标
protected int width; //宽
protected int height; //高
protected BufferedImage image; //图片
public int getX() { //X坐标的访问器
return x;
}
public void setX(int x) { //改变X坐标
this.x = x;
}
public int getY() { //Y坐标的访问器
return y;
}
public void setY(int y) { //改变Y坐标
this.y = y;
}
public int getWidth() { //宽的访问器
return width;
}
public void setWidth(int width) { //宽度
this.width = width;
}
public int getHeight() { //高的访问器
return height;
}
public void setHeight(int height) { //高度
this.height = height;
}
public BufferedImage getImage() {
return image;
}
public void setImage(BufferedImage image) { //背景图
this.image = image;
}
public abstract boolean outOfBounds(); //检查是否出界

public abstract void step();  //飞机移动
public boolean shootBy(Bullet bullet){  //检查飞机是否被击中
    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 = bullet.x;  //子弹的x坐标         
    int y = bullet.y;  //子弹的y坐标
    return x>=x1&&x=y1&&y

}

主函数
import java.awt.Font;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ShootGame extends JPanel {
public static void main(String[] args) {
JFrame frame = new JFrame(“Fly”);
ShootGame game = new ShootGame(); // 面板对象
frame.add(game); // 将面板添加到JFrame中
frame.setSize(WIDTH, HEIGHT); // 设置大小
frame.setAlwaysOnTop(true); // 设置其总在最上
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 默认关闭操作
frame.setIconImage(new ImageIcon(“images/icon.jpg”).getImage()); // 设置窗体的图标
frame.setLocationRelativeTo(null); // 设置窗体初始位置
frame.setVisible(true); // 尽快调用paint
game.action(); // 启动执行
}
public static final int WIDTH = 400; // 面板宽
public static final int HEIGHT = 684; // 面板高

private int state;
private static final int START = 0;//启动状态
private static final int RUNNING = 1;//运行状态
private static final int PAUSE = 2;//暂停状态
private static final int GAME_OVER = 3;//游戏结束

private int score = 0; // 得分
private Timer timer; // 定时器
private int intervel = 1000 / 100; // 时间间隔(毫秒)

public static BufferedImage background;//背景图
public static BufferedImage start;//启动图
public static BufferedImage airplane;//敌机图
public static BufferedImage bee;//蜜蜂图
public static BufferedImage bullet;//子弹
public static BufferedImage hero0;//英雄机
public static BufferedImage hero1;//英雄机
public static BufferedImage pause;//暂停图
public static BufferedImage gameover;//结束图

private FlyingObject[] flyings = {}; //一堆敌人
private Bullet[] bullets = {}; //一堆子弹
private Hero hero = new Hero(); //一个英雄机

static { 
    try {
        background = ImageIO.read(ShootGame.class
                .getResource("background.png"));
        start = ImageIO.read(ShootGame.class.getResource("start.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"));
        pause = ImageIO.read(ShootGame.class.getResource("pause.png"));
        gameover = ImageIO
                .read(ShootGame.class.getResource("gameover.png"));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/** 画 */
@Override
public void paint(Graphics g) {
    g.drawImage(background, 0, 0, null); // 画背景图

    paintHero(g); //画英雄机对象
    paintBullets(g);//画敌机对象
    paintFlyingObjects(g); //画子弹对象
    paintScore(g); //画分和命
    paintState(g); //画状态
}

public void paintHero(Graphics g) { //画英雄对象
    g.drawImage(hero.getImage(), hero.getX(), hero.getY(), null);
}

public void paintBullets(Graphics g) {
    for (int i = 0; i < bullets.length; i++) {//遍历所有子弹
        Bullet b = bullets[i];//获取每一个子弹
        g.drawImage(b.getImage(), b.getX() - b.getWidth() / 2, b.getY(),//画子弹
                null);
    }
}

public void paintFlyingObjects(Graphics g) {
    for (int i = 0; i < flyings.length; i++) {  //遍历所以敌人
        FlyingObject f = flyings[i];//获取每一个敌人
        g.drawImage(f.getImage(), f.getX(), f.getY(), null);//画敌人对象
    }
}

public void paintScore(Graphics g) {
    int x = 10; // x坐标
    int y = 25; // y坐标
    Font font = new Font(Font.SANS_SERIF, Font.BOLD, 14);  
    g.setColor(new Color(0x3A3B3B));
    g.setFont(font); // 设置字体
    g.drawString("SCORE:" + score, x, y);  
    y += 20; // y坐标增20
    g.drawString("LIFE:" + hero.getLife(), x, y);  
}

public void paintState(Graphics g) {
    switch (state) {//根据当前状态的不同画不同的图
    case START: // 启动状态
        g.drawImage(start, 0, 0, null);
        break;
    case PAUSE: //暂停状态
        g.drawImage(pause, 0, 0, null);
        break;
    case GAME_OVER: //游戏结束状态
        g.drawImage(gameover, 0, 0, null);
        break;
    }
}

/** 启动执行代码 */
public void action() {
       MouseAdapter l = new MouseAdapter() {// 鼠标监听事件
        @Override
        public void mouseMoved(MouseEvent e) { 
            if (state == RUNNING) {   //运行状态执行
                int x = e.getX();    //获取鼠标的x坐标
                int y = e.getY();    //获取鼠标的y坐标
                hero.moveTo(x, y);  //英雄机随着鼠标移动
            }
        }

        @Override
        public void mouseEntered(MouseEvent e) { // 鼠标进入
            if (state == PAUSE) { // 暂停状态下运行
                state = RUNNING;//修改为运行状态
            }
        }

        @Override
        public void mouseExited(MouseEvent e) { // 鼠标退出
            if (state != GAME_OVER&&state!=START) { // 游戏未结束,则设置其为暂停
                state = PAUSE;
            }
        }

        @Override
        public void mouseClicked(MouseEvent e) { // 鼠标点击
            switch (state) {//根据当前状态做不同的操作
            case START://启动状态时
                state = RUNNING; //修改为运行状态
                break;
            case GAME_OVER:  //游戏结束状态时
                flyings = new FlyingObject[0];   //清理现场
                bullets = new Bullet[0];  
                hero = new Hero();  
                score = 0;    
                state = START; //修改为启动状态
                break;
            }
        }
    };
    this.addMouseListener(l); // 处理鼠标点击操作
    this.addMouseMotionListener(l); // 处理鼠标滑动操作

    timer = new Timer(); // 定时器
    int intervel = 10;
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            if (state == RUNNING) {    //运行状态时执行
                enterAction();    //敌人(敌机+小蜜蜂)入场
                stepAction();  //飞行物(敌机+小蜜蜂+子弹+英雄机)走一步
                shootAction();     //子弹入场(英雄机发射子弹)
                bangAction();    //子弹与敌人(敌机+小蜜蜂)的碰撞
                outOfBoundsAction();  //删除越界的飞行物(敌机+小蜜蜂+子弹)
                checkGameOverAction();   //判断游戏结束
            }
            repaint(); // 重绘,调用paint()方法
        }

    }, intervel, intervel);//定时做以上的程序
}

int flyEnteredIndex = 0; // 飞行物入场计数

public void enterAction() { //10毫秒走一次
    flyEnteredIndex++;//每10毫秒增1
    if (flyEnteredIndex % 40 == 0) { //400(10*40)毫秒走一次
        FlyingObject obj = nextOne(); //获取敌人(敌机+小蜜蜂)对象
        flyings = Arrays.copyOf(flyings, flyings.length + 1);//扩容
        flyings[flyings.length - 1] = obj;//将敌人添加到最后一个元素上
    }
}

public void stepAction() {
    for (int i = 0; i < flyings.length; i++) { //遍历所有敌人
        FlyingObject f = flyings[i];
        f.step();//敌人走一步
    }

    for (int i = 0; i < bullets.length; i++) { 
        Bullet b = bullets[i];
        b.step();//子弹走一步
    }
    hero.step(); //英雄机走一步
}

public void flyingStepAction() {
    for (int i = 0; i < flyings.length; i++) {
        FlyingObject f = flyings[i];
        f.step();//飞行物走一步
    }
}

int shootIndex = 0; // 射击计数

public void shootAction() {
    shootIndex++;//每10毫秒增1
    if (shootIndex % 30== 0) {  
        Bullet[] bs = hero.shoot(); 
        bullets = Arrays.copyOf(bullets, bullets.length + bs.length); //扩容(有几个元素就扩大几个容量)
        System.arraycopy(bs, 0, bullets, bullets.length - bs.length,
                bs.length); //数组的追加
    }
}

public void bangAction() {
    for (int i = 0; i < bullets.length; i++) {//遍历所以子弹 
        Bullet b = bullets[i];
        bang(b); //实现一个子弹与所有敌人(敌机+小蜜蜂)的碰撞
    }
}

public void outOfBoundsAction() {
    int index = 0; //1)不越界敌人数组下标  2)不越界敌人个数
    FlyingObject[] flyingLives = new FlyingObject[flyings.length];  //不越界敌人数组
    for (int i = 0; i < flyings.length; i++) {//遍历所有敌人
        FlyingObject f = flyings[i];//获取每一个敌人
        if (!f.outOfBounds()) { //不越界
            flyingLives[index++] = f;  //将不越界敌人对象添加到不越界敌人数组中
        }
    }
    flyings = Arrays.copyOf(flyingLives, index); // 将不越界的飞行物都留着

    index = 0; // 索引重置为0
    Bullet[] bulletLives = new Bullet[bullets.length];
    for (int i = 0; i < bullets.length; i++) {
        Bullet b = bullets[i];
        if (!b.outOfBounds()) {
            bulletLives[index++] = b;//将不越界子弹添加到数组
        }
    }
    bullets = Arrays.copyOf(bulletLives, index); // 将不越界的子弹留着
}

/** 检查游戏结束 */
public void checkGameOverAction() {
    if (isGameOver()) {//游戏结束
        state = GAME_OVER; 
    }
}

public boolean isGameOver() {

    for (int i = 0; i < flyings.length; i++) {
        int index = -1;
        FlyingObject obj = flyings[i];
        if (hero.hit(obj)) {  //英雄机被敌机撞上
            hero.subtractLife();  //减命
            hero.setDoubleFire(0);  //火力初始化
            index = i; 
        }
        if (index != -1) {
            FlyingObject t = flyings[index];//交换被撞的敌人与数组中的最后一个元素
            flyings[index] = flyings[flyings.length - 1];
            flyings[flyings.length - 1] = t; 

            flyings = Arrays.copyOf(flyings, flyings.length - 1);  
        }
    }

    return hero.getLife() <= 0;
}


public void bang(Bullet bullet) {
    int index = -1; 
    for (int i = 0; i < flyings.length; i++) {
        FlyingObject obj = flyings[i];
        if (obj.shootBy(bullet)) {  //如果撞上了
            index = i; //记录被撞敌人的下标
            break;
        }
    }
    if (index != -1) {  

        FlyingObject one = flyings[index]; // 记录被击中的飞行物

        FlyingObject temp = flyings[index]; // 被击中的飞行物与最后一个飞行物交换
        flyings[index] = flyings[flyings.length - 1];
        flyings[flyings.length - 1] = temp;

        flyings = Arrays.copyOf(flyings, flyings.length - 1); //数组缩容
        if (one instanceof Enemy) { //如果装上的是敌人

            Enemy e = (Enemy) one;//制强转为敌人
            score += e.getScore(); //  得分
        } else if (one instanceof Award) {  //如果撞上的是奖励
            Award a = (Award) one;//强制转为奖励
            int type = a.getType(); //获取奖励类型 
            switch (type) {
            case Award.DOUBLE_FIRE:
                hero.addDoubleFire();  //加火力
                break;
            case Award.LIFE:
                hero.addLife(); //加生命
                break;
            case Award.TRIPLE_FIRE:
                hero.setDoubleFire(0);
                hero.addTripleFire();
                break;
            }
        }
    }
}

public static FlyingObject nextOne() {
    Random random = new Random();
    int type = random.nextInt(20); // [0,20)
    if (type == 0) {
        return new Bee();//随机数为0时产生蜜蜂
    } else {
        return new Airplane();
    }
}

}

你可能感兴趣的:(有人知道怎么加飞机大战的背景音乐吗??)