一聚教程网:一个值得你收藏的教程网站

热门教程

JAVA实现经典游戏坦克大战代码示例

时间:2022-06-29 01:56:09 编辑:袖梨 来源:一聚教程网

本篇文章小编给大家分享一下JAVA实现经典游戏坦克大战代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

《坦克大战》游戏以坦克战斗为主题,用java语言实现,采用了swing技术进行了界面化处理,设计思路用了面向对象思想。

主要需求

可以生成不同的地图,消灭地图上的所有坦克为胜利;可以设置关卡地图,不断增加难度。

主要设计

1、要有难度关卡:第一关,第二关,第三关,第四关,第五关;第一关地图最简单,第五关地图最难;

2、坦克要有血条,打多次才会死

3、地图要我有我方坦克四辆(相当于4条命)和多辆敌方坦克

4、我方坦克的炮弹数量是固定的,设置为500

5、地图右边显示基本信息

6、地图上要在砖块,铁块,河流

功能截图

游戏开始页面

代码实现

窗口布局

public class GameFrame extends JFrame {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -1176914786963603304L;


    public GameFrame() {
        super();


        this.setSize(800, 700);
        this.setTitle("坦克大战");
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setIconImage(TankGameImages.myTankImg[0]);

        // 显示器屏幕大小
        Dimension screenSizeInfo = Toolkit.getDefaultToolkit().getScreenSize();
        int leftTopX = ((int) screenSizeInfo.getWidth() - this.getWidth()) / 2;
        int leftTopY = ((int) screenSizeInfo.getHeight() - this.getHeight()) / 2;

        // 设置显示的位置在屏幕中间
        this.setLocation(leftTopX, leftTopY);
    }

}

地图渲染核心算法

@Service
public class PaintService {
    @Autowired
    private GameContext context;
    private Brick rightBrick = new Brick(700, 50);
    private Iron rightIron = new Iron(700, 50);
    private Water rightWater = new Water(700, 50);

    /**
     * 画出东西(包括坦克、障碍物。。)
     *
     * @param g     Graphics
     * @param stuff 东西对象
     * @param panel 被画的那个面板
     */
    public void drawStuff(Graphics g, Stuff stuff, JPanel panel) {
        switch (stuff.getType()) {
            //枚举的switch,有意思,不需要+StuffTypeEnum.TANK
            case TANK:
                Tank tank = (Tank) stuff;
                switch (stuff.getDirect()) { // 判断所朝的方向
                    case NORTH:
                        this.drawNorth(g, tank, panel);
                        break;
                    case SOUTH:
                        this.drawSouth(g, tank, panel);
                        break;
                    case WEST:
                        this.drawWest(g, tank, panel);
                        break;
                    case EAST:
                        this.drawEast(g, tank, panel);
                        break;
                }
                break;
            case BRICK:

//                g.setColor(new Color(216, 90, 49));
//                g.fill3DRect(stuff.getX() - 20, stuff.getY() - 20, 40, 40, false);

                g.drawImage(TankGameImages.stuffImg[StuffTypeEnum.BRICK.getKey()],
                        stuff.getX() - 10, stuff.getY() - 10, 20, 20, panel);
                break;
            case IRON:

//                g.setColor(new Color(225, 225, 225));
//                g.fill3DRect(stuff.getX() - 20,
//                        stuff.getY() - 20, 40, 40, false);

                g.drawImage(TankGameImages.stuffImg[StuffTypeEnum.IRON.getKey()], stuff.getX() - 10,
                        stuff.getY() - 10, 20, 20, panel);
                break;
            case WATER:

//                g.setColor(new Color(65, 64, 253));
//                g.fillRect(stuff.getX() - 20,
//                        stuff.getY() - 20, 40, 40);

                g.drawImage(TankGameImages.stuffImg[StuffTypeEnum.WATER.getKey()],
                        stuff.getX() - 10, stuff.getY() - 10, 20, 20, panel);
                break;
        }

    }

    /**
     * 画出爆炸
     *
     * @param g     Graphics
     * @param bombs 炸弹对象容器
     * @param panel 被画的那个面板
     */
    public void drawBomb(Graphics g, Vector bombs, JPanel panel) {
        for (int i = 0; i < bombs.size(); i++) {
            int l = bombs.get(i).getL();
            Bomb b = bombs.get(i); // 从炸弹容器中取出一颗炸弹
            if (b.getLifeTime() > 24) { // 生命值21-25
                g.drawImage(TankGameImages.bomb[0], b.getX() - l / 2, b.getY()
                        - l / 2, l, l, panel);
            } else if (b.getLifeTime() > 18) { // 生命值16-20
                g.drawImage(TankGameImages.bomb[1], b.getX() - l / 2, b.getY()
                        - l / 2, l, l, panel);
            } else if (b.getLifeTime() > 12) { // 生命值11-15
                g.drawImage(TankGameImages.bomb[2], b.getX() - l / 2, b.getY()
                        - l / 2, l, l, panel);
            } else if (b.getLifeTime() > 6) { // 生命值6-10
                g.drawImage(TankGameImages.bomb[3], b.getX() - l / 2, b.getY()
                        - l / 2, l, l, panel);
            } else { // 生命值低于6
                g.drawImage(TankGameImages.bomb[4], b.getX() - l / 2, b.getY()
                        - l / 2, l, l, panel);
            }
            b.lifeDown(); // 生命随时间衰减
            if (b.getLifeTime() == 0) { // 该炸弹死亡
                b.setLive(false);
            }
        }
    }

    /**
     * 画出敌人坦克和子弹
     *
     * @param g       Graphics
     * @param enemies 敌人坦克容量
     * @param panel   被画的面板
     */
    public void drawEnemyTank(Graphics g, Vector enemies, JPanel panel) {
        for (int i = 0; i < enemies.size(); i++) {
            this.drawStuff(g, enemies.get(i), panel); // 画出敌人的坦克
            for (int j = 0; j < enemies.get(i).getBullets().size(); j++) {
                if (enemies.get(i).getBullets().get(j) != null) {
                    Bullet eb = enemies.get(i).getBullets().get(j);
                    g.drawImage(TankGameImages.bullet, eb.getX() - 2,
                            eb.getY() - 2, 4, 4, panel);
                }
            }
        }
    }

    /**
     * 画出我的坦克和子弹
     *
     * @param g       Graphics
     * @param myTanks 我的坦克容量
     * @param panel   被画的那个面板
     */
    public void drawMyTank(Graphics g, Vector myTanks, JPanel panel) {
        for (int m = 0; m < myTanks.size(); m++) {
            MyTank myTank = myTanks.get(m); // 取出我的坦克
            this.drawStuff(g, myTank, panel); // 画出我的坦克
            for (int i = 0; i < myTank.getBullets().size(); i++) {
                if (myTank.getBullets().get(i) != null) {
                    Bullet b = myTank.getBullets().get(i);
                    g.drawImage(TankGameImages.bullet, b.getX() - 2,
                            b.getY() - 2, 4, 4, panel);
                }
            }
        }
    }

    /**
     * 画出地图
     *
     * @param g     Graphics
     * @param map   地图对象
     * @param panel 被画的那个面板
     */
    public void drawMap(Graphics g, Map map, JPanel panel) {
        Vector bricks = map.getBricks();
        Vector irons = map.getIrons();
        Vector waters = map.getWaters();
        for (int i = 0; i < bricks.size(); i++) {
            this.drawStuff(g, bricks.get(i), panel);
        }
        for (int i = 0; i < irons.size(); i++) {
            this.drawStuff(g, irons.get(i), panel);
        }
        for (int i = 0; i < waters.size(); i++) {
            this.drawStuff(g, waters.get(i), panel);
        }
    }

    /**
     * 画出一个面朝北的坦克
     *
     * @param g     Graphics
     * @param tank  东西对象
     * @param panel 被画的那个面板
     */
    public void drawNorth(Graphics g, Tank tank, JPanel panel) {

//        int x = tank.getX();
//        int y = tank.getY(); //0.设置画笔颜色
//        g.setColor(Color.white);
//        //1.画出左边的矩形
//        g.fill3DRect(x - 20, y - 20, 10,
//                40, false);
//        g.fill3DRect(x + 10, y - 20, 10, 40, false);//2.画出右边矩形
//        //3.更换画笔颜色
//        g.setColor(tank.getColor()); //4.画出轮子条纹
//        for (int
//             i = 0;
//             i < 20 - 1;
//             i++) {
//            g.drawLine(x - 20, y - 20 + (i + 1) * 2, x - 10 - 1, y - 20 + (i + 1) * 2);
//            g.drawLine(x + 10, y - 20 + (i + 1) * 2, x + 20 - 1, y - 20 + (i + 1) * 2);
//        } //5.画出中间1矩形
//        g.fill3DRect(x - 15, y - 14, 30, 28, false); //6.更换画笔颜色
//        g.setColor(Color.white); //7.画出中间2矩形
//        g.draw3DRect(x - 10, y - 9, 20,
//                18, false); //8.画出中间3矩形
//        g.draw3DRect(x - 3, y - 5, 6, 10, false); //9.画直线
//        g.drawLine(x - 15, y - 14, x - 10, y - 9);
//        g.drawLine(x + 15, y - 14, x + 10, y - 9);
//        g.drawLine(x - 15, y + 14, x - 10, y + 9);
//        g.drawLine(x + 15, y + 14, x + 10, y + 9);
//        g.setColor(tank.getColor()); //10.画矩形
//        g.fill3DRect(x - 3, y - 12, 6, 3,
//                false);
//        g.fill3DRect(x - 2, y - 20, 4, 2, false);
//        g.fill3DRect(x - 1, y - 20,
//                2, 11, false);

        Image image;
        if (tank.getTankType() == TankTypeEnum.MY) {
            g.setColor(Color.green);
            image = TankGameImages.myTankImg[DirectionEnum.NORTH.getKey()];// 初始化图片
        } else {
            image = TankGameImages.enemyTankImg[DirectionEnum.NORTH.getKey()];
            g.setColor(Color.gray);
        }
        g.drawImage(image, tank.getX() - 20, tank.getY() - 20, 40, 40, panel);
        g.fillRect(tank.getX() - 20, tank.getY() - 30, tank.getBlood() * 4, 5);
    }

    /**
     * 画出一个方向朝南的坦克
     *
     * @param g     Graphics
     * @param tank  东西对象
     * @param panel 被画的那个面板
     */
    public void drawSouth(Graphics g, Tank tank, JPanel panel) {

//        int x = tank.getX();
//        int y = tank.getY();
//        g.setColor(Color.white);
//        g.fill3DRect(x - 20, y - 20, 10, 40, false);
//        g.fill3DRect(x + 10, y - 20, 10,
//                40, false);
//        g.setColor(tank.getColor());
//        for (int i = 0; i < 20 - 1; i++) {
//            g.drawLine(x - 20, y - 20 + (i + 1) * 2, x - 10 - 1, y - 20 + (i + 1) * 2);
//            g.drawLine(x + 10,
//                    y - 20 + (i + 1) * 2, x + 20 - 1, y - 20 + (i + 1) * 2);
//        }
//        g.fill3DRect(x - 15, y - 14, 30,
//                28, false);
//        g.setColor(Color.white);
//        g.draw3DRect(x - 10, y - 9, 20,
//                18, false);
//        g.draw3DRect(x - 3, y - 5, 6, 10, false);
//        g.drawLine(x - 15,
//                y - 14, x - 10, y - 9);
//        g.drawLine(x + 15, y - 14, x + 10, y - 9);
//        g.drawLine(x - 15,
//                y + 14, x - 10, y + 9);
//        g.drawLine(x + 15, y + 14, x + 10, y + 9);
//        g.setColor(tank.getColor());
//        g.fill3DRect(x - 3, y + 9, 6, 3, false);
//        g.fill3DRect(x - 1, y + 9, 2, 11, false);
//        g.fill3DRect(x - 2, y + 18, 4, 2,
//                false);

        Image image;
        if (tank.getTankType() == TankTypeEnum.MY) {
            g.setColor(Color.green);
            image = TankGameImages.myTankImg[DirectionEnum.SOUTH.getKey()];// 初始化图片
        } else {
            image = TankGameImages.enemyTankImg[DirectionEnum.SOUTH.getKey()];
            g.setColor(Color.gray);
        }
        g.drawImage(image, tank.getX() - 20, tank.getY() - 20, 40, 40, panel);
        g.fillRect(tank.getX() - 20, tank.getY() - 30, tank.getBlood() * 4, 5);
    }

    /**
     * 画出一个方向朝西的坦克
     *
     * @param g     Graphics
     * @param tank  东西对象
     * @param panel 被画的那个面板
     */
    public void drawWest(Graphics g, Tank tank, JPanel panel) {

//        int x = tank.getX();
//        int y = tank.getY();
//        g.setColor(Color.white);
//        g.fill3DRect(x - 20, y - 20, 40, 10, false);
//        g.fill3DRect(x - 20, y + 10, 40,
//                10, false);
//        g.setColor(tank.getColor());
//        for (int i = 0; i < 20 - 1; i++) {
//            g.drawLine(x - 20 + (i + 1) * 2, y - 20, x - 20 + (i + 1) * 2, y - 10 - 1);
//            g.drawLine(x - 20 + (i + 1) * 2, y - 20 + 30, x - 20 + (i + 1) * 2, y - 10 - 1 + 30);
//        }
//        g.fill3DRect(x - 14, y - 15, 28, 30, false);
//        g.setColor(Color.white);
//        g.draw3DRect(x - 9, y - 10, 18, 20, false);
//        g.draw3DRect(x - 5, y - 3, 10,
//                6, false);
//        g.drawLine(x - 15, y - 14, x - 10, y - 9);
//        g.drawLine(x + 15, y - 14,
//                x + 10, y - 9);
//        g.drawLine(x - 15, y + 14, x - 10, y + 9);
//        g.drawLine(x + 15, y + 14,
//                x + 10, y + 9);
//        g.setColor(tank.getColor());
//        g.fill3DRect(x - 12, y - 3, 3,
//                6, false);
//        g.fill3DRect(x - 20, y - 1, 11, 2, false);
//        g.fill3DRect(x - 20,
//                y - 2, 2, 4, false);

        Image image;
        if (tank.getTankType() == TankTypeEnum.MY) {
            image = TankGameImages.myTankImg[DirectionEnum.WEST.getKey()];// 初始化图片
            g.setColor(Color.green);
        } else {
            image = TankGameImages.enemyTankImg[DirectionEnum.WEST.getKey()];
            g.setColor(Color.gray);
        }
        g.drawImage(image, tank.getX() - 20, tank.getY() - 20, 40, 40, panel);
        g.fillRect(tank.getX() - 20, tank.getY() - 30, tank.getBlood() * 4, 5);
    }

    /**
     * 画出一个方向朝东的坦克
     *
     * @param g     Graphics
     * @param tank  东西对象
     * @param panel 被画的那个面板
     */
    public void drawEast(Graphics g, Tank tank, JPanel panel) {

//        int x = tank.getX();
//        int y = tank.getY();
//        g.setColor(Color.white);
//        g.fill3DRect(x - 20, y - 20, 40, 10, false);
//        g.fill3DRect(x - 20, y + 10, 40,
//                10, false);
//        g.setColor(tank.getColor());
//        for (int i = 0; i < 20 - 1; i++) {
//            g.drawLine(x - 20 + (i + 1) * 2, y - 20, x - 20 + (i + 1) * 2, y - 10 - 1);
//            g.drawLine(x - 20 + (i + 1) * 2, y - 20 + 30, x - 20 + (i + 1) * 2, y - 10 - 1 + 30);
//        }
//        g.fill3DRect(x - 14, y - 15, 28, 30, false);
//        g.setColor(Color.white);
//        g.draw3DRect(x - 9, y - 10, 18, 20, false);
//        g.draw3DRect(x - 5, y - 3, 10,
//                6, false);
//        g.drawLine(x - 15, y - 14, x - 10, y - 9);
//        g.drawLine(x + 15, y - 14,
//                x + 10, y - 9);
//        g.drawLine(x - 15, y + 14, x - 10, y + 9);
//        g.drawLine(x + 15, y + 14,
//                x + 10, y + 9);
//        g.setColor(tank.getColor());
//        g.fill3DRect(x + 9, y - 3, 3, 6,
//                false);
//        g.fill3DRect(x + 9, y - 1, 11, 2, false);
//        g.fill3DRect(x + 18, y - 2,
//                2, 4, false);

        Image image;
        if (tank.getTankType() == TankTypeEnum.MY) {
            image = TankGameImages.myTankImg[DirectionEnum.EAST.getKey()];// 初始化图片
            g.setColor(Color.green);
        } else {
            image = TankGameImages.enemyTankImg[DirectionEnum.EAST.getKey()];
            g.setColor(Color.gray);
        }
        g.drawImage(image, tank.getX() - 20, tank.getY() - 20, 40, 40, panel);
        g.fillRect(tank.getX() - 20, tank.getY() - 30, tank.getBlood() * 4, 5);
    }

    /**
     * 画出游戏右边的那个面板
     *
     * @param g   Graphics
     * @param tgp 游戏主要面板对象
     */
    public void drawRight(Graphics g, GamePanel tgp, RealTimeGameData data) {
        if (data.getMapMakingFlag().equals(Boolean.TRUE)) {
            g.drawString("当前选中画笔(可按C键切换)", 620, 20);
            if (data.getCurrentStuff() == StuffTypeEnum.IRON) {
                drawStuff(g, rightIron, tgp);
            } else if (data.getCurrentStuff() == StuffTypeEnum.BRICK) {
                drawStuff(g, rightBrick, tgp);
            } else if (data.getCurrentStuff() == StuffTypeEnum.WATER) {
                drawStuff(g, rightWater, tgp);
            } else {
                g.drawString("橡皮擦", 680, 50);
            }

        } else {
            for (int i = 0; i < data.getEnemyTankNum(); i++) {
                if (i >= 4) {
                    g.drawImage(TankGameImages.enemyTankImg[DirectionEnum.NORTH.getKey()],
                            402 + 50 * i, 100, 40, 40, tgp);
                } else {
                    g.drawImage(TankGameImages.enemyTankImg[DirectionEnum.NORTH.getKey()],
                            602 + 50 * i, 20, 40, 40, tgp);
                }
            }
            for (int j = 0; j < data.getMyTankNum(); j++) {
                g.drawImage(TankGameImages.myTankImg[DirectionEnum.NORTH.getKey()], 602 + 50 * j,
                        400, 40, 40, tgp);
            }
            g.drawString("我的坦克子弹数目:" + data.getMyBulletNum(), 620, 500);
        }

    }


    public void rePaintPanel(GamePanel panel, Graphics g) {

        RealTimeGameData data = context.getGameData();
        if (data.isStart()) {
            g.setColor(Color.black);
            g.fillRect(0, 0, GameConstants.GAME_PANEL_WIDTH, GameConstants.GAME_PANEL_HEIGHT);
            g.fillRect(280, 600, 40, 40);
            this.drawMap(g, data.getMap(), panel);
            this.drawMyTank(g, data.getMyTanks(), panel); // 画出我的坦克(包括子弹)
            this.drawEnemyTank(g, data.getEnemies(), panel); // 画出敌人坦克(包括子弹)
            this.drawBomb(g, data.getBombs(), panel); // 画出爆炸
            this.drawRight(g, panel, data);

            if (data.getMyTankNum() == 0) { // 如果我的坦克数量为0
                g.drawImage(TankGameImages.gameOver, 250, data.getDy(), 100,
                        100, panel);
            }

            if (data.getEnemyTankNum() == 0) { // 如果敌人坦克的数量为0
                g.drawImage(TankGameImages.gameWin, 250, data.getDy(), 100,
                        100, panel);
            }
            if (data.getDy() == 250) {
                g.fillRect(0, 0, 800, 600);
                g.setColor(Color.BLUE);
                if (data.getMyTankNum() == 0) {
                    g.drawString("失败了!!!", 300, 220);
                } else {
                    g.drawString("挑战成功,请稍等...", 300, 220);
                }
                g.drawString(
                        ("敌人坦克死亡数量:" + (8 - data.getEnemyTankNum())),
                        300, 260);
                g.drawString("我的坦克死亡总数量:" + data.getBeKilled(), 300,
                        280);
                g.drawString(
                        "我的炮弹消耗总数量:"
                                + (GameConstants.MY_TANK_INIT_BULLET_NUM - data
                                .getMyBulletNum()), 300, 300);
                g.drawString("敌人坦克剩余数量:" + data.getEnemyTankNum(), 300,
                        320);
                g.drawString("我的坦克剩余总数量:" + data.getMyTankNum(), 300,
                        340);
                g.drawString("我的炮弹剩余总数量:" + data.getMyBulletNum(), 300,
                        360);
            }
        } else {
            g.drawImage(TankGameImages.startImage, 0, 0, 800, 700, panel);
            //g.drawImage(TankGameImages.font, 0, data.getKy(), panel);
            if (data.isIconSmile()) {
                //g.drawImage(TankGameImages.yctSmile1, data.getKx(), 45,
                //        panel);
                data.setIconSmile(false);
            } else {
                //g.drawImage(TankGameImages.yctSmile2, data.getKx(), 45,
                //        panel);
                data.setIconSmile(true);
            }
        }
    }
}

坦克核心算法

@Service
public class TankEventService {

    /**
     * 判断坦克是否与另一个事物重叠
     *
     * @param stuff  东西对象
     * @param length 两者之间的最短距离
     * @return 是否重叠
     */
    public boolean isTankOverlap(Tank tank, Stuff stuff, int length) {
        boolean b = false;
        int x = stuff.getX();
        int y = stuff.getY();
        if (tank.getDirect() == DirectionEnum.NORTH) {
            tank.setY(tank.getY() - tank.getSpeed());
            if (Math.abs(tank.getY() - y) < length
                    && Math.abs(tank.getX() - x) < length) {
                b = true;
                tank.setY(tank.getY() + tank.getSpeed());
            } else {
                tank.setY(tank.getY() + tank.getSpeed());
            }
        }
        if (tank.getDirect() == DirectionEnum.SOUTH) {
            tank.setY(tank.getY() + tank.getSpeed());
            if (Math.abs(tank.getY() - y) < length
                    && Math.abs(tank.getX() - x) < length) {
                b = true;
            }
            tank.setY(tank.getY() - tank.getSpeed());
        }
        if (tank.getDirect() == DirectionEnum.EAST) {
            tank.setX(tank.getX() + tank.getSpeed());
            if (Math.abs(tank.getY() - y) < length
                    && Math.abs(tank.getX() - x) < length) {
                b = true;
            }
            tank.setX(tank.getX() - tank.getSpeed());
        }
        if (tank.getDirect() == DirectionEnum.WEST) {
            tank.setX(tank.getX() - tank.getSpeed());
            if (Math.abs(tank.getY() - y) < length
                    && Math.abs(tank.getX() - x) < length) {
                b = true;
            }
            tank.setX(tank.getX() + tank.getSpeed());
        }
        return b;
    }

    /**
     * 判断是否重叠
     *
     * @param enemies 敌人坦克容量
     * @return 是否重叠
     */
    public boolean isMyTankOverlap(MyTank tank, Vector enemies) {
        for (int i = 0; i < enemies.size(); i++) {
            if (isTankOverlap(tank, enemies.get(i), 40))
                return true;
        }
        return false;
    }

    /**
     * 判断自己跟别的坦克是否重叠
     *
     * @param enemies 敌人坦克容量
     * @param myTanks 我的坦克容量
     * @return 是否重叠
     */
    public boolean isEnemyTankOverlap(EnemyTank enemy, Vector enemies, Vector myTanks) {
        for (int i = 0; i < enemies.size(); i++) {
            if (enemy != enemies.get(i)) {
                if (isTankOverlap(enemy, enemies.get(i), 40)) {
                    enemy.setOverlapNo(true);
                    return true;
                }
            }
        }
        for (int j = 0; j < myTanks.size(); j++) {
            if (isTankOverlap(enemy, myTanks.get(j), 40)) {
                enemy.setOverlapYes(true);
                return true;
            }
        }

        enemy.setOverlapNo(false);
        enemy.setOverlapYes(false);
        return false;
    }

    /**
     * 每隔36毫秒 一直向西走
     */
    public void enemyGoWest(EnemyTank enemy) {
        for (; ; ) {
            GameTimeUnit.sleepMillis(36);
            if (!enemy.isOverlapNo()&& !enemy.isOverlapYes()) {
                enemy.goWest();
            }
            if (enemy.getMyTankLocation() != DirectionEnum.WEST) {
                enemy.setDirect(enemy.getMyTankDirect());
                break;
            }
        }
    }

    /**
     * 每隔36毫秒 一直向东走
     */
    public void enemyGoEast(EnemyTank enemy) {
        for (; ; ) {
            GameTimeUnit.sleepMillis(36);
            if (!enemy.isOverlapNo() && !enemy.isOverlapYes()) {
                enemy.goEast();
            }
            if (enemy.getMyTankLocation() != DirectionEnum.EAST) {
                enemy.setDirect(enemy.getMyTankDirect());
                break;
            }
        }
    }

    /**
     * 每隔36毫秒 一直向北走
     */
    public void enemyGoNorth(EnemyTank enemy) {
        for (; ; ) {
            GameTimeUnit.sleepMillis(36);
            if (!enemy.isOverlapNo() && !enemy.isOverlapYes()) {
                enemy.goNorth();
            }
            if (enemy.getMyTankLocation() != DirectionEnum.NORTH) {
                enemy.setDirect(enemy.getMyTankDirect());
                break;
            }
        }
    }

    /**
     * 每隔36毫秒 一直向南走
     */
    public void enemyGoSouth(EnemyTank enemy) {
        for (; ; ) {
            GameTimeUnit.sleepMillis(36);
            if (!enemy.isOverlapNo() && !enemy.isOverlapYes()) {
                enemy.goSouth();
            }
            if (enemy.getMyTankLocation() != DirectionEnum.SOUTH) {
                enemy.setDirect(enemy.getMyTankDirect());
                break;
            }
        }
    }

    /**
     * 从指定的三个方向中随机选择一个
     *
     * @param direct1 方向1
     * @param direct2 方向2
     * @param direct3 方向3
     */
    public DirectionEnum enemyGetRandomDirect(DirectionEnum direct1, DirectionEnum direct2, DirectionEnum direct3) {
        int random = (int) (Math.random() * 3);

        DirectionEnum returnDirect = DirectionEnum.INVALID;
        switch (random) {
            case 0:
                returnDirect = direct1;
                break;
            case 1:
                returnDirect = direct2;
                break;
            case 2:
                returnDirect = direct3;
                break;
        }
        return returnDirect;
    }

    /**
     * 让敌人坦克能够发现我的坦克并开炮
     *
     * @param myTank 我的坦克
     * @param map    地图对象
     */
    public void enemyFindAndKill(EnemyTank enemy, MyTank myTank, Map map) {
        int myX = myTank.getX();
        int myY = myTank.getY();
        int enX = enemy.getX();
        int enY = enemy.getY();
        if (Math.abs(myX - enX) < 20 && myY <= 580) {
            if (enY < myY) {
                int s = 0;
                for (int t = 0; t < map.getIrons().size(); t++) {
                    Iron iron = map.getIrons().get(t);
                    if (Math.abs(enX - iron.getX()) <= 10 && iron.getY() > enY
                            && iron.getY() < myY) {
                        s = 1;
                        break;
                    }
                }
                if (s == 0) {
                    enemy.setShot(true);
                    enemy.setMyTankLocation(DirectionEnum.SOUTH);
                }
            } else {
                int s = 0;
                for (int t = 0; t < map.getIrons().size(); t++) {
                    Iron iron = map.getIrons().get(t);
                    if (Math.abs(enX - iron.getX()) <= 10 && iron.getY() < enY
                            && iron.getY() > myY) {
                        s = 1;
                        break;
                    }
                }
                if (s == 0) {
                    enemy.setShot(true);
                    enemy.setMyTankLocation(DirectionEnum.NORTH);
                }
            }
        } else if (Math.abs(myY - enY) < 20 && myY <= 580) {
            if (enX > myX) {
                int s = 0;
                for (int t = 0; t < map.getIrons().size(); t++) {
                    Iron iron = map.getIrons().get(t);
                    if (Math.abs(enY - iron.getY()) <= 10 && iron.getX() < enX
                            && iron.getX() > myX) {
                        s = 1;
                        break;
                    }
                }
                if (s == 0) {
                    enemy.setShot(true);
                    enemy.setMyTankLocation(DirectionEnum.WEST);
                }
            } else {
                int s = 0;
                for (int t = 0; t < map.getIrons().size(); t++) {
                    Iron iron = map.getIrons().get(t);
                    if (Math.abs(enY - iron.getY()) <= 10 && iron.getX() > enX
                            && iron.getX() < myX) {
                        s = 1;
                        break;
                    }
                }
                if (s == 0) {
                    enemy.setShot(true);
                    enemy.setMyTankLocation(DirectionEnum.EAST);
                }
            }
        } else {
            enemy.setShot(false);
            enemy.setMyTankLocation(DirectionEnum.INVALID);
        }
    }
}

热门栏目