坦克大战简单版本

 绘制框
import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Scanner;

public class TankGame03 extends JFrame {
    private MyPanel myPanel=null;  //定义一个面板属性
    private Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {
        new TankGame03();
    }
    public TankGame03(){
        System.out.println("请选择 1.重新开始游戏  2.继续上局游戏");
        String key =scanner.next();
        myPanel=new MyPanel(key);  //初始化面板
        this.add(myPanel);
        //启动面板线程
        new Thread(myPanel).start();
        this.setSize(1300,950);
        this.addKeyListener(myPanel);  //让JFrame监听myPanel的键盘事件
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

        //在JFrame 中增加相应关闭窗口的处理
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                Recorder.record(); //掉用方法,用来记录击败敌方坦克数
                System.exit(0);
            }
        });
    }

}
//绘制自己的坦克区域

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Vector;

//继承面板JPanel绘制坦克
//为了监听键盘事件,实现KeyLister接口
//为了让Panel 不停的重绘子弹,需要将 MyPanel 实现Runnable ,当做一个线程使用
@SuppressWarnings({"all"})
public class MyPanel extends JPanel implements KeyListener, Runnable {
    Hero hero = null;      //定义自己的坦克
    Vector<EnemyTank> enemyTanks = new Vector<>();  //创建一个敌人坦克的集合
    int enemySize = 6;        //定义敌人坦克的数量
    //当子弹击中坦克时,就加入爆炸效果
    Vector<Bomb> bombs = new Vector<>();  //用于存放炸弹
    Vector<Node> nodes = new Vector<Node>();  //用来恢复敌人坦克的坐标和方向

    //定义三张组合爆炸的图片
    Image image1 = null;
    Image image2 = null;
    Image image3 = null;

    public MyPanel(String key) {
        //先判断文件是否存在;
        //如果存在,正常执行游戏,如果不存在,把 key 重值为 "1"
        File file = new File(Recorder.getFilePath());
        if(file.exists()){
            nodes = Recorder.getDate();   //把敌人坦克的方向坐标信息给 nodes
        }else{
            System.out.println("没有存盘,重新开始游戏");
            key="1";
        }
        //将MyPanel的enemyTanks 设置给 Recorder 的enemyTanks
        Recorder.setEnemyTanks(enemyTanks);
        hero = new Hero(700, 100); //初始化自己的坦克
        hero.setSpeech(5);  //设置坦克移动的速度
        switch (key) {
            case "1":
                for (int i = 0; i < enemySize; i++) {
                    //初始化一个敌人的坦克
                    EnemyTank enemyTank = new EnemyTank(100 * i, 0);
                    //将enemyTanks 设置给 enemyTank
                    enemyTank.setEnemyTanks(enemyTanks);
                    //设置敌人坦克的方向
                    enemyTank.setDirect(2);
                    //启动坦克线程
                    new Thread(enemyTank).start();
                    //初始化一颗子弹
                    Shot shot = new Shot(enemyTank.getX() + 20, enemyTank.getY() + 60, enemyTank.getDirect());
                    //添加到Vector<Shot>集合中
                    enemyTank.shots.add(shot);
                    //启动子弹线程
                    new Thread(shot).start();
                    enemyTanks.add(enemyTank);
                }

                //初始化三张图片
                image1 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_1.gif"));
                image2 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_2.gif"));
                image3 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_3.gif"));
                break;
            case "2":
                for (int i = 0; i < nodes.size(); i++) {
                    //初始化一个敌人的坦克
                    Node node = nodes.get(i);
                    EnemyTank enemyTank = new EnemyTank(node.getX(),node.getY());
                    //将enemyTanks 设置给 enemyTank
                    enemyTank.setEnemyTanks(enemyTanks);
                    //设置敌人坦克的方向
                    enemyTank.setDirect(node.getDirect());
                    //启动坦克线程
                    new Thread(enemyTank).start();
                    //初始化一颗子弹
                    Shot shot = new Shot(enemyTank.getX() + 20, enemyTank.getY() + 60, enemyTank.getDirect());
                    //添加到Vector<Shot>集合中
                    enemyTank.shots.add(shot);
                    //启动子弹线程
                    new Thread(shot).start();
                    enemyTanks.add(enemyTank);
                }

                //初始化三张图片
                image1 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_1.gif"));
                image2 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_2.gif"));
                image3 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_3.gif"));
                break;


        }
    }

    //编写方法,显示我方击毁敌方坦克的信息
    public void showInfo(Graphics g) {
        //画出玩家的总成绩
        g.setColor(Color.BLACK);
        Font font = new Font("楷体", Font.BOLD, 25);
        g.setFont(font);
        g.drawString("您累积击毁敌方坦克", 1020, 30);
        drawTank(1020, 60, g, 0, 1);  //画出一个敌方坦克
        g.setColor(Color.BLACK);
        g.drawString(Recorder.getAllEnemyTankNum() + "", 1080, 100);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0, 0, 1000, 750); //绘制画坦克的区域 ,默认为黑色

        showInfo(g); // 我方击毁敌方坦克的信息

        if (hero.isLive && hero != null) {
            drawTank(hero.getX(), hero.getY(), g, hero.getDirect(), 0); //绘制自己的坦克
        }
        for (int i = 0; i < enemyTanks.size(); i++) {  //遍历vector,画出敌人的坦克
            EnemyTank enemyTank = enemyTanks.get(i);
            //当敌人坦克存活时,才画出坦克
            if (enemyTank.isLive) {
                drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirect(), 1);
                for (int j = 0; j < enemyTank.shots.size(); j++) { //遍历vector,画出敌人的子弹
                    //取出子弹
                    Shot shot = enemyTank.shots.get(j);
                    //判断子弹是否存活,在绘制
                    if (shot.isLive) {
                        g.fill3DRect(shot.x, shot.y, 3, 3, false);
                    } else {
                        //移除子弹
                        enemyTank.shots.remove(shot);
                    }
                }

            }
        }

        //画出爆炸效果
        for (int i = 0; i < bombs.size(); i++) {
            Bomb bomb = bombs.get(i);
            if (bomb.lift > 6) {
                g.drawImage(image1, bomb.x, bomb.y, 60, 60, this);
            }
            if (bomb.lift > 3) {
                g.drawImage(image2, bomb.x, bomb.y, 60, 60, this);
            } else {
                g.drawImage(image3, bomb.x, bomb.y, 60, 60, this);
            }
            //调用bomb中的lifeDown方法,减少生命周期
            bomb.liftDown();
            //当炸弹生命周期为0后,移除炸弹
            if (!bomb.isLive) {
                bombs.remove(bomb);
            }
        }
//        //画出hero的子弹
//        if (hero.shot != null && hero.shot.isLive) {
//            g.setColor(Color.yellow);
//            g.fill3DRect(hero.shot.x, hero.shot.y, 3, 3, false);
//        }

        //画出hero的多颗子弹
        for (int i = 0; i < hero.shots.size(); i++) {
            Shot shot = hero.shots.get(i);
            //画出hero的子弹
            if (shot != null && shot.isLive) {
                g.setColor(Color.yellow);
                g.fill3DRect(shot.x, shot.y, 3, 3, false);
            } else {
                //当子弹消亡后,在集合中移除子弹
                hero.shots.remove(shot);
            }
        }
    }

    //判断子弹是否击中敌人
    //在run方法中判断我方坦克是否击中敌方坦克
    public void hitShot(Shot s, Tank enemyTank) {
        //判断敌人坦克方向,确认敌人坦克范围
        //判断s 击中坦克
        switch (enemyTank.getDirect()) {
            case 0: //坦克向上
            case 2: //坦克向下
                if (s.x > enemyTank.getX() && s.x < enemyTank.getX() + 40
                        && s.y > enemyTank.getY() && s.y < enemyTank.getY() + 60) {
                    s.isLive = false;
                    enemyTank.isLive = false;
                    //移除敌人坦克
                    enemyTanks.remove(enemyTank);
                    //当我方坦克击毁敌方坦克,就对数据allEnemyTankNum ++
                    //应为enemyTank 可以是Hero ,也可以是 EnemyTank
                    if (enemyTank instanceof EnemyTank) {
                        Recorder.addAllEnemyTankNum();
                    }
                    //敌人坦克被击中后,制造出爆炸效果
                    Bomb bomb = new Bomb(enemyTank.getX(), enemyTank.getY());
                    //添加到炸弹集合中
                    bombs.add(bomb);
                }
                break;
            case 1: //坦克向右
            case 3: //坦克向左
                if (s.x > enemyTank.getX() && s.x < enemyTank.getX() + 60
                        && s.y > enemyTank.getY() && s.y < enemyTank.getY() + 40) {
                    s.isLive = false;
                    enemyTank.isLive = false;
                    //移除敌人坦克
                    enemyTanks.remove(enemyTank);
                    if (enemyTank instanceof EnemyTank) {
                        Recorder.addAllEnemyTankNum();
                    }
                    //敌人坦克被击中后,制造出爆炸效果
                    Bomb bomb = new Bomb(enemyTank.getX(), enemyTank.getY());
                    //添加到炸弹集合中
                    bombs.add(bomb);
                }
                break;
        }
    }

    //判断敌方坦克是否击中我方坦克
    public void hitHero() {
        //遍历敌方坦克
        for (int i = 0; i < enemyTanks.size(); i++) {
            EnemyTank enemyTank = enemyTanks.get(i);
            //遍历敌方坦克的子弹
            for (int j = 0; j < enemyTank.shots.size(); j++) {
                Shot shot = enemyTank.shots.get(j);
                if (shot.isLive && hero.isLive) {
                    hitShot(shot, hero);
                }
            }
        }
    }

    //判断我方子弹是否击中敌人坦克
    public void hitEnemyTank() {
        for (int i = 0; i < hero.shots.size(); i++) {  //遍历我方坦克的子弹集合
            Shot shot = hero.shots.get(i);  //取出一颗子弹
            //判断是否击中敌人坦克
            if (shot != null && shot.isLive) {
                for (int j = 0; j < enemyTanks.size(); j++) {
                    EnemyTank enemyTank = enemyTanks.get(j);
                    //调用hitShot()方法,判断是否击中敌人坦克
                    hitShot(shot, enemyTank);
                }
            }
        }
    }

    /**
     * @param x      坦克左上角横坐标
     * @param y      坦克的左上角纵坐标
     * @param g      坦克的画笔
     * @param direct 坦克的方向(上下左右)
     * @param type   坦克的类型(用颜色来区分)
     */
    //定义一个绘制坦克的方法,完成封装性
    @SuppressWarnings({"all"})
    public void drawTank(int x, int y, Graphics g, int direct, int type) {
        switch (type) { // 设置坦克的颜色类型,来区分不同类型的坦克.
            case 0:  //黄色
                g.setColor(Color.yellow);
                break;
            case 1: //青蓝色
                g.setColor(Color.cyan);
                break;
        }
        //根据坦克方向,绘制不同的坦克(上(0)右(1)下(2)左(3))
        switch (direct) {
            case 0:  //向上
                g.fill3DRect(x, y, 10, 60, false); //坦克的左轮
                g.fill3DRect(x + 30, y, 10, 60, false); //坦克的右轮
                g.fill3DRect(x + 10, y + 10, 20, 40, false); //坦克中区
                g.fillOval(x + 10, y + 20, 20, 20); //坦克的圆盖
                g.drawLine(x + 20, y + 30, x + 20, y);
                break;
            case 1:  //向右
                g.fill3DRect(x, y, 60, 10, false); //坦克的上轮
                g.fill3DRect(x, y + 30, 60, 10, false); //坦克的下轮
                g.fill3DRect(x + 10, y + 10, 40, 20, false); //坦克中区
                g.fillOval(x + 20, y + 10, 20, 20); //坦克的圆盖
                g.drawLine(x + 20, y + 20, x + 60, y + 20); //炮筒
                break;
            case 2:  //向下
                g.fill3DRect(x, y, 10, 60, false); //坦克的左轮
                g.fill3DRect(x + 30, y, 10, 60, false); //坦克的右轮
                g.fill3DRect(x + 10, y + 10, 20, 40, false); //坦克中区
                g.fillOval(x + 10, y + 20, 20, 20); //坦克的圆盖
                g.drawLine(x + 20, y + 30, x + 20, y + 60);
                break;
            case 3:  //向左
                g.fill3DRect(x, y, 60, 10, false); //坦克的上轮
                g.fill3DRect(x, y + 30, 60, 10, false); //坦克的下轮
                g.fill3DRect(x + 10, y + 10, 40, 20, false); //坦克中区
                g.fillOval(x + 20, y + 10, 20, 20); //坦克的圆盖
                g.drawLine(x + 20, y + 20, x, y + 20); //炮筒
                break;
            default:
                System.out.println("请绘制坦克");
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    //通过按压键盘,控制坦克移动的方向0(上)1(下)2(左)3(右)
    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W) { //向上移动
            hero.setDirect(0);
            //限制坦克向上移动的范围
            if (hero.getY() >= 0) {
                hero.moveUp();
            }

        } else if (e.getKeyCode() == KeyEvent.VK_D) { //向右移动
            hero.setDirect(1);
            //限制坦克向右移动的范围
            if (hero.getX() + 60 <= 1000) {
                hero.moveRight();
            }

        } else if (e.getKeyCode() == KeyEvent.VK_S) {  //向下移动
            hero.setDirect(2);
            //限制坦克向下移动的范围
            if (hero.getY() + 60 <= 750) {
                hero.moveDown();
            }

        } else if (e.getKeyCode() == KeyEvent.VK_A) {  //向左移动
            hero.setDirect(3);
            //限制坦克向左移动的范围
            if (hero.getX() >= 0) {
                hero.moveLeft();
            }
        }
        if (e.getKeyCode() == KeyEvent.VK_J) {
//            //判断子弹是否创建或者子弹消亡,按 J 重新发射一颗子弹
//            if (hero.shot == null || !hero.shot.isLive) {
            //发射子弹
//                hero.shotEnemyTank();
//            }

            hero.shotEnemyTank();
        }
        //重绘制面板
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {
        while (true) {
            try {   //让该线程每个100毫秒,重新绘制一次面板
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

//            //判断是否击中敌人坦克
//            if (hero.shot != null && hero.shot.isLive) {
//                for (int i = 0; i < enemyTanks.size(); i++) {
//                    EnemyTank enemyTank = enemyTanks.get(i);
//                    //调用hitShot()方法,判断是否击中敌人坦克
//                    hitShot(hero.shot, enemyTank);
//                }
//            }
            //判断是否击中敌方坦克
            hitEnemyTank();
            //判断是否击中我方坦克
            hitHero();
            this.repaint();
        }
    }
}


//父类Tank
public class Tank {
    private int x; //设置坦克的横坐标
    private int y; //设置坦克的纵坐标
    private int direct; //设置坦克的方向 0(上)1(下)2(左)3(右)
    private int speech = 2; //设置坦克移动的速度
    boolean isLive = true;   //判断坦克是否存活

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

    public void moveUp() {
        y -= speech;
    }

    public void moveRight() {
        x += speech;
    }

    public void moveDown() {
        y += speech;
    }

    public void moveLeft() {
        x -= speech;
    }


    public int getSpeech() {
        return speech;
    }

    public void setSpeech(int speech) {
        this.speech = speech;
    }

    public int getDirect() {
        return direct;
    }

    public void setDirect(int direct) {
        this.direct = direct;
    }

    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;
    }
}

**import java.util.Vector;

//敌人的坦克继承坦克类
public class EnemyTank extends Tank implements Runnable {
    //在敌人类使用Vector保存多个Shot
    Vector<Shot> shots = new Vector<>();
    //增加成员,EnemyTank 坦克可以得到所有坦克的成员 Vector
    Vector<EnemyTank> enemyTanks = new Vector<>();

    boolean isLive = true;

    public EnemyTank(int x, int y) {
        super(x, y);
    }

    //这里提供一个方法,可以将myPanel的成员 Vector<EnemyTank> enemyTanks = new vector<>();
    //设置到EnemyTank 的成员 enemyTanks中
    public void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        this.enemyTanks = enemyTanks;
    }

    //编写方法判断当前 enemyTank 是否和 Vector<EnemyTank> enemyTanks 中的坦克碰撞或者重叠
    public boolean isTouchEnemyTank() {
        switch (this.getDirect()) {
            case 0:
                //让当前敌人坦克和其他敌人的所有坦克比较
                for (int i = 0; i < enemyTanks.size(); i++) {
                    //取出一个坦克
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //不和自己比较
                    if (this != enemyTank) {
                        //如果敌人敌人坦克是上下方向
                        //如果敌人坦克是 上/下 的x的范围 [enemyTank.getX() , enemyTank.getX() +40]
                        //                   y的范围  [enemyTank.getY() , enemyTank.getY() + 60]
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            //自己坦克在左上角的坐标[this.getX() , this,getY() ]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                return true;
                            }
                            //自己坦克的右上角的坐标[this.getX()+40 , this,getY()]
                            if (this.getX() + 40 >= enemyTank.getX()
                                    && this.getX() + 40 <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                return true;
                            }
                        }
                        //如果敌人坦克是左右方向
                        //如果敌人坦克是 左/右 的x的范围 [enemyTank.getX() , enemyTank.get()+60]
                        //                   y的范围  [enemyTank.getY() , enemyTank.getY() + 40]
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                            //自己坦克在左上角的坐标[this.getX() , this,getY() ]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                return true;
                            }
                            //自己坦克的右上角的坐标[this.getX()+40 , this,getY()]
                            if (this.getX() + 40 >= enemyTank.getX()
                                    && this.getX() + 40 <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                return true;
                            }
                        }
                    }

                }
                break;

            case 1:
                //让当前敌人坦克和其他敌人的所有坦克比较
                for (int i = 0; i < enemyTanks.size(); i++) {
                    //取出一个坦克
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //不和自己比较
                    if (this != enemyTank) {
                        //如果敌人敌人坦克是上下方向
                        //如果敌人坦克是 上/下 的x的范围 [enemyTank.getX() , enemyTank.getX() +40]
                        //                   y的范围  [enemyTank.getY() , enemyTank.getY() + 60]
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            //自己坦克在右上角的坐标[this.getX()+60 , this.getY() ]
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX() + 60 <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                return true;
                            }
                            //自己坦克的右下角的坐标[this.getX()+60 , this,getY()+40 ]
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX() + 60 <= enemyTank.getX() + 40
                                    && this.getY() + 40 >= enemyTank.getY()
                                    && this.getY() + 40 <= enemyTank.getY() + 60) {
                                return true;
                            }
                        }
                        //如果敌人坦克是左右方向
                        //如果敌人坦克是 左/右 的x的范围 [enemyTank.getX(), enemyTank.getX() +60]
                        //                   y的范围  [enemyTank.getY() , enemyTank.getY() + 40]
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            //自己坦克在右上角的坐标[this.getX()+60 , this.getY() ]
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX() + 60 <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                return true;
                            }
                            //自己坦克的右下角的坐标[this.getX()+60 , this,getY()+40 ]
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX() + 60 <= enemyTank.getX() + 60
                                    && this.getY() + 40 >= enemyTank.getY()
                                    && this.getY() + 40 <= enemyTank.getY() + 40) {
                                return true;
                            }
                        }
                    }
                }
                break;

            case 2:
                //让当前敌人坦克和其他敌人的所有坦克比较
                for (int i = 0; i < enemyTanks.size(); i++) {
                    //取出一个坦克
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //不和自己比较
                    if (this != enemyTank) {
                        //如果敌人敌人坦克是上下方向
                        //如果敌人坦克是 上/下 的x的范围 [enemyTank.getX() , enemyTank.getX() +40]
                        //                   y的范围  [enemyTank.getY() , enemyTank.getY() + 60]
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            //自己坦克在左下角的坐标[this.getX() , this,getY()+60 ]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() + 60 >= enemyTank.getY()
                                    && this.getY() + 60 <= enemyTank.getY() + 60) {
                                return true;
                            }
                            //自己坦克的右下角的坐标[this.getX()+40 , this,getY()+60]
                            if (this.getX() + 40 >= enemyTank.getX()
                                    && this.getX() + 40 <= enemyTank.getX() + 40
                                    && this.getY() + 60 >= enemyTank.getY()
                                    && this.getY() + 60 <= enemyTank.getY() + 60) {
                                return true;
                            }
                        }
                        //如果敌人坦克是左右方向
                        //如果敌人坦克是 左/右 的x的范围 [enemyTank.getX() , enemyTank.get()+60]
                        //                   y的范围  [enemyTank.getY() , enemyTank.getY() + 40]
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                            //自己坦克在左下角的坐标[this.getX() , this,getY()+60]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() + 60 >= enemyTank.getY()
                                    && this.getY() + 60 <= enemyTank.getY() + 40) {
                                return true;
                            }
                            //自己坦克的右下角的坐标[this.getX()+40 , this,getY()+60]
                            if (this.getX() + 40 >= enemyTank.getX()
                                    && this.getX() + 40 <= enemyTank.getX() + 60
                                    && this.getY() + 60 >= enemyTank.getY()
                                    && this.getY() + 60 <= enemyTank.getY() + 40) {
                                return true;
                            }
                        }
                    }

                }
                break;
            case 3:
                //让当前敌人坦克和其他敌人的所有坦克比较
                for (int i = 0; i < enemyTanks.size(); i++) {
                    //取出一个坦克
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //不和自己比较
                    if (this != enemyTank) {
                        //如果敌人敌人坦克是上下方向
                        //如果敌人坦克是 上/下 的x的范围 [enemyTank.getX() , enemyTank.getX() +40]
                        //                   y的范围  [enemyTank.getY() , enemyTank.getY() + 60]
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            //自己坦克在左上角的坐标[this.getX(), this.getY() ]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                return true;
                            }
                            //自己坦克的左下角的坐标[this.getX() , this,getY()+40 ]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() + 40 >= enemyTank.getY()
                                    && this.getY() + 40 <= enemyTank.getY() + 60) {
                                return true;
                            }
                        }
                        //如果敌人坦克是左右方向
                        //如果敌人坦克是 左/右 的x的范围 [enemyTank.getX(), enemyTank.getX() +60]
                        //                   y的范围  [enemyTank.getY() , enemyTank.getY() + 40]
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            //自己坦克在左上角的坐标[this.getX()+ , this.getY() ]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                return true;
                            }
                            //自己坦克的左下角的坐标[this.getX() , this,getY()+40 ]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() + 40 >= enemyTank.getY()
                                    && this.getY() + 40 <= enemyTank.getY() + 40) {
                                return true;
                            }
                        }
                    }
                }
                break;
        }
        return false;
    }

    @Override
    public void run() {
        while (true) {
            //判断shots.size()是否为0,如果没有子弹了,敌方坦克还活着,就创建一颗子弹添加进去
            if (shots.size() == 0 && isLive) {
                Shot shot = null;
                switch (getDirect()) {
                    case 0:
                        shot = new Shot(getX() + 20, getY(), 0);
                        break;
                    case 1:
                        shot = new Shot(getX() + 60, getY() + 20, 1);
                        break;
                    case 2:
                        shot = new Shot(getX() + 20, getY() + 60, 2);
                        break;
                    case 3:
                        shot = new Shot(getX(), getY() + 20, 3);
                        break;
                }
                shots.add(shot);
                //启动线程
                new Thread(shot).start();
            }
            //让敌人坦克可以自己移动
            switch (getDirect()) {
                case 0:
                    for (int i = 0; i < 30; i++) {
                        if (getY() > 0 && !isTouchEnemyTank() ) {
                            moveUp();
                        }//向上
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 1:
                    for (int i = 0; i < 30; i++) {
                        if (getX() + 60 < 1000 && !isTouchEnemyTank() ) {
                            moveRight(); //向右
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 2:
                    for (int i = 0; i < 30; i++) {
                        if (getY() + 60 < 750 && !isTouchEnemyTank() ) {
                            moveDown(); //向下
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 3:
                    for (int i = 0; i < 30; i++) {
                        if (getX() > 0 && !isTouchEnemyTank() )
                            moveLeft(); //向左
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
            }
            //让坦克随机移动,随机改变坦克的方向
            setDirect((int) (Math.random() * 4));

            if (!isLive) {   //当坦克死亡跳出循环
                break;
            }
        }
    }
}**
```import java.util.Vector;

//绘制自己的坦克 继承父类
public class Hero extends Tank {
    Shot shot = null; //设置一个子弹属性
    Vector<Shot> shots = new Vector<>();  //创建一个子弹集合,让坦克可以发射多颗子弹

    public Hero(int x, int y) {
        super(x, y);
    }

    //创建 Shot 对象, 根据当前Hero对象的位置和方向来创建Shot
    public void shotEnemyTank() {
        if (shots.size() < 5) {
            switch (getDirect()) {//得到Hero对象方向
                case 0: //向上
                    shot = new Shot(getX() + 20, getY(), 0);
                    break;
                case 1: //向右
                    shot = new Shot(getX() + 60, getY() + 20, 1);
                    break;
                case 2: //向下
                    shot = new Shot(getX() + 20, getY() + 60, 2);
                    break;
                case 3: //向左
                    shot = new Shot(getX(), getY() + 20, 3);
                    break;
            }
            //把发射的子弹添加进Vector 中
            shots.add(shot);
            //启动线程
            new Thread(shot).start();
        }
    }
}

//实现爆炸效果
public class Bomb  {
    int x, y;  //炸弹的横纵坐标
    int lift = 9;  //炸弹的生命周期
    boolean isLive = true;

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

    public void liftDown() { //当生命周期结束时,爆炸结束
        if (lift > 0) {
            lift--;
        } else {
            isLive = false;
        }
    }
}


//子弹
public class Shot implements Runnable{
    int x;  //子弹的横坐标
    int y;  //子弹的纵坐标
    int direct; //子弹的方向
    int speed=4; //子弹的速度
    boolean isLive=true;  //子弹是否存活

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

    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 getDirect() {
        return direct;
    }

    public void setDirect(int direct) {
        this.direct = direct;
    }

    @Override
    public void run() {
        while (true) {
            //子弹休眠50毫秒
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //根据方向来改变x,y坐标
            switch (direct) {
                case 0://上
                    y -= speed;
                    break;
                case 1://右
                    x += speed;
                    break;
                case 2://下
                    y += speed;
                    break;
                case 3://左
                    x -= speed;
                    break;
            }
            System.out.println("x="+x+"\ty="+y);
            //判断子弹什么时候死亡,当子弹超出边界,子弹击中敌方坦克消亡
            if(x<0 || x>1000 || y<0 || y >750 || !isLive){
                System.out.println("子弹已消亡");
                isLive=false;
                break;
            }

        }
    }
}


//用来恢复数据,一个Node对象,表示一个敌人坦克的信息
public class Node {
    private int x;
    private int y;
    private int direct;

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

    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 getDirect() {
        return direct;
    }

    public void setDirect(int direct) {
        this.direct = direct;
    }
}


import java.io.*;
import java.util.Vector;

//记录相关信息和文件交互
@SuppressWarnings({"all"})
public class Recorder {
    //用于记录击毁坦克数
    private static int allEnemyTankNum = 0;
    //定义IO对象
    private static BufferedWriter bw = null;
    private static BufferedReader br = null;
    private static FileReader fr = null;
//    private static String filePath = "d:\\javaCreateFile\\myRecord.txt";
    //把记录文件保存到Src中
    private static String filePath = "src\\myRecord.txt";
    //从 MyPanel 中获取敌方坦克的信息
    private static Vector<EnemyTank> enemyTanks = new Vector<>();
    //定义一个Node对象,用于保存敌人信息
    private static Vector<Node> nodes = new Vector<>();

    public static void setNodes(Vector<Node> nodes) {
        Recorder.nodes = nodes;
    }
    //返回当前文件的路径
    public static String getFilePath() {
        return filePath;
    }

    //恢复 filePath 文件里面的数据
    public static Vector<Node> getDate() {
        try {
            br = new BufferedReader(new FileReader(filePath));
            allEnemyTankNum = Integer.parseInt(br.readLine());  //恢复击杀的总坦克数
            String line = " ";  //用来读取存放的行
            while ((line = br.readLine()) != null) {
                String[] s = line.split(" ");  //在字符串的空格处切割
                Node node = new Node(Integer.parseInt(s[0]), Integer.parseInt(s[1]), Integer.parseInt(s[2]));  //或去一个敌人坦克信息
                nodes.add(node);  //添加进入 nodes 集合
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return nodes;
    }

    public static void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        Recorder.enemyTanks = enemyTanks;
    }

    //增加一个方法,当游戏退出的时候,把 allEnemyTankNum 保存到  filePath中
    //记录剩余敌方坦克的坐标
    public static void record() {
        try {
            bw = new BufferedWriter(new FileWriter(filePath));
            bw.write(allEnemyTankNum + "" + "\r\n");
            for (int i = 0; i < enemyTanks.size(); i++) {
                EnemyTank enemyTank = enemyTanks.get(i);
                //判断敌方坦克是否存活
                if (enemyTank.isLive) {
                    //保存 enemyTank的信息
                    String re = enemyTank.getX() + " " + enemyTank.getY() + " " + enemyTank.getDirect() + " ";
                    bw.write(re + "\r\n");
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static int getAllEnemyTankNum() {
        return allEnemyTankNum;
    }

    public static void setAllEnemyTankNum(int allEnemyTankNum) {
        Recorder.allEnemyTankNum = allEnemyTankNum;
    }

    //当我发击毁一个敌人坦克
    public static void addAllEnemyTankNum() {
        Recorder.allEnemyTankNum++;
    }
}

上一篇:BeyondCorps


下一篇:MATIC逆势上扬 Polygon生态露脸