关于使用Java实现简单飞机大战

此次使用Java语言实现一个简易的2D小游戏飞机大战。
实现大概功能:创建一个页面,存在一个玩家飞机,可以射击子弹,当子弹打到敌机、奖赏类(Bee与Bigbee)时,获得相应的积分或是奖励。boss在达到固定积分时会出现,并且会射击子弹。
由于Bee和Bigbee属性类似,只有图片及奖赏不同,因此可继承于同一个接口;而Enemyplane和Boss在笔者写的过程中由于Boss添加了射击的属性,因此在生成时需要进行判断是否为Boss,若生成的是boss则生成boss的子弹,在代码中使用了if (one instanceof Enemy2)进行判断,因此写了两个接口,读者可自行优化使代码简洁;其余问题见源码注释。
注意:在此不建议在Game_Background中的生成飞行物使用普通数组,因为使用普通的数组,在增删飞行物时,过程复杂,对空间压力较大,不利于操作,生成飞行物过多时容易爆存,因此建议使用ArrayList进行操作。
缺点:设置了两个Enemy接口,当玩家子弹碰撞boss子弹时,双方子弹不会消失,模式太少。
在源码中注释了判断两个任意飞行物是否相撞的函数,可自行添加修改。
下面是简易的思维导图
在这里插入图片描述
Object_Fly类

package Game06;

import java.awt.image.BufferedImage;
import java.util.ArrayList;

public abstract class Object_Fly {
    protected int x; //横轴坐标
    protected int y; //纵轴坐标
    protected int longness; //图片长度
    protected int width; //图片宽度
    protected BufferedImage photo;

    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 longness;
    }
    public void setHeight(int longness) {
        this.longness = longness;
    }
    public BufferedImage getPhote() {
        return photo;
    }
    public void setPhoto(BufferedImage photo) {
        this.photo = photo;
    }

    public abstract void Flying(); //飞行函数
    public abstract boolean outBackGround(); //判断是否出游戏界面
//    public static boolean boom(Object_Fly f1, Object_Fly f2){ //判断是否碰撞
//        //求出两个矩形的中心点
//        int f1x = f1.x + f1.width / 2;
//        int f1y = f1.y + f1.longness / 2;
//        int f2x = f2.x + f2.width / 2;
//        int f2y = f2.y + f2.longness / 2;
//
//        //横向和纵向碰撞检测
//        boolean H = Math.abs(f1x - f2x) < (f1.width + f2.width) / 2;
//        boolean V = Math.abs(f1y -f2y) < (f1.longness + f2.longness) / 2;
//
//        //必须两个方向同时碰撞
//        return H & V;
//    }
    public boolean shootBy(Bullet bullet){ //检测敌机是否被子弹击中
        int x = bullet.x;  //子弹横坐标
        int y = bullet.y;  //子弹纵坐标
        return this.x < x && x < this.x + width && this.y < y && y < this.y + longness;
    }

    public boolean shootByBoss(Bullet2 bullet){ //检测玩家是否被Boss的子弹击中
        int x = bullet.x;  //子弹横坐标
        int y = bullet.y;  //子弹纵坐标
        return this.x < x && x < this.x + width && this.y < y && y < this.y + longness;
    }

    public ArrayList<Bullet2> boss_shoot(){
        int xStep1 = width / 4;    //分成4份
        int yStep = 20;  //步
        ArrayList<Bullet2> bullets2 = new ArrayList<>();
        Bullet2 b0 = new Bullet2(x + xStep1, y - yStep);  //y-yStep(子弹距飞机的位置)
        Bullet2 b1 = new Bullet2(x + 3 * xStep1, y - yStep);
        bullets2.add(b0);
        bullets2.add(b1);
        return bullets2;
    }
}

Player类(玩家)

package Game06;

import java.awt.image.BufferedImage;
import java.util.ArrayList;

//玩家飞机
public class Player extends Object_Fly {
    private int cnt_fire; //火力值
    private int blood; //血量
    private int index; //标记使用的玩家的飞机
    private BufferedImage[] Player_photos = {};  //英雄机图片
    public int getBlood() {
        return blood;
    }
    public void setBlood(int blood) {
        this.blood = blood;
    }
    public void addBlood() { //血量加1
        blood++;
    }
    public void addDoubleBlood() {
        blood += 2;
    }
    public void subBlood() { //血量减1
        blood--;
    }

    public int getCnt_fire() {
        return cnt_fire;
    }
    public void setCnt_fire(int cnt_fire) {
        this.cnt_fire = cnt_fire;
    }
    public void addCnt_fire() {
        cnt_fire = 2;
    }
    public void addCnt_fireDOUBLE() {
        cnt_fire = 80;
    }


    public Player() {
        blood = 3;
        cnt_fire = 0;
        Player_photos = new BufferedImage[]{Game_BackGround.player0, Game_BackGround.player1};
        photo = Game_BackGround.player0; //初始图片是player0
        longness = photo.getHeight();
        width = photo.getWidth();
        x = 150;
        y = 400;
    }

    public void Flying(int x, int y) { //飞行物移动
        if(Player_photos.length > 0){
            photo = Player_photos[index++ / 10 % Player_photos.length];  //切换图片player0,player1
        }
    }

    public boolean outOfBounds() { //判断是否出游戏界面
        return false;
    }

    public ArrayList<Bullet> shoot() { //射击函数
        int xStep1 = width / 4;    //分成4份
        int xStep2 = width / 8;    //分成6份
        int yStep = 20;  //步
        ArrayList<Bullet> bullets = new ArrayList<>();
        //碰到普通蜜蜂就是三倍火力
        if (cnt_fire == 2) {
            Bullet b0 = new Bullet(x + xStep1, y - yStep);  //y-yStep(子弹距飞机的位置)
            Bullet b1 = new Bullet(x + 3 * xStep1, y - yStep);
            bullets.add(b0);
            bullets.add(b1);
            return bullets;
        } else if (cnt_fire  > 2) {
            Bullet b0 = new Bullet(x + 1 * xStep2, y - yStep);
            Bullet b1 = new Bullet(x + 3 * xStep2, y - yStep);
            Bullet b2 = new Bullet(x + 5 * xStep2, y - yStep);
            Bullet b3 = new Bullet(x + 7 * xStep2, y - yStep);
            bullets.add(b0);
            bullets.add(b1);
            bullets.add(b2);
            bullets.add(b3);
            return bullets;
        } else {
            Bullet b0 = new Bullet(x + 2 * xStep1, y - yStep);
            bullets.add(b0);
            return bullets;
        }
    }

    public void Moving(int x,int y){ //玩家移动
        this.x = x - width / 2;
        this.y = y - longness / 2;
    }


    @Override
    public void Flying() {
        if(Player_photos.length>0){
            photo = Player_photos[index++ / 10 % Player_photos.length];  //切换图片hero0,hero1
        }
    }

    @Override
    public boolean outBackGround() {
        return false;
    }


    public boolean hit(Object_Fly other){ //i

        int x1 = other.x - this.width / 2;                 //x坐标最小距离
        int x2 = other.x + this.width / 2 + other.width;   //x坐标最大距离
        int y1 = other.y - this.longness / 2;                //y坐标最小距离
        int y2 = other.y + this.longness /2 + other.longness; //y坐标最大距离

        int player_x = this.x + this.width / 2;               //英雄机x坐标中心点距离
        int player_y = this.y + this.longness / 2;              //英雄机y坐标中心点距离

        return player_x > x1 && player_x < x2 && player_y > y1 && player_y < y2;   //区间范围内为撞上了
    }
}

Bee and Bigbee类

package Game06;

import java.awt.image.BufferedImage;
import java.util.ArrayList;

//玩家飞机
public class Player extends Object_Fly {
    private int cnt_fire; //火力值
    private int blood; //血量
    private int index; //标记使用的玩家的飞机
    private BufferedImage[] Player_photos = {};  //英雄机图片
    public int getBlood() {
        return blood;
    }
    public void setBlood(int blood) {
        this.blood = blood;
    }
    public void addBlood() { //血量加1
        blood++;
    }
    public void addDoubleBlood() {
        blood += 2;
    }
    public void subBlood() { //血量减1
        blood--;
    }

    public int getCnt_fire() {
        return cnt_fire;
    }
    public void setCnt_fire(int cnt_fire) {
        this.cnt_fire = cnt_fire;
    }
    public void addCnt_fire() {
        cnt_fire = 2;
    }
    public void addCnt_fireDOUBLE() {
        cnt_fire = 80;
    }


    public Player() {
        blood = 3;
        cnt_fire = 0;
        Player_photos = new BufferedImage[]{Game_BackGround.player0, Game_BackGround.player1};
        photo = Game_BackGround.player0; //初始图片是player0
        longness = photo.getHeight();
        width = photo.getWidth();
        x = 150;
        y = 400;
    }

    public void Flying(int x, int y) { //飞行物移动
        if(Player_photos.length > 0){
            photo = Player_photos[index++ / 10 % Player_photos.length];  //切换图片player0,player1
        }
    }

    public boolean outOfBounds() { //判断是否出游戏界面
        return false;
    }

    public ArrayList<Bullet> shoot() { //射击函数
        int xStep1 = width / 4;    //分成4份
        int xStep2 = width / 8;    //分成6份
        int yStep = 20;  //步
        ArrayList<Bullet> bullets = new ArrayList<>();
        //碰到普通蜜蜂就是三倍火力
        if (cnt_fire == 2) {
            Bullet b0 = new Bullet(x + xStep1, y - yStep);  //y-yStep(子弹距飞机的位置)
            Bullet b1 = new Bullet(x + 3 * xStep1, y - yStep);
            bullets.add(b0);
            bullets.add(b1);
            return bullets;
        } else if (cnt_fire  > 2) {
            Bullet b0 = new Bullet(x + 1 * xStep2, y - yStep);
            Bullet b1 = new Bullet(x + 3 * xStep2, y - yStep);
            Bullet b2 = new Bullet(x + 5 * xStep2, y - yStep);
            Bullet b3 = new Bullet(x + 7 * xStep2, y - yStep);
            bullets.add(b0);
            bullets.add(b1);
            bullets.add(b2);
            bullets.add(b3);
            return bullets;
        } else {
            Bullet b0 = new Bullet(x + 2 * xStep1, y - yStep);
            bullets.add(b0);
            return bullets;
        }
    }

    public void Moving(int x,int y){ //玩家移动
        this.x = x - width / 2;
        this.y = y - longness / 2;
    }


    @Override
    public void Flying() {
        if(Player_photos.length>0){
            photo = Player_photos[index++ / 10 % Player_photos.length];  //切换图片hero0,hero1
        }
    }

    @Override
    public boolean outBackGround() {
        return false;
    }


    public boolean hit(Object_Fly other){ //i

        int x1 = other.x - this.width / 2;                 //x坐标最小距离
        int x2 = other.x + this.width / 2 + other.width;   //x坐标最大距离
        int y1 = other.y - this.longness / 2;                //y坐标最小距离
        int y2 = other.y + this.longness /2 + other.longness; //y坐标最大距离

        int player_x = this.x + this.width / 2;               //英雄机x坐标中心点距离
        int player_y = this.y + this.longness / 2;              //英雄机y坐标中心点距离

        return player_x > x1 && player_x < x2 && player_y > y1 && player_y < y2;   //区间范围内为撞上了
    }
}

package Game06;

import java.util.Random;

public class Bigbee extends Object_Fly implements BeeAward {
    private int awardType; //2是加血,3是加火力
    private int x_move = 3; //滑稽的移动速度快
    private int y_move = 3;
    public void Flying() { //蜜蜂的移动
        x += x_move;
        y += y_move;
        if(x > Game_BackGround.WIDTH - width){
            x_move = -1;
        }
        if(x < 0){
            x_move = 1;
        }
    }

    @Override
    public boolean outBackGround() { //判断是否已经飞出游戏界面
        return y > Game_BackGround.LONGNESS;
    }

    @Override
    public int getType() { //获得奖励类型
        return awardType;
    }
    public Bigbee() {
        this.photo = Game_BackGround.bigbee;
        width = photo.getWidth();
        longness = photo.getHeight();
        y = -longness;
        Random rand = new Random();
        x = rand.nextInt(Game_BackGround.WIDTH - width);
        awardType = (rand.nextInt(2) + 2);   //初始化时给奖励
    }
}

Enemy and Boss

package Game06;

import java.util.Random;

//敌机
public class enemyPlane extends Object_Fly implements Enemy {
    private int grades = 10; //每个敌机的分数
    private int y_move = 2; //每次移动坐标两格
    public int getGrades() { //对应接口,获得击落的敌机所带的分数
        return grades;
    }

    public  boolean outOfBounds() { //判断敌机是否已经飞出游戏界面
        return y > Game_BackGround.LONGNESS;
    }

    public void Flying() { //敌机没次移动
        y += y_move;
    }

    @Override
    public boolean outBackGround() {
        return false;
    }
    public enemyPlane() {
        this.photo = Game_BackGround.enemyPlane;
        longness = photo.getHeight();
        width = photo.getWidth();
        y = -longness;
        Random r = new Random();
        x = r.nextInt(Game_BackGround.WIDTH - width);
    }
}

package Game06;

import java.util.ArrayList;
import java.util.Random;

public class Boss extends Object_Fly implements Enemy2 {
    private int grades = 80; //每个敌机的分数
    private int x_move = 1; //每次移动坐标两格
    private int y_move = 2; //每次y轴移动
    public int getGrades() { //对应接口,获得击落的敌机所带的分数
        return grades;
    }

    public  boolean outOfBounds() { //判断敌机是否已经飞出游戏界面
        return y > Game_BackGround.LONGNESS;
    }

    public void Flying() { //敌机没次移动
        if (y < 70) y += y_move;
        x += x_move;
        if(x > Game_BackGround.WIDTH - width){
            x_move = -1;
        }
        if(x < 0){
            x_move = 1;
        }
    }

    @Override
    public boolean outBackGround() {
        return false;
    }

    public Boss() {
        this.photo = Game_BackGround.boss;
        longness = photo.getHeight();
        width = photo.getWidth();
        y = -longness;
        Random r = new Random();
        x = r.nextInt(Game_BackGround.WIDTH - width);
    }

    public ArrayList<Bullet2> boss_shoot() { //射击函数
        int xStep1 = width / 4;    //分成4份
        int yStep = 20;  //步
        ArrayList<Bullet2> bullets2 = new ArrayList<>();
        Bullet2 b0 = new Bullet2(x + xStep1, y + yStep);  //y-yStep(子弹距飞机的位置)
        Bullet2 b1 = new Bullet2(x + 3 * xStep1, y + yStep);
        bullets2.add(b0);
        bullets2.add(b1);
        return bullets2;

    }
}

Bullet1 and Bullet2

package Game06;

public class Bullet extends Object_Fly {
    int y_move = 2; //每次移动坐标两格

    public Bullet(int x, int y) { //子弹的初始位置与玩家飞机位置有关
        this.x = x;
        this.y = y;
        this.photo = Game_BackGround.bullet;
    }

    @Override
    public void Flying() {
        y -= y_move;

    }

    @Override
    public boolean outBackGround() {
        return y < -longness;
    }

}

package Game06;

public class Bullet2 extends Object_Fly {
    int y_move = 2;

    public Bullet2(int x, int y) { //boss子弹的初始位置与boss位置有关
        this.x = x;
        this.y = y;
        this.photo = Game_BackGround.bullet2;
    }
    @Override
    public void Flying() {
        y += y_move;
    }

    @Override
    public boolean outBackGround() {
        return y < longness;
    }
}

各种接口

package Game06;

public interface BeeAward { //击中小蜜蜂的奖励
    public int LIFE = 0; //生命值增加
    public int DOUBLEFIRE = 1; //双倍火力
    public int DOUBLELIFE = 2; //大蜜蜂时生命值加倍
    public int DOUBLEDOUBLEFIRE = 3; //大蜜蜂时火力*3
    public int getType();
}

package Game06;

//击中敌机的接口
public interface Enemy {
    int getGrades(); //敌机所带的分数
}

package Game06;

public interface Enemy2 {
    //boss接口
    int getGrades(); //敌机所带的分数
}

图片生成及游戏启动函数
Game_Background

package Game06;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.List;
import java.util.Timer;
import java.util.*;

public class Game_BackGround extends JPanel { //Game_Background相当于一个画板

    //背景图片的大小400*654
    public static final int WIDTH = 400; // 面板宽
    public static final int LONGNESS = 654; // 面板高

    //游戏的当前状态: START RUNNING PAUSE GAME_OVER
    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 grades = 0; // 得分
    private int behit_cnt = 0; //boss被击中次数
    private int boss_index = -1; //记录boss的编号
    private int boss_cnt = 0; //控制boss生成个数
    private Timer timer; // 定时器
    private int intervel = 1000 / 100; // 时间间隔(毫秒)
    private  boolean isBossAlive =false;
    public static BufferedImage player0; //英雄机状态0
    public static BufferedImage player1; //英雄机状态1
    public static BufferedImage bee; //小蜜蜂
    public static BufferedImage bigbee; //大蜜蜂
    public static BufferedImage enemyPlane; //敌机
    public static BufferedImage boss; //敌机图片
    public static BufferedImage bullet; //子弹
    public static BufferedImage bullet2; //子弹
    public static BufferedImage background; //背景图片
    public static BufferedImage start; //开始图片
    public static BufferedImage pause; //暂停图片
    public static BufferedImage gameover; //游戏结束

    private Player player = new Player(); // 玩家飞机
    private List<Bullet> bullets = new ArrayList<>(); // 子弹数组
    private List<Bullet2> bullets2 = new ArrayList<>(); // boss子弹数组
    private List<Object_Fly> flyings = new ArrayList<>(); //代替蜜蜂、大蜜蜂及敌机和boss

    //静态块专门加载静态资源
    static{
        try {
            background = ImageIO.read(Game_BackGround.class.getResource("background.png"));
            player0 = ImageIO.read(Game_BackGround.class.getResource("player0.png"));
            player1 = ImageIO.read(Game_BackGround.class.getResource("player1.png"));
            bee = ImageIO.read(Game_BackGround.class.getResource("bee.png"));
            bigbee = ImageIO.read(Game_BackGround.class.getResource("bigbee.png"));
            enemyPlane = ImageIO.read(Game_BackGround.class.getResource("enemy1.png"));
            boss = ImageIO.read(Game_BackGround.class.getResource("boss.png"));
            bullet = ImageIO.read(Game_BackGround.class.getResource("bullet.png"));
            bullet2 = ImageIO.read(Game_BackGround.class.getResource("bullet2.png"));
            start = ImageIO.read(Game_BackGround.class.getResource("start.png"));
            pause = ImageIO.read(Game_BackGround.class.getResource("pause.png"));
            gameover = ImageIO.read(Game_BackGround.class.getResource("gameover.png"));

        } catch (IOException e) {

            e.printStackTrace();
        }
    }

    //Graphics相当于画笔的作用
    public void paint(Graphics g) {
        g.drawImage(background, 0, 0, null); //x、y的值相当于绘制的起点
        g.drawImage(player.getPhote(), player.x, player.y, null); //玩家飞机
        for (int i = 0; i < flyings.size(); i++) { //蜜蜂和敌机
            g.drawImage(flyings.get(i).getPhote(), flyings.get(i).x, flyings.get(i).y, null);
        }
        for (int i = 0; i < bullets.size(); i++) { //子弹
            g.drawImage(bullets.get(i).getPhote(), bullets.get(i).x - bullets.get(i).getWidth() / 2, bullets.get(i).y, null);
        }
        for (int i = 0; i < bullets2.size(); i++) { //boss子弹
            g.drawImage(bullets2.get(i).getPhote(), bullets2.get(i).x - bullets2.get(i).getWidth() / 2, bullets2.get(i).y, null);
        }
        paintGrades(g);
        paintState(g);
    }

    //分数
    public void paintGrades(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("GRADES:" + grades, x, y); // 画分数
        y += 20; // y坐标增20
        g.drawString("BLOOD:" + player.getBlood(), 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 play() {

        MouseAdapter m = new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) { //鼠标移动
                if (state == RUNNING) { // 运行状态下移动英雄机--随鼠标位置
                    int x = e.getX();
                    int y = e.getY();
                    player.Moving(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.clear(); // 清空飞行物
                        bullets.clear(); // 清空子弹
                        player = new Player(); // 重新创建英雄机
                        grades = 0; // 清空成绩
                        state = START; // 状态设置为启动
                        break;
                }
            }
        };
        this.addMouseListener(m); //处理鼠标点击操作
        this.addMouseMotionListener(m); //处理鼠标移动操作

        timer = new Timer(); // 主流程控制
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (state == RUNNING) { //运行状态
                    enterAction(); //飞行物入场
                    stepAction(); //移动
                    shootAction(); //玩家飞机射击
                    bangAction(); //子弹打飞行物
                    outOfBoundsAction(); //删除越界飞行物及子弹
                    checkGameOverAction(); //检查游戏结束
                }
                repaint(); //调用paint()重绘
            }
        }, intervel, intervel);
    }

    int flying_cnt = 0; //记录飞行物个数
    public Object_Fly nextOne() { //生成新的飞行物,蜜蜂和敌机随机生成,但概率不相等
        if (isBossAlive == false && grades % 400 == 0 || grades % 410 == 0 || grades % 420 == 0 || grades % 430 == 0) {
            isBossAlive = true;
            boss_cnt++;
            if (boss_cnt == grades / 400) {
                return new Boss();
            }
            boss_cnt--;
        }
        Random r = new Random();
        int x = r.nextInt(100);
        if (x < 85)    return new enemyPlane();
        else if(x >= 85 && x < 95)   return new Bee();
        else    return new Bigbee();
    }
    public void enterAction() {
        flying_cnt++;
        if (flying_cnt % 40 == 0) {
            Object_Fly obj = nextOne();
            flyings.add(obj);
        }
    }

    public void stepAction() { //移动函数
        for (int i = 0; i < flyings.size(); i++) {
            Object_Fly fly = flyings.get(i);
            fly.Flying();
        }
        for (int i = 0; i < bullets.size(); i++) { // 子弹走一步
            Bullet b = bullets.get(i);
            b.Flying();
        }
        for (int i = 0; i < bullets2.size(); i++) { // boss子弹走一步
            Bullet2 b = bullets2.get(i);
            b.Flying();
        }
        player.Flying();
    }

    int shoot_cnt = 0; //射击次数
    public void shootAction() { //玩家飞机射击
        shoot_cnt++;
        if (shoot_cnt % 10 == 0) {
            ArrayList<Bullet> bs = player.shoot(); // 英雄打出子弹
            for (int i = 0; i < bs.size(); i++) {
                bullets.add(bs.get(i));
            }
        }

        for (int i = 0; i < flyings.size(); i++) {
            if (shoot_cnt % 60 == 0 && flyings.get(i) instanceof Enemy2) {
                ArrayList<Bullet2> bs2 = flyings.get(i).boss_shoot(); // boss打出子弹
                for (int j = 0; j < bs2.size(); j++) {
                    bullets2.add(bs2.get(j));
                }
            }
        }
    }

    //子弹与飞行物碰撞检测
    public void bangAction() {
        for (int i = 0; i < bullets.size(); i++) { // 遍历所有玩家子弹
            Bullet b = bullets.get(i);
            bang(b); // 子弹和飞行物之间的碰撞检查
        }
        for (int i = 0; i < bullets2.size(); i++) { // 遍历所有boss子弹
            Bullet2 b = bullets2.get(i);
            bang2(b); // 子弹和飞行物之间的碰撞检查
        }
    }
    //子弹和飞行物之间的碰撞检查
    public void bang(Bullet bullet) {
        int index = -1; // 击中的飞行物索引
        for (int i = 0; i < flyings.size(); i++) {
            Object_Fly obj = flyings.get(i);
            if (obj.shootBy(bullet)) { // 判断是否击中
                index = i; // 记录被击中的飞行物的索引
                bullets.remove(bullet); //删除子弹
                break;
            }
        }
        if (index != -1) { // 有击中的飞行物
            //删除被击中的飞行物
            Object_Fly one = flyings.get(index); // 记录被击中的飞行物
            if (one instanceof Enemy || one instanceof BeeAward) {
                flyings.remove(index);
                // 检查one的类型(敌人加分,奖励获取)
                if (one instanceof Enemy) { // 检查类型,是敌人,则加分
                    Enemy e = (Enemy) one; // 强制类型转换
                    grades += e.getGrades(); // 加分
                } else if (one instanceof BeeAward) { // 若为奖励,设置奖励
                    BeeAward a = (BeeAward) one;
                    int type = a.getType(); // 获取奖励类型
                    switch (type) {
                        case BeeAward.DOUBLEFIRE:
                            player.addCnt_fire(); // 设置双倍火力
                            break;
                        case BeeAward.LIFE:
                            player.addBlood(); // 设置加命
                            break;
                        case BeeAward.DOUBLEDOUBLEFIRE: //四倍火力
                            player.addCnt_fireDOUBLE();
                            break;
                        case BeeAward.DOUBLELIFE:
                            player.addDoubleBlood();
                            break;
                    }
                }
            } else if (one instanceof Enemy2) {
                if (behit_cnt == 0) boss_index = index;
                if (behit_cnt != 7 && boss_index == index)    behit_cnt++;
                else if (behit_cnt == 7){
                    flyings.remove(boss_index);
                    Enemy2 e = (Enemy2) one;
                    grades += e.getGrades(); // 加分
                    behit_cnt = 0;
                    isBossAlive = false;
                }
            }
        }
    }

    public void bang2 (Bullet2 bullet2) {
        if (player.shootByBoss(bullet2)) {
            player.subBlood();
            player.setCnt_fire(0); // 双倍火力解除
            bullets2.remove(bullet2);
        }
    }

    //删除越界飞行物及子弹
    public void outOfBoundsAction() {
        for (int i = 0; i < flyings.size(); i++) {
            if (flyings.get(i).outBackGround()) {
                flyings.remove(i); // 不越界的留着
            }
        }
        for (int i = 0; i < bullets.size(); i++) {
            if (bullets.get(i).outBackGround()) {
                bullets.remove(i);
            }
        }
        for (int i = 0; i < bullets2.size(); i++) {
            if (bullets2.get(i).outBackGround()) {
                bullets2.remove(i);
            }
        }
    }

    // 游戏结束函数
    public void checkGameOverAction() {
        if (isGameOver()) {
            state = GAME_OVER; // 改变状态
        }
    }
    //检查游戏是否结束
    public boolean isGameOver() {

        for (int i = 0; i < flyings.size(); i++) {
            int index = -1;
            Object_Fly obj = flyings.get(i);
            if (player.hit(obj)) { // 检查英雄机与飞行物是否碰撞
                player.subBlood(); // 减命
                player.setCnt_fire(0); // 双倍火力解除
                index = i; // 记录碰上的飞行物索引
            }
            if (index != -1) {
                flyings.remove(index);
            }
        }
        return player.getBlood() <= 0;
    }
    public void Music(){               //注意,java只能播放无损音质,如.wav这种格式
        try {
            File f;
            URI uri;
            URL url;
            f = new File("/Users/mayinglin/IdeaProjects/My_Game/src/Game05/fly.wav"); //绝对路径
            uri = f.toURI();
            url = uri.toURL(); //解析路径
            AudioClip aau;
            aau = Applet.newAudioClip(url);
            aau.loop();  //单曲循环
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

demo类(main函数所在)

package Game06;

import javax.swing.*;

import static Game06.Game_BackGround.LONGNESS;
import static Game06.Game_BackGround.WIDTH;

public class demo {
    public static void main(String[] args) {
        JFrame j = new JFrame("雷霆战机"); //画框
        Game_BackGround game = new Game_BackGround(); // 画板
        j.add(game); // 将面板添加到JFrame中
        j.setSize(WIDTH, LONGNESS); // 设置大小
        j.setAlwaysOnTop(true); // 设置其总在最上
        j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 默认关闭操作
        j.setLocationRelativeTo(null); // 设置窗体初始位置,null相当于默认在最中间,同时在底层调用JFrame的paint()方法
        j.setVisible(true); //显示窗口
        game.play(); // 启动执行
        game.Music();
    }
}

相关图片不在次粘贴,读者使用时,可自行添加图片,注意与源码中路径相匹配。
运行截图:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 12
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

max_tanAlpha

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值