【java项目】飞机大战

项目-飞机大战

窗口的创建

package GUI.project1;

import javax.swing.*;

public class GameWin extends JFrame {
    
    
    public void launch(){
    
    
        //设置窗口是否可见
        this.setVisible(true);
        //设置窗口大小
        this.setSize(600,600);
        //设置窗口位置
        this.setLocationRelativeTo(null);
        //设置窗口标题
        this.setTitle("aircraft battle");
    }

    public static void main(String[] args) {
    
    
        GameWin gameWin = new GameWin();
        gameWin.launch();

    }
}

背景图片的添加/点击事件的启动

GameUtil.java

package project1;

import java.awt.*;

public class GameUtils {
    
    
    //背景图片
    public static Image bgImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\bg.jpg");
    //boss图片
    public static Image bossImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\boss.png");
    //爆炸图片
    public static Image explodeImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\explode\\e6.gif");


}

GameWin.java

package project1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class GameWin extends JFrame {
    
    
    //游戏状态 0未开始 1游戏中 2暂停 3通关失败 4通关成功
    static int state = 0;
    public void launch(){
    
    
        //设置窗口是否可见
        this.setVisible(true);
        //设置窗口大小
        this.setSize(600,600);
        //设置窗口位置
        this.setLocationRelativeTo(null);
        //设置窗口标题
        this.setTitle("aircraft battle");

        this.addMouseListener(new MouseAdapter() {
    
    
            @Override
            public void mouseClicked(MouseEvent e) {
    
    
                //e.getButton()==1:按下鼠标左键
                if (e.getButton()==1 && state==0){
    
    
                    state = 1;
                    repaint();
                }
            }
        });
    }

    @Override
    public void paint(Graphics g) {
    
    
        if (state == 0){
    
    
            g.drawImage(GameUtils.bgImg,0,0,this);
            g.drawImage(GameUtils.bossImg,220,120,this);
            g.drawImage(GameUtils.explodeImg,270,350,this);
            g.setColor(Color.yellow);
            g.setFont(new Font("仿宋",Font.BOLD,40));
            g.drawString("点击开始游戏",180,300);
        }
        if (state == 1){
    
    
            g.drawImage(GameUtils.bgImg,0,0,this);
            g.drawImage(GameUtils.bossImg,220,120,this);
            g.drawImage(GameUtils.explodeImg,270,350,this);
            g.setColor(Color.yellow);
            g.setFont(new Font("仿宋",Font.BOLD,40));
            g.drawString("开始游戏了",180,300);
        }
    }

    public static void main(String[] args) {
    
    
        GameWin gameWin = new GameWin();
        gameWin.launch();

    }
}

游戏物体父类的编写

GameObj.java

package project1;

import java.awt.*;

public class GameObj {
    
    
    Image img;
    int x;
    int y;
    int width;
    int height;
    double speed;
    GameWin frame;

    public GameObj() {
    
    
    }

    public GameObj(Image img, int x, int y, double speed) {
    
    
        this.img = img;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }

    public GameObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
    
    
        this.img = img;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.speed = speed;
        this.frame = frame;
    }

    public Image getImg() {
    
    
        return img;
    }

    public void setImg(Image 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 double getSpeed() {
    
    
        return speed;
    }

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

    public GameWin getFrame() {
    
    
        return frame;
    }

    public void setFrame(GameWin frame) {
    
    
        this.frame = frame;
    }
    public void paintSelf(Graphics gImage){
    
    
        gImage.drawImage(img,x,y,null);
    }
    public Rectangle getRec(){
    
    
        return new Rectangle(x,y,width,height);
    }
}

背景的移动

package project1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class GameWin extends JFrame {
    
    
    //游戏状态 0未开始 1游戏中 2暂停 3通关失败 4通关成功
    static int state = 0;
    //背景图对象
    BgObj bgObj = new BgObj(GameUtils.bgImg,0,-2000,2);
    public void launch(){
    
    
        //设置窗口是否可见
        this.setVisible(true);
        //设置窗口大小
        this.setSize(600,600);
        //设置窗口位置
        this.setLocationRelativeTo(null);
        //设置窗口标题
        this.setTitle("aircraft battle");

        this.addMouseListener(new MouseAdapter() {
    
    
            @Override
            public void mouseClicked(MouseEvent e) {
    
    
                //e.getButton()==1:按下鼠标左键
                if (e.getButton()==1 && state==0){
    
    
                    state = 1;
                    repaint();
                }
            }
        });
        while (true){
    
    
            repaint();
            try{
    
    
                Thread.sleep(25);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
        }
    }

    @Override
    public void paint(Graphics g) {
    
    
        if (state == 0){
    
    
            g.drawImage(GameUtils.bgImg,0,0,this);
            g.drawImage(GameUtils.bossImg,220,120,this);
            g.drawImage(GameUtils.explodeImg,270,350,this);
            g.setColor(Color.yellow);
            g.setFont(new Font("仿宋",Font.BOLD,40));
            g.drawString("点击开始游戏",180,300);
        }
        if (state == 1){
    
    
            bgObj.paintSelf(g);
        }
    }

    public static void main(String[] args) {
    
    
        GameWin gameWin = new GameWin();
        gameWin.launch();

    }
}

双缓存技术–解决文字闪动

修改GameWin.java

package project1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class GameWin extends JFrame {
    
    
    //游戏状态 0未开始 1游戏中 2暂停 3通关失败 4通关成功
    static int state = 0;
    Image offScreenImage = null;
    int width = 600;
    int height = 600;
    //背景图对象
    BgObj bgObj = new BgObj(GameUtils.bgImg,0,-2000,2);
    public void launch(){
    
    
        //设置窗口是否可见
        this.setVisible(true);
        //设置窗口大小
        this.setSize(width,height);
        //设置窗口位置
        this.setLocationRelativeTo(null);
        //设置窗口标题
        this.setTitle("aircraft battle");

        this.addMouseListener(new MouseAdapter() {
    
    
            @Override
            public void mouseClicked(MouseEvent e) {
    
    
                //e.getButton()==1:按下鼠标左键
                if (e.getButton()==1 && state==0){
    
    
                    state = 1;
                    repaint();
                }
            }
        });
        while (true){
    
    
            repaint();
            try{
    
    
                Thread.sleep(25);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
        }
    }

    @Override
    public void paint(Graphics g) {
    
    
        if (offScreenImage == null){
    
    
            offScreenImage = createImage(width,height);
        }
        Graphics gImage = offScreenImage.getGraphics();
        gImage.fillRect(0,0,width,height);
        if (state == 0){
    
    
            gImage.drawImage(GameUtils.bgImg,0,0,this);
            gImage.drawImage(GameUtils.bossImg,220,120,this);
            gImage.drawImage(GameUtils.explodeImg,270,350,this);
            gImage.setColor(Color.yellow);
            gImage.setFont(new Font("仿宋",Font.BOLD,40));
            gImage.drawString("点击开始游戏",180,300);
        }
        if (state == 1){
    
    
            bgObj.paintSelf(gImage);
        }
        g.drawImage(offScreenImage,0,0,null);
    }

    public static void main(String[] args) {
    
    
        GameWin gameWin = new GameWin();
        gameWin.launch();

    }
}

背景图片循环出现

在BgObj.java中设置临界条件
在这里插入图片描述

我方战斗机的添加和鼠标控制

PlaneObj.java

package project1;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class PlaneObj extends GameObj{
    
    
    @Override
    public Image getImg() {
    
    
        return super.getImg();
    }

    public PlaneObj() {
    
    
        super();
    }

    public PlaneObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
    
    
        super(img, x, y, width, height, speed, frame);

        this.frame.addMouseListener(new MouseAdapter() {
    
    
            @Override
            public void mouseMoved(MouseEvent e) {
    
    
                PlaneObj.super.x = e.getX() - 11;
                PlaneObj.super.y = e.getY() - 16;
            }
        });
    }

    @Override
    public void paintSelf(Graphics gImage) {
    
    
        super.paintSelf(gImage);
    }

    @Override
    public Rectangle getRec() {
    
    
        return super.getRec();
    }
}

bug:飞机没动静

添加首颗子弹

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

批量添加子弹

GameWin.java

在这里插入代码片package project1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class GameWin extends JFrame {
    
    
    //游戏状态 0未开始 1游戏中 2暂停 3通关失败 4通关成功
    static int state = 0;
    Image offScreenImage = null;
    int width = 600;
    int height = 600;
    //背景图对象
    BgObj bgObj = new BgObj(GameUtils.bgImg,0,-2000,2);
    //飞机对象
    PlaneObj planeObj = new PlaneObj(GameUtils.planeImg,290,550,20,30,0,this);

    //游戏的重绘次数
    int count = 1;
    public void launch(){
    
    
        //设置窗口是否可见
        this.setVisible(true);
        //设置窗口大小
        this.setSize(width,height);
        //设置窗口位置
        this.setLocationRelativeTo(null);
        //设置窗口标题
        this.setTitle("aircraft battle");

        GameUtils.gameObjList.add(bgObj);
        GameUtils.gameObjList.add(planeObj);

        this.addMouseListener(new MouseAdapter() {
    
    
            @Override
            public void mouseClicked(MouseEvent e) {
    
    
                //e.getButton()==1:按下鼠标左键
                if (e.getButton()==1 && state==0){
    
    
                    state = 1;
                    repaint();
                }
            }
        });

        while (true){
    
    
            if (state == 1){
    
    
                createObj();
                repaint();
            }
            repaint();
            try{
    
    
                Thread.sleep(25);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
        }
    }


    @Override
    public void paint(Graphics g) {
    
    
        if (offScreenImage == null){
    
    
            offScreenImage = createImage(width,height);
        }
        Graphics gImage = offScreenImage.getGraphics();
        gImage.fillRect(0,0,width,height);
        if (state == 0){
    
    
            gImage.drawImage(GameUtils.bgImg,0,0,this);
            gImage.drawImage(GameUtils.bossImg,220,120,this);
            gImage.drawImage(GameUtils.explodeImg,270,350,this);
            gImage.setColor(Color.yellow);
            gImage.setFont(new Font("仿宋",Font.BOLD,40));
            gImage.drawString("点击开始游戏",180,300);
        }
        if (state == 1){
    
    
            //运行中
            for (int i =0;i<GameUtils.gameObjList.size();i++){
    
    
                GameUtils.gameObjList.get(i).paintSelf(gImage);
            }

        }
        g.drawImage(offScreenImage,0,0,null);
        count++;
    }

    void createObj(){
    
    
        if (count % 15 == 0){
    
    
            //我方子弹
            GameUtils.shellObjList.add(new ShellObj(GameUtils.shellImg,planeObj.getX()+3,planeObj.getY()-16,14,29,5,this));
            //通过索引把shellObjList的最后一个元素,加入到gameObjList中 ???
            GameUtils.gameObjList.add(GameUtils.shellObjList.get(GameUtils.shellObjList.size()-1));
        }

    }

    public static void main(String[] args) {
    
    
        GameWin gameWin = new GameWin();
        gameWin.launch();

    }
}

GameUtils.java

package project1;

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

public class GameUtils {
    
    
    //背景图片
    public static Image bgImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\bg.jpg");
    //boss图片
    public static Image bossImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\boss.png");
    //爆炸图片
    public static Image explodeImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\explode\\e6.gif");
    //我方战斗机图片
    public static Image planeImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\plane.png");
    //我方子弹图片
    public static Image shellImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\shell.png");

    //所有游戏物体的集合
    public static List<GameObj> gameObjList = new ArrayList<>();
    //我方子弹的集合
    public static List<ShellObj> shellObjList = new ArrayList<>();

}

问题:
1.GUI中添加集合元素的用法
2.gameObjList的意义
解答:看下一节课的代码

敌方飞机的批量添加功能

1.创建EnemyObj.java

package project1;

import java.awt.*;

public class EnemyObj extends GameObj{
    
    
    public EnemyObj() {
    
    
        super();
    }

    public EnemyObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
    
    
        super(img, x, y, width, height, speed, frame);
    }

    @Override
    public void paintSelf(Graphics gImage) {
    
    
        super.paintSelf(gImage);
        y += speed;
    }

    @Override
    public Rectangle getRec() {
    
    
        return super.getRec();
    }
}

2.在GameUtil.java中添加敌方图片和集合

package project1;

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

public class GameUtils {
    
    
    //背景图片
    public static Image bgImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\bg.jpg");
    //boss图片
    public static Image bossImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\boss.png");
    //爆炸图片
    public static Image explodeImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\explode\\e6.gif");
    //我方战斗机图片
    public static Image planeImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\plane.png");
    //我方子弹图片
    public static Image shellImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\shell.png");
    //敌机图片
    public static Image eneImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\enemy.png");


    //所有游戏物体的集合
    public static List<GameObj> gameObjList = new ArrayList<>();
    //我方子弹的集合
    public static List<ShellObj> shellObjList = new ArrayList<>();
    //敌机的集合
    public static List<EnemyObj> enemyObjList = new ArrayList<>();
}

3.在GameWin.java中像上一步操作中一样,添加enemyObjList和gameObjList中

x值的设置:可看视频讲解

void createObj(){
    
    
        if (count % 15 == 0){
    
    
            //我方子弹
            GameUtils.shellObjList.add(new ShellObj(GameUtils.shellImg,planeObj.getX()+3,planeObj.getY()-16,14,29,5,this));
            //通过索引把shellObjList的最后一个元素,加入到gameObjList中 ???
            GameUtils.gameObjList.add(GameUtils.shellObjList.get(GameUtils.shellObjList.size()-1));
        }
        if (count % 15 == 0){
    
    
            GameUtils.enemyObjList.add(new EnemyObj(GameUtils.eneImg,(int) (Math.random()*12)*50,0,49,36,5,this));
            GameUtils.gameObjList.add(GameUtils.enemyObjList.get(GameUtils.enemyObjList.size()-1));
        }
    }

在这里插入图片描述

我方子弹与敌方飞机的碰撞检测

EnemyObj.java中修改

public void paintSelf(Graphics gImage) {
    
    
        super.paintSelf(gImage);
        y += speed;
        for (ShellObj shellObj:GameUtils.shellObjList){
    
    
            if (this.getRec().intersects(shellObj.getRec())){
    
    
                System.out.println("碰撞了");
            }
        }
    }

我方子弹与敌方飞机碰撞时的处理

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

我方子弹与敌方飞机碰撞时碰撞坠毁

EnemyObj.java
在这里插入图片描述
GameWin.java
在这里插入图片描述

因为鼠标移动的那个bug,所以没办法达到碰撞坠毁的效果

敌方boss的添加和移动

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

为敌方boss添加子弹

1.新建BulletObj类

package project1;

import java.awt.*;

public class BulletObj extends GameObj{
    
    
    public BulletObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
    
    
        super(img, x, y, width, height, speed, frame);
    }

    @Override
    public void paintSelf(Graphics gImage) {
    
    
        super.paintSelf(gImage);
    }

    @Override
    public Rectangle getRec() {
    
    
        return super.getRec();
    }
}

2.在GameUtils.java中添加图片和集合

//敌方子弹的集合
    public static Image bulletImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\bullet.png");

//敌方子弹的集合
    public static List<BulletObj> bulletObjList = new ArrayList<>();

3.在GameWin.java中加入集合类中

if (count % 15 == 0){
    
    
            GameUtils.bulletObjList.add(new BulletObj(GameUtils.bulletImg,bossObj.getX()+76,bossObj.getY()+85,15,25,5,this));
            GameUtils.gameObjList.add(GameUtils.bulletObjList.get(GameUtils.bulletObjList.size()-1));
        }

4.再回BulletObj,java中添加子弹速度

@Override
    public void paintSelf(Graphics gImage) {
    
    
        super.paintSelf(gImage);
        y += speed;
    }

我方飞机和敌方飞机子弹碰撞检测

在BulletObj.java中设置state为3

@Override
    public void paintSelf(Graphics gImage) {
    
    
        super.paintSelf(gImage);
        y += speed;
        //敌方子弹于我方飞机的碰撞检测
        if (this.getRec().intersects(this.frame.planeObj.getRec())){
    
    
            GameWin.state = 3;
        }
    }

我方飞机与敌方boss的碰撞检测

在PlaneObj.java中设置state为3
在这里插入图片描述

计分板的编写

1.在GameWin.java中定义游戏得分变量
在这里插入图片描述
2.在 GameUtil.java中绘制字符串工具类
在这里插入图片描述
3.在主类中替换相同代码
在这里插入图片描述
4.在主类中绘制计分板
在这里插入图片描述
5.在EnemyObj.java中检测碰撞时score++
在这里插入图片描述

设定游戏规则

当敌机出现100架后,Boss才出现

1.在GameWin.java中设定
在这里插入图片描述
2.GameWin.java中,生成一辆敌机,enemyCount就++
在这里插入图片描述
3.增加if判断条件,判断bossobj的生成条件
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
4.增加boss的子弹生成判断
在这里插入图片描述
5.bug:报错 --去PlaneObj.java中的碰撞检测加上判断
在这里插入图片描述

为敌方boss添加生命值

1.设定生命值
在这里插入图片描述

2.设置敌方飞机和我方子弹的碰撞检测
在这里插入图片描述
3.去GameWin.java中添加通关提示语
在这里插入图片描述

游戏的暂停功能

在这里插入图片描述

子弹和敌机的越界消失

因为子弹和敌机在窗口外依然被绘制,所以会降低游戏运行速度

1.子弹跑出窗口后的处理
在这里插入图片描述

2.敌机子弹消失判断
在这里插入图片描述
3.在GameWin.java中观察长度
在这里插入图片描述

输出:最后稳定
在这里插入图片描述

为敌方小飞机添加爆炸效果

1.ExplodeObj类的实现
在这里插入图片描述
2.GameObj.java中构造器的添加
在这里插入图片描述
3.ExplodeObj类中构造器的继承
在这里插入图片描述

4.将爆炸图放在图片数组中
在这里插入图片描述
2.添加爆炸图片的判断
在这里插入图片描述
在这里插入图片描述
3.在主类中添加爆炸集合类
在这里插入图片描述
4.在EnemyObj.java中绑定爆炸类
在这里插入图片描述
5.从GameWin.java添加到gameobjList中
在这里插入图片描述
6.在EnemyObj.java添加到removeList中
在这里插入图片描述

为敌方boss添加生命血条

在BossObj.java中添加
在这里插入图片描述

成品

GameWin

package project1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class GameWin extends JFrame {
    
    
    //游戏状态 0未开始 1游戏中 2暂停 3通关失败 4通关成功
    static int state = 0;
    //游戏得分
    public static int score = 0;
    Image offScreenImage = null;

    //背景图对象
    BgObj bgObj = new BgObj(GameUtils.bgImg,0,-2000,2);
    //我方飞机对象
    public PlaneObj planeObj = new PlaneObj(GameUtils.planeImg,290,550,20,30,0,this);
    //敌方Boss对象
    public BossObj bossObj = null;
    int width = 600;
    int height = 600;
    //游戏的重绘次数
    int count = 1;
    //低级出现的数量
    int enemyCount = 0;


    public void launch(){
    
    
        //设置窗口是否可见
        this.setVisible(true);
        //设置窗口大小
        this.setSize(width,height);
        //设置窗口位置
        this.setLocationRelativeTo(null);
        //设置窗口标题
        this.setTitle("aircraft battle");

        GameUtils.gameObjList.add(bgObj);
        GameUtils.gameObjList.add(planeObj);

        this.addMouseListener(new MouseAdapter() {
    
    
            @Override
            public void mouseClicked(MouseEvent e) {
    
    
                //e.getButton()==1:按下鼠标左键
                if (e.getButton()==1 && state==0){
    
    
                    state = 1;
                }
            }
        });

        //暂停
        this.addKeyListener(new KeyAdapter() {
    
    
            @Override
            public void keyPressed(KeyEvent e) {
    
    
                if (e.getKeyCode() == 32){
    
    
                    switch (state){
    
    
                        case 1:
                            state = 2;
                            break;
                        case 2:
                            state = 1;
                            break;
                        default:
                    }
                }
            }
        });

        while (true){
    
    
            if (state == 1){
    
    
                createObj();
                repaint();
            }
            repaint();
            try{
    
    
                Thread.sleep(25);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
        }
    }


    @Override
    public void paint(Graphics g) {
    
    
        if (offScreenImage == null){
    
    
            offScreenImage = createImage(width,height);
        }
        Graphics gImage = offScreenImage.getGraphics();
        gImage.fillRect(0,0,width,height);
        if (state == 0){
    
    
            gImage.drawImage(GameUtils.bgImg,0,0,this);
            gImage.drawImage(GameUtils.bossImg,220,120,this);
            gImage.drawImage(GameUtils.explodeImg,270,350,this);
            GameUtils.drawWord(gImage,"点击开始游戏",Color.yellow,40,180,300);
        }
        if (state == 1){
    
    
            //顺序很重要:先添加,再循环,最后删除
            GameUtils.gameObjList.addAll(GameUtils.explodeObjList);
            for (int i =0;i<GameUtils.gameObjList.size();i++){
    
    
                GameUtils.gameObjList.get(i).paintSelf(gImage);
            }
            GameUtils.gameObjList.removeAll(GameUtils.removeList);
        }
        if (state == 3){
    
    
            //失败
            gImage.drawImage(GameUtils.explodeImg,planeObj.getX()-35,planeObj.getY()-50,this);
            GameUtils.drawWord(gImage,"GAME OVER",Color.RED,50,180,300);
        }
        if (state == 4){
    
    
            //通关
            gImage.drawImage(GameUtils.explodeImg,bossObj.getX()+30,bossObj.getY(),this);
            GameUtils.drawWord(gImage,"游戏通关",Color.green,50,190,300);
        }
        GameUtils.drawWord(gImage,score+"分",Color.GREEN,40,30,100);

        g.drawImage(offScreenImage,0,0,null);
        count++;
//        System.out.println(GameUtils.gameObjList.size());
    }

    void createObj(){
    
    
        if (count % 15 == 0){
    
    
            //我方子弹
            GameUtils.shellObjList.add(new ShellObj(GameUtils.shellImg,planeObj.getX()+3,planeObj.getY()-16,14,29,5,this));
            //通过索引把shellObjList的最后一个元素,加入到gameObjList中 ???
            GameUtils.gameObjList.add(GameUtils.shellObjList.get(GameUtils.shellObjList.size()-1));
        }
        if (count % 15 == 0){
    
    
            GameUtils.enemyObjList.add(new EnemyObj(GameUtils.eneImg,(int) (Math.random()*12)*50,0,49,36,5,this));
            GameUtils.gameObjList.add(GameUtils.enemyObjList.get(GameUtils.enemyObjList.size()-1));
            enemyCount++;
        }
        if (count % 15 == 0 && bossObj != null){
    
    
            GameUtils.bulletObjList.add(new BulletObj(GameUtils.bulletImg,bossObj.getX()+76,bossObj.getY()+85,15,25,5,this));
            GameUtils.gameObjList.add(GameUtils.bulletObjList.get(GameUtils.bulletObjList.size()-1));
        }
        if (enemyCount > 100 && bossObj == null){
    
    
            bossObj = new BossObj(GameUtils.bossImg,250,35,155,100,5,this);
            GameUtils.gameObjList.add(bossObj);
        }
    }

    public static void main(String[] args) {
    
    
        GameWin gameWin = new GameWin();
        gameWin.launch();

    }
}

GameUtils

package project1;

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

public class GameUtils {
    
    
    //背景图片
    public static Image bgImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\aircraft battle\\imgs\\bg.jpg");
    //boss图片
    public static Image bossImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\aircraft battle\\imgs\\boss.png");
    //爆炸图片
    public static Image explodeImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\aircraft battle\\imgs\\explode\\e6.gif");
    //我方战斗机图片
    public static Image planeImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\aircraft battle\\imgs\\plane.png");
    //我方子弹图片
    public static Image shellImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\aircraft battle\\imgs\\shell.png");
    //敌机图片
    public static Image eneImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\aircraft battle\\imgs\\enemy.png");
    //敌方子弹的集合
    public static Image bulletImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\aircraft battle\\imgs\\bullet.png");

    //所有游戏物体的集合
    public static List<GameObj> gameObjList = new ArrayList<>();
    //我方子弹的集合
    public static List<ShellObj> shellObjList = new ArrayList<>();
    //敌机的集合
    public static List<EnemyObj> enemyObjList = new ArrayList<>();
    //要删除元素的集合
    public static List<GameObj> removeList = new ArrayList<>();
    //敌方子弹的集合
    public static List<BulletObj> bulletObjList = new ArrayList<>();
    //爆炸的集合
    public static List<ExplodeObj> explodeObjList = new ArrayList<>();

    //绘制字符串工具类
    public static void drawWord(Graphics gImage,String str,Color color,int size,int x,int y){
    
    
        gImage.setColor(color);
        gImage.setFont(new Font("仿宋",Font.BOLD,size));
        gImage.drawString(str,x,y);
    }

}

GameObj

package project1;

import java.awt.*;

public class GameObj {
    
    
    Image img;
    int x;
    int y;
    int width;
    int height;
    double speed;
    GameWin frame;

    public GameObj() {
    
    
    }

    public GameObj(Image img, int x, int y, double speed) {
    
    
        this.img = img;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }

    public GameObj(int x, int y) {
    
    
        this.x = x;
        this.y = y;
    }

    public GameObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
    
    
        this.img = img;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.speed = speed;
        this.frame = frame;
    }

    public Image getImg() {
    
    
        return img;
    }

    public void setImg(Image 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 double getSpeed() {
    
    
        return speed;
    }

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

    public GameWin getFrame() {
    
    
        return frame;
    }

    public void setFrame(GameWin frame) {
    
    
        this.frame = frame;
    }
    public void paintSelf(Graphics gImage){
    
    
        gImage.drawImage(img,x,y,null);
    }
    public Rectangle getRec(){
    
    
        return new Rectangle(x,y,width,height);
    }
}

BgObj

package project1;

import java.awt.*;

public class BgObj extends GameObj{
    
    
    public BgObj() {
    
    
    }

    public BgObj(Image img, int x, int y, double speed) {
    
    
        super(img, x, y, speed);
    }

    @Override
    public void paintSelf(Graphics gImage) {
    
    
        super.paintSelf(gImage);
        y += speed;
        if (y>=0){
    
    
            y -= 2000;
        }
    }
}

BossObj

package project1;

import java.awt.*;

public class BossObj extends GameObj{
    
    
    int life = 10;
    public BossObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
    
    
        super(img, x, y, width, height, speed, frame);
    }

    @Override
    public void paintSelf(Graphics gImage) {
    
    
        super.paintSelf(gImage);
        if (x>550 || x<-50){
    
    
            speed = -speed;
        }
        x += speed;
        for (ShellObj shellObj : GameUtils.shellObjList){
    
    
            if (this.getRec().intersects(shellObj.getRec())){
    
    
                //将子弹移除
                shellObj.setX(-100);
                shellObj.setY(100);
                //添加到removeList中
                GameUtils.removeList.add(shellObj);
                //生命值减少
                life--;
            }
            if (life <= 0){
    
    
                GameWin.state = 4;
            }
        }
        //血条背景
        gImage.setColor(Color.red);
        //血条的绘制
        gImage.fillRect(20,40,life*100/10,10);
    }

    @Override
    public Rectangle getRec() {
    
    
        return super.getRec();
    }
}

BulletObj

package project1;

import java.awt.*;

public class BulletObj extends GameObj{
    
    
    public BulletObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
    
    
        super(img, x, y, width, height, speed, frame);
    }

    @Override
    public void paintSelf(Graphics gImage) {
    
    
        super.paintSelf(gImage);
        y += speed;
        //敌方子弹于我方飞机的碰撞检测
        if (this.getRec().intersects(this.frame.planeObj.getRec())){
    
    
            GameWin.state = 3;
        }
    }

    @Override
    public Rectangle getRec() {
    
    
        return super.getRec();
    }
}

EnemyObj

package project1;

import java.awt.*;

public class EnemyObj extends GameObj{
    
    
    public EnemyObj() {
    
    
        super();
    }

    public EnemyObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
    
    
        super(img, x, y, width, height, speed, frame);
    }

    @Override
    public void paintSelf(Graphics gImage) {
    
    
        super.paintSelf(gImage);
        y += speed;
        //敌方与我方碰撞处理
        if (this.getRec().intersects(this.frame.planeObj.getRec())){
    
    
            GameWin.state = 3;
        }
        //敌方子弹的越界消失
        if (y>600){
    
    
            this.x = -300;
            this.y = 300;
            GameUtils.removeList.add(this);
        }
        //子弹碰撞处理
        for (ShellObj shellObj:GameUtils.shellObjList){
    
    
            if (this.getRec().intersects(shellObj.getRec())){
    
    
                ExplodeObj explodeObj = new ExplodeObj(x,y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);
                //这段代码没看懂,去掉好像也不影响 --移动位置不能重复,不然会导致碰撞检测的发生
                shellObj.setX(-100);
                shellObj.setY(100);
                this.x = -200;
                this.y = 200;
                GameUtils.removeList.add(shellObj);
                GameUtils.removeList.add(this);
                GameWin.score++;
            }
        }
    }

    @Override
    public Rectangle getRec() {
    
    
        return super.getRec();
    }
}

ExplodeObj

package project1;

import java.awt.*;

public class ExplodeObj extends GameObj{
    
    
    static Image[] pic = new Image[16];

    int explodeCount = 0;

    static {
    
    
        for (int i = 0; i < pic.length; i++) {
    
    
            pic[i] = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\explode\\e"+(i+1)+".gif");
        }
    }
    public ExplodeObj(int x, int y) {
    
    
        super(x, y);
    }

    @Override
    public void paintSelf(Graphics gImage) {
    
    
        //如果explodeCount小于数组长度
        if (explodeCount < 16){
    
    
            img = pic[explodeCount];
            super.paintSelf(gImage);
            explodeCount++;
        }
    }
}

PlaneObj

package project1;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class PlaneObj extends GameObj{
    
    
    @Override
    public Image getImg() {
    
    
        return super.getImg();
    }

    public PlaneObj() {
    
    
        super();
    }

    public PlaneObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
    
    
        super(img, x, y, width, height, speed, frame);

        frame.addMouseListener(new MouseAdapter() {
    
    
            @Override
            public void mouseMoved(MouseEvent e) {
    
    
                PlaneObj.super.x = e.getX();
                PlaneObj.super.y = e.getY();
            }
        });
    }


    @Override
    public void paintSelf(Graphics gImage) {
    
    
        super.paintSelf(gImage);
        //我方飞机与敌方boss的碰撞检测
        if (this.frame.bossObj != null && this.getRec().intersects(this.frame.bossObj.getRec())){
    
    
            GameWin.state = 3;
        }
    }

    @Override
    public Rectangle getRec() {
    
    
        return super.getRec();
    }
}

ShellObj

package project1;

import java.awt.*;

public class ShellObj extends GameObj{
    
    
    public ShellObj() {
    
    
        super();
    }

    public ShellObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
    
    
        super(img, x, y, width, height, speed, frame);
    }

    @Override
    public Image getImg() {
    
    
        return super.getImg();
    }

    @Override
    public void paintSelf(Graphics gImage) {
    
    
        super.paintSelf(gImage);
        y -= speed;
        //我方子弹的越界消失
        if (y<0){
    
    
            this.x = -100;
            this.y = 100;
            GameUtils.removeList.add(this);
        }
    }

    @Override
    public Rectangle getRec() {
    
    
        return super.getRec();
    }
}

猜你喜欢

转载自blog.csdn.net/m0_65431212/article/details/128514635