java新手写飞机大战气泡版(坤坤大战)

105 阅读11分钟

飞机大战气泡版(坤坤大战)初学者版

  1. 初学java学习做的
  2. w,s,a,d上下左右移动
  3. g切换攻击模式,r全屏攻击
  4. 实现了飞机的移动
  5. 实现了气泡随机出现,撞击边缘反弹的效果
  6. 实现气泡分裂效果
  7. 实现子弹碰撞敌方单位效果
  8. 实现飞机碰撞金币和敌方单位的效果

image.png

image.png

package com.itheima.pojo;

import com.hzheima.util.ImageUtil;
import com.itheima.AppConfig;

import java.awt.*;
import java.util.ArrayList;
import java.util.Random;

public class Ball extends FlyingObject {
    Random rand = new Random();
    private int xSpeed;
    private int ySpeed;
    private int startLoc;//0是左边出来,1是右边
    private boolean startDown;
    private int blood;
    private int initialBlood;
    private int isBossBall;

    public int getxSpeed() {
        return xSpeed;
    }

    public void setxSpeed(int xSpeed) {
        this.xSpeed = xSpeed;
    }

    public int getySpeed() {
        return ySpeed;
    }

    public void setySpeed(int ySpeed) {
        this.ySpeed = ySpeed;
    }

    public int getStartLoc() {
        return startLoc;
    }

    public void setStartLoc(int startLoc) {
        this.startLoc = startLoc;
    }

    public boolean isStartDown() {
        return startDown;
    }

    public void setStartDown(boolean startDown) {
        this.startDown = startDown;
    }

    public int getBlood() {
        return blood;
    }

    public void setBlood(int blood) {
        this.blood = blood;
    }

    public int getInitialBlood() {
        return initialBlood;
    }

    public void setInitialBlood(int initialBlood) {
        this.initialBlood = initialBlood;
    }

    public int getIsBossBall() {
        return isBossBall;
    }

    public void setIsBossBall(int isBossBall) {
        this.isBossBall = isBossBall;
    }

    public Ball(int x, int y, int xSpeed, int ySpeed, int blood) {
        super.x = x;
        super.y = y;
        this.xSpeed = xSpeed;
        this.ySpeed = ySpeed;
        this.initialBlood = blood;
        this.blood = initialBlood;
        int i = rand.nextInt(15);
        this.img = ImageUtil.getImg("c" + i + ".png");
        this.width = img.getWidth();
        this.height = img.getHeight();
        startDown = true;
        isBossBall = 0;
    }

    public Ball() {
        int i = rand.nextInt(15);
        blood = i + 1;
        img = ImageUtil.getImg("c" + i + ".png");
        startLoc = rand.nextInt(2);
        width = img.getWidth();
        height = img.getHeight();
        this.initialBlood = blood;
        this.blood = initialBlood;
        if (startLoc == AppConfig.LEFT) {
            x = -width;
            xSpeed = 2;
        } else {
            x = AppConfig.APP_WIDTH;
            xSpeed = -2;
        }
        y = 100;
        ySpeed = 2;
        isBossBall = 0;
    }

    public void move() {
        this.x += xSpeed;
        if (this.startLoc == AppConfig.LEFT) {
            if (this.x > 250) {
                startDown = true;
            }
        } else if (this.startLoc == AppConfig.RIGHT) {
            if (this.x < AppConfig.APP_WIDTH - 250) {
                startDown = true;
            }
        }
        if (startDown) {
            y += ySpeed;
        }
        //撞左墙
        if (xSpeed < 0 && x <= 0) {
            xSpeed = -xSpeed;
        }
        //撞右墙
        if (xSpeed > 0 && x >= AppConfig.APP_WIDTH - width - 10) {
            xSpeed = -xSpeed;
        }
        //撞上墙
        if (ySpeed < 0 && y <= 0) {
            ySpeed = -ySpeed;
        }
        //撞下墙
        if (ySpeed > 0 && y >= AppConfig.APP_HEIGHT - height - 35) {
            ySpeed = -ySpeed;
        }
    }

    public void move1() {
        this.x += xSpeed;
        this.y += ySpeed;
        //撞左墙
        if (xSpeed < 0 && x <= 0) {
            xSpeed = -xSpeed;
        }
        //撞右墙
        if (xSpeed > 0 && x >= AppConfig.APP_WIDTH - width - 10) {
            xSpeed = -xSpeed;
        }
    }

    public boolean shootBy(Bullet bullet) {
        boolean flag = byShoot(bullet);
        if (flag) {
            //被击中
            this.blood--;
            startDown = true;
        }
        return flag;
    }

    @Override
    public void draw(Graphics g) {
        super.draw(g);
        //绘制血量
        g.setColor(Color.WHITE);
        g.setFont(new Font("楷体", Font.BOLD, 30));
        int dx = blood >= 10 ? 16 : 10;
        g.drawString(blood + "", x + width / 2 - dx, y + height / 2 + 10);
    }

    public ArrayList<Ball> split() {
        ArrayList<Ball> list = new ArrayList<>();
        int rxSpeed1 = rand.nextInt(11) - 5;
        Ball ball1 = new Ball(this.x, this.y, rxSpeed1, this.ySpeed, this.blood);
        int rxSpeed2 = rand.nextInt(11) - 5;
        Ball ball2 = new Ball(this.x, this.y, rxSpeed2, this.ySpeed, this.blood);
        list.add(ball1);
        list.add(ball2);
        return list;
    }

    @Override
    public String toString() {
        return "Ball{" +
                "rand=" + rand +
                ", xSpeed=" + xSpeed +
                ", ySpeed=" + ySpeed +
                ", startLoc=" + startLoc +
                ", startDown=" + startDown +
                ", blood=" + blood +
                ", initialBlood=" + initialBlood +
                '}';
    }
}
package com.itheima.pojo;

import com.hzheima.util.ImageUtil;
import com.itheima.AppConfig;
import com.itheima.MusicUtil;

import java.awt.*;
import java.util.ArrayList;
import java.util.Random;

public class BallBoss extends FlyingObject {
    Random rand = new Random();
    private int xSpeed;
    private int ySpeed;
    private int blood;
    ArrayList<Ball> balls = new ArrayList<>();
    int count = 0;

    public int getxSpeed() {
        return xSpeed;
    }

    public void setxSpeed(int xSpeed) {
        this.xSpeed = xSpeed;
    }

    public int getySpeed() {
        return ySpeed;
    }

    public void setySpeed(int ySpeed) {
        this.ySpeed = ySpeed;
    }

    public int getBlood() {
        return blood;
    }

    public void setBlood(int blood) {
        this.blood = blood;
    }

    public ArrayList<Ball> getBalls() {
        return balls;
    }

    public void setBalls(ArrayList<Ball> balls) {
        this.balls = balls;
    }

    public BallBoss() {
        img = ImageUtil.getImg("kunkunji1.png");
        width = img.getWidth()/3;
        height = img.getHeight()/3;
        this.blood = 100;
        x = AppConfig.APP_WIDTH / 2 - width / 2;
        xSpeed = 0;
        y = -height;
        ySpeed = 3;
    }

    public void move() {
        y = y + ySpeed;
    }

    public void draw(Graphics g) {
            super.draw(g);
            //绘制血量
            g.setColor(Color.WHITE);
            g.setFont(new Font("楷体", Font.BOLD, 30));
            g.drawString(blood + "", x + width / 2 - 26, y + height / 2 + 75);
    }

    public void attack() {
        count++;
        if (count % 30 == 0 && y > 0) {
            Ball ball = new Ball();
            ball.setIsBossBall(1);
            ball.setImg(ImageUtil.getImg("egg.png"));
            ball.setX(x + width / 2 - 26);
            ball.setY(y + height / 2 + 10);
            ball.setBlood(2);
            ball.setInitialBlood(2);
            ball.setStartDown(true);
            balls.add(ball);
            MusicUtil.playMp3("kunkunhit.mp3");
            count = 0;
        }
        for (int i = balls.size() - 1; i >= 0; i--) {
            Ball ball1 = balls.get(i);
            ball1.move1();
            if (ball1.getY() > AppConfig.APP_HEIGHT) {
                balls.remove(ball1);
                i--;
            }
        }
    }

    //精英球被击中
    public boolean shootBy(Bullet bullet) {
        boolean flag = byShoot(bullet);
        if (flag) {
            //被击中
            this.blood--;
        }
        return flag;
    }

}
package com.itheima.pojo;

import com.hzheima.util.ImageUtil;

import java.awt.*;

public class Bullet extends FlyingObject{
    public Bullet(int px, int py) {
        this.img = ImageUtil.getImg("fire.png");
        this.x = px+3;
        this.y = py-47;
        this.width = img.getWidth();
        this.height = img.getHeight();
        this.speed = 10;
    }

    public Bullet() {
        this.img = img;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.speed = speed;
    }

    public void move(){
        this.y -= speed;
    }
    public void move1(){
        this.y -= speed;
        this.x += speed/4;
    }
    public void move2(){
        this.y -= speed;
        this.x -= speed/4;
    }
}
package com.itheima.pojo;

import com.hzheima.util.ImageUtil;
import com.itheima.AppConfig;

import java.awt.*;
import java.awt.image.BufferedImage;

public class FlyingObject {
    protected BufferedImage img;
    protected int x;
    protected int y;
    protected int width;
    protected int height;
    protected int speed;

    public BufferedImage getImg() {
        return img;
    }

    public void setImg(BufferedImage img) {
        this.img = img;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int 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 int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public void draw(Graphics g){
        //绘制
        g.drawImage(this.getImg(),this.getX(),this.getY(),this.getWidth(),this.getHeight(),null);
    }
    public boolean byShoot(FlyingObject obj){
        boolean flag = obj.y < this.y + this.height && obj.y + obj.height > this.y
                && obj.x + obj.width > this.x && obj.x < this.x + this.width;
        return flag;
    }
}
package com.itheima.pojo;


public class Level {
    int level;//当前是什么关卡
    int ballNum;//当前屏幕最多出现球的数量
    int ballKill;//当前关卡需要击杀的数量

    public Level() {
    }

    public Level(int level, int ballNum, int ballKill) {
        this.level = level;
        this.ballNum = ballNum;
        this.ballKill = ballKill;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public int getBallNum() {
        return ballNum;
    }

    public void setBallNum(int ballNum) {
        this.ballNum = ballNum;
    }

    public int getBallKill() {
        return ballKill;
    }

    public void setBallKill(int ballKill) {
        this.ballKill = ballKill;
    }
}
package com.itheima.pojo;

import com.hzheima.util.ImageUtil;
import com.itheima.AppConfig;

import java.util.Random;


public class Money extends FlyingObject {
    Random rand = new Random();
    private int xSpeed;
    private int ySpeed;

    public Money() {
        img = ImageUtil.getImg("money.png");
        width = img.getWidth()/2;
        height = img.getHeight()/2;
        x = rand.nextInt(AppConfig.APP_WIDTH-width+1);
        y = -height;
        xSpeed = 0;
        ySpeed = 5;
    }

    public void move() {
        this.y +=ySpeed;
        this.x +=xSpeed;
    }
}
package com.itheima.pojo;

import com.hzheima.util.ImageUtil;
import com.itheima.AppConfig;
import com.itheima.MusicUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;


public class Plane extends FlyingObject {
    private int xSpeed;//x轴方向移动速度
    private int ySpeed;//y轴方向移动速度
    private int upgrade;//射击方式
    //定义一个集合存子弹
    ArrayList<Bullet> Bullets = new ArrayList<>();
    ArrayList<Bullet> Bullets1 = new ArrayList<>();
    ArrayList<Bullet> Bullets2 = new ArrayList<>();
    int count = 0;
    int mag = 10;//生成子弹速度

    public Plane() {
        img = ImageUtil.getImg("hero.png");
        width = img.getWidth() / 4;
        height = img.getHeight() / 4;
        x = 200;
        y = 620;
        xSpeed = 10;
        ySpeed = 10;
        upgrade = 0;
    }

    public Plane(BufferedImage img, int x, int y, int width, int height) {
        this.img = img;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    public int getxSpeed() {
        return xSpeed;
    }

    public void setxSpeed(int xSpeed) {
        this.xSpeed = xSpeed;
    }

    public int getySpeed() {
        return ySpeed;
    }

    public void setySpeed(int ySpeed) {
        this.ySpeed = ySpeed;
    }

    public ArrayList<Bullet> getBullets() {
        return Bullets;
    }

    public void setBullets(ArrayList<Bullet> bullets) {
        Bullets = bullets;
    }

    public ArrayList<Bullet> getBullets1() {
        return Bullets1;
    }

    public void setBullets1(ArrayList<Bullet> bullets1) {
        Bullets1 = bullets1;
    }

    public ArrayList<Bullet> getBullets2() {
        return Bullets2;
    }

    public void setBullets2(ArrayList<Bullet> bullets2) {
        Bullets2 = bullets2;
    }

    public int getUpgrade() {
        return upgrade;
    }

    public void setUpgrade(int upgrade) {
        this.upgrade = upgrade;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getMag() {
        return mag;
    }

    public void setMag(int mag) {
        this.mag = mag;
    }

    public void moveToRight() {
        x = x + xSpeed;
        if (x > AppConfig.APP_WIDTH - this.width - 15) {
            x = AppConfig.APP_WIDTH - this.width - 15;
        }
    }

    public void moveToLeft() {
        x = x - xSpeed;
        if (x < 0) {
            x = 0;
        }
    }

    public void moveToUp() {
        y = y - ySpeed;
        if (y < AppConfig.APP_HEIGHT / 2) {
            y = AppConfig.APP_HEIGHT / 2;
        }
    }

    public void moveToDown() {
        y = y + ySpeed;
        if (y > AppConfig.APP_HEIGHT - this.height - 33) {
            y = AppConfig.APP_HEIGHT - this.height - 33;
        }
    }

    public void shoot() {
        count++;
        if (count % mag == 0) {
            switch (this.upgrade) {
                case 1:
                    Bullet bullet1 = new Bullet(this.x, this.y);
                    Bullets1.add(bullet1);
                    Bullet bullet2 = new Bullet(this.x, this.y);
                    Bullets2.add(bullet2);
                case 0:
                    Bullet bullet = new Bullet(this.x, this.y);
                    Bullets.add(bullet);
                default:
                    MusicUtil.playMp3("shoot.mp3");
            }
            count = 0;
        }
        //让子弹移动
        bulletMove(Bullets, 0);
        bulletMove(Bullets1, 1);
        bulletMove(Bullets2, 2);
    }

    private void bulletMove(ArrayList<Bullet> bullets, int num) {
        for (int i = 0; i < bullets.size(); i++) {
            Bullet bullet = bullets.get(i);
            switch (num) {
                case 0:
                    bullet.move();
                    break;
                case 1:
                    bullet.move1();
                    break;
                case 2:
                    bullet.move2();
                    break;
                default:
                    break;
            }
            if (bullet.getY() < -bullet.getHeight()) {
                bullets.remove(bullet);
                i--;
            }
        }
    }

    //被球撞了
    public boolean isCrashByBall(Ball ball) {
        boolean flag = byShoot(ball);
        if (flag) {
            //撞上了
            changeImg();
        }
        return flag;
    }
    //被金币撞了
    public boolean isCrashByMoney(Money money) {
        boolean flag = byShoot(money);
        return flag;
    }

    private void changeImg() {
        this.img = ImageUtil.getImg("boom.png");
    }
}
package com.itheima.ui;
import com.hzheima.game.GameJFrame;
import com.hzheima.util.ImageUtil;
import com.itheima.AppConfig;

import java.awt.*;


public class BallFightFrame extends GameJFrame{
    public BallFightFrame() throws HeadlessException {
        //设置标题
        setTitle("飞机大战");
        //设置图标
        setIconImage(ImageUtil.getImg("logo.png"));
        //设置窗体大小
        setSize(AppConfig.APP_WIDTH,AppConfig.APP_HEIGHT);
        //设置一下初始位置
        setLocation(100,100);
    }
}
package com.itheima.ui;

import com.hzheima.game.GameJPanel;
import com.hzheima.util.ImageUtil;
import com.itheima.AppConfig;
import com.itheima.MusicUtil;
import com.itheima.pojo.*;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Random;


public class BallFightPanel extends GameJPanel {
    BufferedImage bj;//背景
    BufferedImage bj1;//背景1
    BufferedImage money;//金币
    BufferedImage st;//左括号
    BufferedImage end;//右括号
    Plane plane;
    ArrayList<Ball> balls = new ArrayList<>();//球集合
    private boolean gameOver = false;
    private boolean pass = false;
    int countBall = 0;
    int countMoney = 0;
    int countKillBall = 0;//累计击杀的小球
    ArrayList<Level> levels = new ArrayList<>();//关卡集合
    ArrayList<Money> moneys = new ArrayList<>();//金币集合
    ArrayList<BallBoss> ballBosses = new ArrayList<>();//boss集合
    boolean isBallBossDead = false;
    BallBoss ballBoss;
    int level = 0;//关卡
    int score = 0;//分数
    int allMoney = 0;//金币
    int allMoneyPeak = 0;//历史最高金币
    boolean isStartGame = false;

    public BallFightPanel() {
        //设置背景颜色
        setBackground(Color.BLUE);
        bj = ImageUtil.getImg("BG.png");
        bj1 = ImageUtil.getImg("BGkunkun.png");
        money = ImageUtil.getImg("money.png");
        st = ImageUtil.getImg("st.png");
        end = ImageUtil.getImg("ed.png");
        addLevel(AppConfig.LEVELNUM);
        plane = new Plane();
        ballBoss = new BallBoss();
        //背景音乐
        playBGMusic();
    }

    private void playBGMusic() {
        MusicUtil.playMp3("kunkunBG.mp3");
    }

    @Override
    public void paint(Graphics g) {
        if (!isStartGame) {
            //Graphics g相当于一个画笔,可以绘制图片文字
            g.drawImage(bj1, 0, 0, 512, 768, null);
            g.setColor(Color.black);
            g.setFont(new Font("楷体", Font.BOLD, 50));
            g.drawString("坤坤大战", 150, 80);
            g.setColor(Color.black);
            g.setFont(new Font("楷体", Font.BOLD, 30));
            g.drawString("开始游戏(K)", 160, 150);
            g.drawString("退出游戏(L)", 160, 200);
        }
        else {
            //Graphics g相当于一个画笔,可以绘制图片文字
            g.drawImage(bj, 0, 0, 512, 768, null);
            //写分数,设置字体
            g.setFont(new Font("楷体", Font.BOLD, 25));
            //设置颜色
            g.setColor(Color.white);
            //画分数
            g.drawString("分数:" + score, 30, 30);
            g.drawString("历史最高:" + allMoneyPeak + "k", 280, 60);
            if (allMoney > allMoneyPeak) {
                allMoneyPeak = allMoney;
            }
            g.drawImage(money, 280, 8, 30, 30, null);
            g.drawString(allMoney + "k", 340, 30);
            g.setFont(new Font("楷体", Font.PLAIN, 30));
            g.drawString("关卡" + 0 + (levels.get(level).getLevel() + 1), 26, 60);
            g.drawImage(st, 20, 80, 5, 25, null);
            g.drawImage(end, 512 - 30, 80, 5, 25, null);
            g.setColor(Color.GRAY);
            for (int i = 0; i < levels.get(level).getBallKill(); i++) {
                //画矩形的方法
                g.fillRect(30 + i * (10 + 5), 80, 10, 25);
            }
            g.setColor(Color.CYAN);
            for (int i = 0; i < countKillBall; i++) {
                if (countKillBall>levels.get(level).getBallKill()) {
                    countKillBall --;
                }else{
                    g.fillRect(30 + i * (10 + 5), 80, 10, 25);
                }
            }
            plane.draw(g);
//        bullet.draw(g);
            ArrayList<Bullet> bullets = plane.getBullets();
            ArrayList<Bullet> bullets1 = plane.getBullets1();
            ArrayList<Bullet> bullets2 = plane.getBullets2();
            for (int i = 0; i < bullets.size(); i++) {
                Bullet bullet = bullets.get(i);
                if (plane.getUpgrade() == 0) {
                    plane.setMag(10);
                    bullet.draw(g);
                } else if (plane.getUpgrade() == 1) {
                    plane.setMag(1);
                    bullet.setSpeed(20);
                    bullet.draw(g);
                } else if (plane.getUpgrade() == 2) {
                    plane.setMag(1);
                    bullet.setSpeed(40);
                    bullet.setImg(ImageUtil.getImg("guang.png"));
                    bullet.setHeight(ImageUtil.getImg("guang.png").getHeight() / 2);
                    bullet.setWidth(ImageUtil.getImg("fire.png").getWidth() * 5);
                    bullet.setX(plane.getX() - 60);
                    bullet.draw(g);
                }
            }
            for (int i = 0; i < bullets1.size(); i++) {
                Bullet bullet1 = bullets1.get(i);
                if (plane.getUpgrade() == 0) {
                    plane.setMag(10);
                    bullet1.draw(g);
                } else if (plane.getUpgrade() == 1) {
                    plane.setMag(1);
                    bullet1.setSpeed(20);
                    bullet1.draw(g);
                }
            }
            for (int i = 0; i < bullets2.size(); i++) {
                Bullet bullet2 = bullets2.get(i);
                if (plane.getUpgrade() == 0) {
                    plane.setMag(10);
                    bullet2.draw(g);
                } else if (plane.getUpgrade() == 1) {
                    plane.setMag(1);
                    bullet2.setSpeed(20);
                    bullet2.draw(g);
                }
            }
            for (int i = 0; i < balls.size(); i++) {
                Ball ball = balls.get(i);
                ball.draw(g);
            }
            for (int i = 0; i < moneys.size(); i++) {
                Money money = moneys.get(i);
                money.draw(g);
            }
            if (level == 2&&!isBallBossDead) {
                ballBoss.draw(g);
                ArrayList<Ball> balls1 = ballBoss.getBalls();
                for (int i = 0; i < balls1.size(); i++) {
                    Ball ball = balls1.get(i);
                    ball.draw(g);
                }
            }

            if (gameOver) {
                //设置画笔为红色
                g.setColor(Color.red);
                Graphics2D g2d = (Graphics2D) g;
                //设置透明度,绘制红色矩形
                AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.42f);
                g2d.setComposite(alphaComposite);
                g2d.fillRect(0, 200, AppConfig.APP_WIDTH, 300);
                //透明度设置回来,然后设置成白色
                AlphaComposite alphaComposite1 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f);
                g2d.setComposite(alphaComposite1);
                g.setColor(Color.white);
                g.setFont(new Font("楷体", Font.BOLD, 30));
                g.drawString("游戏结束", 180, 240);
                g.setColor(Color.yellow);
                g.drawString("本次得分:" + score, 150, 300);
                g.drawString("获得金币:", 100, 370);
                g.drawImage(money, 250, 340, 40, 40, null);
                g.drawString(allMoney + "k", 300, 370);
                g.setColor(Color.white);
                g.drawString("{按M重新开始游戏}", 110, 480);
            }
            if (pass) {
                //设置画笔为红色
                g.setColor(Color.red);
                Graphics2D g2d = (Graphics2D) g;
                //设置透明度,绘制红色矩形
                AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.42f);
                g2d.setComposite(alphaComposite);
                g2d.fillRect(0, 200, AppConfig.APP_WIDTH, 300);
                //透明度设置回来,然后设置成白色
                AlphaComposite alphaComposite1 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f);
                g2d.setComposite(alphaComposite1);
                g.setColor(Color.white);
                g.setFont(new Font("楷体", Font.BOLD, 30));
                g.drawString("恭喜过关", 180, 240);
                g.setColor(Color.yellow);
                g.drawString("当前得分:" + score, 150, 300);
                g.drawString("当前获得金币:", 100, 370);
                g.drawImage(money, 250, 340, 40, 40, null);
                g.drawString(allMoney + "k", 300, 370);
                g.setColor(Color.white);
                g.drawString("{按N开始下一关}", 110, 480);
            }
        }
    }

    @Override
    public void run() {
        if (!gameOver && !pass && isStartGame) {
            plane.shoot();
            if (level == 2 && !isBallBossDead) {
                moveBallBoss();
                ballBoss.attack();
                isShootBossBall();
            }
            creatBall();
            creatMoney();
            moveMoney();
            moveBall();
            isShootBall();//判断是否击中小球
            isMoney();//判断飞机是否碰到金币
            isCrash();//判断飞机和小球是否碰撞
            isPass();//判断飞机和小球是否碰撞
        }
        repaint();
    }

    //判断飞机和小球是否碰撞
    private void isCrash() {
        for (int i = 0; i < balls.size(); i++) {
            Ball ball = balls.get(i);
            if (plane.isCrashByBall(ball)) {
                balls.remove(ball);
                gameOver = true;
            }
        }
        for (int i = 0; i < ballBoss.getBalls().size(); i++) {
            Ball ball = ballBoss.getBalls().get(i);
            if (plane.isCrashByBall(ball)) {
                ballBoss.getBalls().remove(ball);
                gameOver = true;
            }
        }
    }

    //判断飞机是否碰到金币
    private void isMoney() {
        for (int i = 0; i < moneys.size(); i++) {
            Money money = moneys.get(i);
            if (plane.isCrashByMoney(money)) {
                moneys.remove(money);
                allMoney++;
            }
        }
    }

    //是否的过关的方法
    private void isPass() {
        if (countKillBall >= levels.get(level).getBallKill()) {
            if (level == 2){
                if (isBallBossDead) {
                    pass = true;
                }
            }else{
                if (levels.get(level).getLevel() < AppConfig.LEVELNUM - 1) {
                    pass = true;
                } else {
                    gameOver = true;
                }
            }
        }
    }

    private void isShootBall() {
        for (int i = 0; i < balls.size(); i++) {
            Ball ball = balls.get(i);
            ArrayList<Bullet> bullets = plane.getBullets();
            ArrayList<Bullet> bullets1 = plane.getBullets1();
            ArrayList<Bullet> bullets2 = plane.getBullets2();
            isShootOneBoll(balls, bullets, ball);
            isShootOneBoll(balls, bullets1, ball);
            isShootOneBoll(balls, bullets2, ball);
        }
        for (int i = 0; i < ballBoss.getBalls().size(); i++) {
            Ball ball = ballBoss.getBalls().get(i);
            ArrayList<Bullet> bullets = plane.getBullets();
            ArrayList<Bullet> bullets1 = plane.getBullets1();
            ArrayList<Bullet> bullets2 = plane.getBullets2();
            isShootOneBoll(ballBoss.getBalls(), bullets, ball);
            isShootOneBoll(ballBoss.getBalls(), bullets1, ball);
            isShootOneBoll(ballBoss.getBalls(), bullets2, ball);
        }
    }

    private void isShootBossBall() {
        Random rand = new Random();
        ArrayList<Bullet> bullets = plane.getBullets();
        ArrayList<Bullet> bullets1 = plane.getBullets1();
        ArrayList<Bullet> bullets2 = plane.getBullets2();
        for (int j = bullets.size() - 1; j >= 0; j--) {
            Bullet bullet = bullets.get(j);
            if (ballBoss.shootBy(bullet)) {
                bullets.remove(bullet);
                if (ballBoss.getBlood() <= 0) {
                    MusicUtil.playMp3("kunkundie.mp3");
                    isBallBossDead = true;
                    for (int i = 0; i < 20; i++) {
                        Money money = new Money();
                        money.setX(ballBoss.getX()+i*10);
                        money.setY(ballBoss.getY()+rand.nextInt(ballBoss.getHeight()));
                        moneys.add(money);
                    }
                }
            }
        }
        for (int j = bullets1.size() - 1; j >= 0; j--) {
            Bullet bullet = bullets1.get(j);
            if (ballBoss.shootBy(bullet)) {
                bullets1.remove(bullet);
                if (ballBoss.getBlood() <= 0) {
                    MusicUtil.playMp3("kunkundie.mp3");
                    isBallBossDead = true;
                    for (int i = 0; i < 20; i++) {
                        Money money = new Money();
                        money.setX(ballBoss.getX()+i*10);
                        money.setY(ballBoss.getY()+rand.nextInt(ballBoss.getHeight()));
                        moneys.add(money);
                    }
                }
            }
        }
        for (int j = bullets2.size() - 1; j >= 0; j--) {
            Bullet bullet = bullets2.get(j);
            if (ballBoss.shootBy(bullet)) {
                bullets2.remove(bullet);
                if (ballBoss.getBlood() <= 0) {
                    MusicUtil.playMp3("kunkundie.mp3");
                    isBallBossDead = true;
                    for (int i = 0; i < 20; i++) {
                        Money money = new Money();
                        money.setX(ballBoss.getX()+i*10);
                        money.setY(ballBoss.getY()+rand.nextInt(ballBoss.getHeight()));
                        moneys.add(money);
                    }
                }
            }
        }
    }

    private void isShootOneBoll(ArrayList<Ball> balls, ArrayList<Bullet> bullets, Ball ball) {
        for (int j = bullets.size() - 1; j >= 0; j--) {
            Bullet bullet = bullets.get(j);
            if (ball.shootBy(bullet)) {
                bullets.remove(bullet);
                if (ball.getBlood() <= 0) {
                    balls.remove(ball);
                    if (ball.getIsBossBall() == 0) {
                        countKillBall++;//击杀小球就加1
                        score += ball.getBlood();//击杀小球就加分
                    }
                }
                if ((ball.getBlood() <= ball.getInitialBlood() / 2) && ball.getBlood() > 1) {
                    ArrayList<Ball> list = ball.split();
                    balls.remove(ball);
                    balls.addAll(list);
                    if (ball.getIsBossBall() == 0) {
                        score += ball.getInitialBlood();//把球打分裂了就加分
                    }
                }
            }
        }
    }

    private void moveBall() {
        for (int i = 0; i < balls.size(); i++) {
            Ball ball = balls.get(i);
            ball.move();
        }
    }

    //精英球移动方法
    private void moveBallBoss() {
        if (ballBoss.getY() <= 0) {
            ballBoss.move();
        } else {
            ballBoss.setySpeed(0);
        }
    }

    //添加球的方法
    private void creatBall() {
        if (countBall % 20 == 0) {
            if (balls.size() < levels.get(level).getBallNum() + 1) {
                balls.add(new Ball());
            }
            countBall = 0;
        }
        countBall++;
    }

    //创建boss方法
//    private void creatBallBoss() {
//        ballBosses.add(new BallBoss());
//    }

    //添加金币的方法
    private void creatMoney() {
        if (countMoney % 100 == 0) {
            moneys.add(new Money());
            countMoney = 0;
        }
        countMoney++;
    }

    //移动金币
    private void moveMoney() {
        for (int i = 0; i < moneys.size(); i++) {
            Money money = moneys.get(i);
            money.move();
        }
    }

    //添加关卡数量方法
    public void addLevel(int num) {
        for (int i = 0; i < num; i++) {
            levels.add(new Level(i, i, 10 + i * 5));
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == 'A' && !gameOver && !pass) {
            plane.moveToLeft();
        } else if (e.getKeyCode() == 'D' && !gameOver && !pass) {
            plane.moveToRight();
        } else if (e.getKeyCode() == 'W' && !gameOver && !pass) {
            plane.moveToUp();
        } else if (e.getKeyCode() == 'S' && !gameOver && !pass) {
            plane.moveToDown();
        } else if (e.getKeyCode() == 'G' && !gameOver && !pass) {
            if (plane.getUpgrade() == 1) {
                plane.setImg(ImageUtil.getImg("hero.png"));
                plane.setUpgrade(0);
            } else {
                plane.setImg(ImageUtil.getImg("hero2.jpg"));
                plane.setUpgrade(1);
            }

        } else if (e.getKeyCode() == 'R' && !gameOver && !pass) {
            plane.setImg(ImageUtil.getImg("dijia.png"));
            plane.setUpgrade(2);
        } else if (e.getKeyCode() == 'M' && gameOver == true) {
            //游戏重新开始
            gameOver = false;
            //小球清空
            balls.clear();
            //方块清空
            countKillBall = 0;
            //飞机重新创建
            plane = new Plane();
            //清空分数
            score = 0;
            //清空飞行的金币
            moneys.clear();
            //清空金币
            allMoney = 0;
            //清空关卡
            level = 0;
            //还原boss
            isBallBossDead = false;
            ballBoss.setBlood(100);
        } else if (e.getKeyCode() == 'N' && pass == true) {
            //关卡+1
            level += 1;
            //游戏重新开始
            pass = false;
            //小球清空
            balls.clear();
            //方块清空
            countKillBall = 0;
            //飞机重新创建
            plane = new Plane();
            //清空飞行的金币
            moneys.clear();
        }
        else if (e.getKeyCode() == 'K'){
            isStartGame = true;
        }
        //重新绘制一次
        repaint();
    }

}
package com.itheima;


public class AppConfig {
    //游戏的宽度
    public static final int APP_WIDTH = 512;

    //游戏的高度
    public static final int APP_HEIGHT = 768;
    public static final int LEFT = 0;
    public static final int RIGHT = 1;
    public static final int BALL_MAX_CREATCOUNT = 1;
    public static final int LEVELNUM = 5;
}
package com.itheima;

import javazoom.jl.player.Player;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import java.io.BufferedInputStream;

public class MusicUtil {
    /**
     * 可以播放mp3文件,只会播放一次
     * @param fileName
     */
    public static void playMp3(String fileName){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    BufferedInputStream in = new BufferedInputStream(MusicUtil.class.getResourceAsStream("/"+fileName));
                    Player player = new Player(in);
                    player.play();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * 可以播放wmv文件,只会播放一次
     * @param fileName
     */
    public static void playMusic(String fileName){
        try {
            Clip clip = AudioSystem.getClip();
            AudioInputStream in =
                    AudioSystem.getAudioInputStream(MusicUtil.class.getResourceAsStream("/" + fileName));
            clip.open(in);
            clip.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 循环播放音乐,播放不了Mp3
     * @param fileName
     */
    public static void loopPlayMusic(String fileName){
        try {
            Clip clip = AudioSystem.getClip();
            AudioInputStream in =
                    AudioSystem.getAudioInputStream(MusicUtil.class.getResourceAsStream("/" + fileName));
            clip.open(in);
            clip.loop(Clip.LOOP_CONTINUOUSLY);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
package com.itheima.start;

import com.itheima.ui.BallFightFrame;
import com.itheima.ui.BallFightPanel;

public class Game {
    public static void main(String[] args) {
        //创建窗体对象
        BallFightFrame ballFightFrame = new BallFightFrame();
        //创建一个面板对象
        BallFightPanel ballFightPanel = new BallFightPanel();
        //将面板添加到窗口里面
        ballFightFrame.add(ballFightPanel);
        //面板调用了action方法,每隔30ms,会调用一次run方法
        ballFightPanel.action();
        //让窗口显示
        ballFightFrame.setVisible(true);
    }
}

类分组

image.png