“This is the sixth day of my participation in the First Challenge 2022. For details: First Challenge 2022”

❤️ Author’s home page: Xiao Xu Zhu

❤️ About the author: Hello everyone, I am Xiao Xu Zhu. Java field quality creator 🏆, CSDN blog expert 🏆, Huawei cloud enjoy expert 🏆, Nuggets of the year popular author 🏆, Ali Cloud expert blogger 🏆

❤️ technology live, the appreciation

❤️ like 👍 collect ⭐ look again, form a habit

preface

We all played tank wars when we were kids. The familiar melodies and rich levels accompanied us for the whole winter and summer vacation. Do you still remember the legendary classic tank wars? Those nostalgic memories, along with us came the classic tank battle, the beginning of the fear, repeatedly destroyed by the enemy tank scene vividly. Now you don’t have to worry about enemy tanks anymore, you can go on a rampage and wipe out enemy tanks. Quick!!

“Tank War” game to tank combat as the theme, using the Java language, using swing technology interface processing, design ideas with object-oriented thought.

The main requirements

[Fixed] Can generate different maps and destroy all tanks on the map for victory Level maps can be set up to increase the difficulty.

The main design

1, there should be difficult levels: the first pass, the second pass, the third pass, the fourth pass, the fifth pass; Level 1 map is the easiest, level 5 map is the hardest;

2, tanks have to have blood strip, hit many times will die

3. The map requires me to have 4 of my tanks (4 lives) and multiple enemy tanks

4. The number of shells for our tanks is fixed, set to 500

5. Basic information is displayed on the right side of the map

6, the map should be in the bricks, iron, rivers

Screenshot function

Game start page

Code implementation

Window layout

public class GameFrame extends JFrame {

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


    public GameFrame(a) {
        super(a);this.setSize(800.700);
        this.setTitle("Tank Wars.");
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setIconImage(TankGameImages.myTankImg[0]);

        // Display screen size
        Dimension screenSizeInfo = Toolkit.getDefaultToolkit().getScreenSize();
        int leftTopX = ((int) screenSizeInfo.getWidth() - this.getWidth()) / 2;
        int leftTopY = ((int) screenSizeInfo.getHeight() - this.getHeight()) / 2;

        // Set the display position in the middle of the screen
        this.setLocation(leftTopX, leftTopY); }}Copy the code

Map rendering core algorithm


@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);

    /** * Draw things (including tanks, obstacles...) * *@param g     Graphics
     * @paramStuff stuff object *@paramPanel The panel that is drawn */
    public void drawStuff(Graphics g, Stuff stuff, JPanel panel) {
        switch (stuff.getType()) {
            // Enumeration switch, interesting, no need for + stuffTypeene.tank
            case TANK:
                Tank tank = (Tank) stuff;
                switch (stuff.getDirect()) { // Determine the direction
                    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; }}/** * draw explosion **@param g     Graphics
     * @paramBombs object container *@paramPanel The panel that is drawn */
    public void drawBomb(Graphics g, Vector<Bomb> bombs, JPanel panel) {
        for (int i = 0; i < bombs.size(); i++) {
            int l = bombs.get(i).getL();
            Bomb b = bombs.get(i); // Remove a bomb from the bomb container
            if (b.getLifeTime() > 24) { // Health is 21-25
                g.drawImage(TankGameImages.bomb[0], b.getX() - l / 2, b.getY()
                        - l / 2, l, l, panel);
            } else if (b.getLifeTime() > 18) { // Health is 16-20
                g.drawImage(TankGameImages.bomb[1], b.getX() - l / 2, b.getY()
                        - l / 2, l, l, panel);
            } else if (b.getLifeTime() > 12) { // Health is 11-15
                g.drawImage(TankGameImages.bomb[2], b.getX() - l / 2, b.getY()
                        - l / 2, l, l, panel);
            } else if (b.getLifeTime() > 6) { // Health is 6-10
                g.drawImage(TankGameImages.bomb[3], b.getX() - l / 2, b.getY()
                        - l / 2, l, l, panel);
            } else { // Health is below 6
                g.drawImage(TankGameImages.bomb[4], b.getX() - l / 2, b.getY()
                        - l / 2, l, l, panel);
            }
            b.lifeDown(); // Life decays with time
            if (b.getLifeTime() == 0) { // The bomb died
                b.setLive(false); }}}/** * Draw enemy tanks and bullets **@param g       Graphics
     * @paramEnemies enemy tank capacity *@paramPanel The panel that is drawn */
    public void drawEnemyTank(Graphics g, Vector<EnemyTank> enemies, JPanel panel) {
        for (int i = 0; i < enemies.size(); i++) {
            this.drawStuff(g, enemies.get(i), panel); // Draw enemy tanks
            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); }}}}/** * Draw my tanks and bullets **@param g       Graphics
     * @paramMyTanks my tank capacity *@paramPanel The panel that is drawn */
    public void drawMyTank(Graphics g, Vector<MyTank> myTanks, JPanel panel) {
        for (int m = 0; m < myTanks.size(); m++) {
            MyTank myTank = myTanks.get(m); // Take out my tank
            this.drawStuff(g, myTank, panel); // Draw my tank
            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); }}}}/** ** Draw the map **@param g     Graphics
     * @paramMap Map object *@paramPanel The panel that is drawn */
    public void drawMap(Graphics g, Map map, JPanel panel) {
        Vector<Brick> bricks = map.getBricks();
        Vector<Iron> irons = map.getIrons();
        Vector<Water> 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); }}/** * Draw a tank facing north **@param g     Graphics
     * @paramTank things object *@paramPanel The panel that is drawn */
    public void drawNorth(Graphics g, Tank tank, JPanel panel) {

// int x = tank.getX();
// int y = tank.getY(); //0. Set the brush color
// g.setColor(Color.white);
// //1. Draw the rectangle on the left
// g.fill3DRect(x - 20, y - 20, 10,
// 40, false);
// g.fill3DRect(x + 10, y - 20, 10, 40, false); //2. Draw the right rectangle
// //3. Change the brush color
// g.setColor(tank.getColor()); //4. Draw the stripes of the wheel
// 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. Draw the middle rectangle
// g.fill3DRect(x - 15, y - 14, 30, 28, false); //6. Change the brush color
// g.setColor(Color.white); //7. Draw the middle two rectangles
// g.draw3DRect(x - 10, y - 9, 20,
// 18, false); //8. Draw the middle three rectangles
// g.draw3DRect(x - 3, y - 5, 6, 10, false); / / 9. Draw a straight line
// 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. Draw a rectangle
// 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()];// Initialize the image
        } 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);
    }

    /** * Draw a tank facing south **@param g     Graphics
     * @paramTank things object *@paramPanel The panel that is drawn */
    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()];// Initialize the image
        } 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);
    }

    /** * Draw a tank facing west **@param g     Graphics
     * @paramTank things object *@paramPanel The panel that is drawn */
    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()];// Initialize the image
            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);
    }

    /** * Draw a tank facing east **@param g     Graphics
     * @paramTank things object *@paramPanel The panel that is drawn */
    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()];// Initialize the image
            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);
    }

    /** * Draw the panel on the right of the game **@param g   Graphics
     * @paramTGP game main panel object */
    public void drawRight(Graphics g, GamePanel tgp, RealTimeGameData data) {
        if (data.getMapMakingFlag().equals(Boolean.TRUE)) {
            g.drawString("Currently selected brush (toggle by pressing 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(eraser.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("Number of my tank bullets :" + 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); // Draw my tank (including bullets)
            this.drawEnemyTank(g, data.getEnemies(), panel); // Draw enemy tanks (including bullets)
            this.drawBomb(g, data.getBombs(), panel); // Draw the explosion
            this.drawRight(g, panel, data);

            if (data.getMyTankNum() == 0) { // If my tank count is 0
                g.drawImage(TankGameImages.gameOver, 250, data.getDy(), 100.100, panel);
            }

            if (data.getEnemyTankNum() == 0) { If the number of enemy tanks is 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("Failed!!".300.220);
                } else {
                    g.drawString("Challenge successful, please wait...".300.220);
                }
                g.drawString(
                        ("Enemy tank deaths :" + (8 - data.getEnemyTankNum())),
                        300.260);
                g.drawString("Total number of tank deaths :" + data.getBeKilled(), 300.280);
                g.drawString(
                        "Total number of shells consumed :"
                                + (GameConstants.MY_TANK_INIT_BULLET_NUM - data
                                .getMyBulletNum()), 300.300);
                g.drawString("Number of enemy tanks remaining :" + data.getEnemyTankNum(), 300.320);
                g.drawString("Total number of tanks left :" + data.getMyTankNum(), 300.340);
                g.drawString("Total number of shells left :" + 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); }}}}Copy the code

Tank core algorithm


@Service
public class TankEventService {

    /** * Determine whether the tank overlaps with another object **@paramStuff stuff object *@paramLength The shortest distance between the two *@returnWhether to overlap */
    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;
    }

    /** * determine whether to overlap **@paramEnemies enemy tank capacity *@returnWhether to overlap */
    public boolean isMyTankOverlap(MyTank tank, Vector<EnemyTank> enemies) {
        for (int i = 0; i < enemies.size(); i++) {
            if (isTankOverlap(tank, enemies.get(i), 40))
                return true;
        }
        return false;
    }

    /** * Determine if they overlap with other tanks **@paramEnemies enemy tank capacity *@paramMyTanks my tank capacity *@returnWhether to overlap */
    public boolean isEnemyTankOverlap(EnemyTank enemy, Vector<EnemyTank> enemies, Vector<MyTank> 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;
    }

    /** ** go straight west every 36 milliseconds */
    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; }}}/** ** go straight east every 36 milliseconds */
    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; }}}/** ** go straight north every 36 milliseconds */
    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; }}}/** ** keep going south every 36 milliseconds */
    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; }}}/** * selects a random ** from the specified three directions@paramDirect1 direction 1 *@paramDirect2 direction 2 *@paramDirect3 direction 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;
    }

    /** * let enemy tanks find my tanks and fire at them **@paramMyTank myTank *@paramMap Map object */
    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); }}}Copy the code

conclusion

Through the “Tank Wars” game implementation, let me have a further understanding of swing related knowledge, the Language of Java has a deeper understanding than before.

Basic Java syntax, such as data types, operators, program flow control, and arrays, is better understood. Java is the core of the core object oriented thought, for this concept, finally realized some.

The source code for

Can pay attention to the blogger, private chat blogger access