• Java 第二阶段提升编程能力【坦克大战3.0】


    代码链接:https://download.csdn.net/download/qq_52354698/86405067?spm=1001.2014.3001.5501

    1. 防止敌人坦克重叠运动

        //编写方法判断当前的这个敌人坦克是否和enemyTanks中的其他坦克发生重叠或者碰撞
        public boolean isTouchEnemyTank() {
            switch (this.getDirect()) {
                case 0:
                    for (int i = 0; i < enemyTanks.size(); i++) {
                        //从vector中依次取出敌人坦克
                        EnemyTank enemyTank = enemyTanks.get(i);
                        if (enemyTank != this) {
                            if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                                if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 40 && this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 60) {
                                    return true;
                                }
                                if (this.getX() + 40 >= enemyTank.getX() && this.getX() + 40 <= enemyTank.getX() + 40 && this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 60) {
                                    return true;
                                }
                            }
                            if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                                if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 60 && this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 40) {
                                    return true;
                                }
                                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++) {
                        //从vector中依次取出敌人坦克
                        EnemyTank enemyTank = enemyTanks.get(i);
                        if (enemyTank != this) {
                            if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                                if (this.getX() + 60 >= enemyTank.getX() && this.getX() + 60 <= enemyTank.getX() + 40 && this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 60) {
                                    return true;
                                }
                                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;
                                }
                            }
                            if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                                if (this.getX() + 60 >= enemyTank.getX() && this.getX() + 60 <= enemyTank.getX() + 60 && this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 40) {
                                    return true;
                                }
                                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++) {
                        //从vector中依次取出敌人坦克
                        EnemyTank enemyTank = enemyTanks.get(i);
                        if (enemyTank != this) {
                            if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                                if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 40 && this.getY() + 60 >= enemyTank.getY() && this.getY() + 60 <= enemyTank.getY() + 60) {
                                    return true;
                                }
                                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;
                                }
                            }
                            if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                                if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 60 && this.getY() + 60 >= enemyTank.getY() && this.getY() + 60 <= enemyTank.getY() + 40) {
                                    return true;
                                }
                                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++) {
                        //从vector中依次取出敌人坦克
                        EnemyTank enemyTank = enemyTanks.get(i);
                        if (enemyTank != this) {
                            if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                                if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 40 && this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 60) {
                                    return true;
                                }
                                if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 40 && this.getY() + 40 >= enemyTank.getY() && this.getY() + 40 <= enemyTank.getY() + 60) {
                                    return true;
                                }
                            }
                            if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                                if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 60 && this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 40) {
                                    return true;
                                }
                                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;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102

    2. 记录玩家的成绩,存盘退出

    记录我方击毁敌方坦克总数

    package com.qdu.tankgame;
    
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    
    /**
     * @author dell
     * @version 1.0
     * 记录相关的信息,与文件交互
     */
    public class Recorder {
    
        private static int allEnemyTanksNum = 0;
        private static FileWriter fileWriter = null;
        private static BufferedWriter bufferedWriter = null;
        private static String recordFile = "e:\\myRecord.txt";
    
        //当游戏退出时,将数据保存到文件中
        public static void keepRecord() throws IOException {
            bufferedWriter = new BufferedWriter(new FileWriter(recordFile));
            bufferedWriter.write(allEnemyTanksNum);
            bufferedWriter.close();
        }
    
        public static int getAllEnemyTanksNum() {
            return allEnemyTanksNum;
        }
    
        public static void setAllEnemyTanksNum(int allEnemyTanksNum) {
            Recorder.allEnemyTanksNum = allEnemyTanksNum;
        }
    
        public static void addAllEnemyTankNum() {
            Recorder.allEnemyTanksNum++;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    3. 记录当时的敌人坦克坐标,存盘退出

    当窗口关闭时,获取到剩余的地方坦克的信息,将其存储到文件中

            for (int i = 0; i < enemyTanks.size(); i++) {
                EnemyTank enemyTank = enemyTanks.get(i);
                if (enemyTank.isLive) {
                    String record = enemyTank.getX() + " " + enemyTank.getY() + " " + enemyTank.getDirect();
                    bufferedWriter.write(record + "\r\n");
                }
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    4. 玩游戏时,可以选择是新开始游戏还是继续上局游戏

    在执行程序的时候,提供用户选择:1.开始新游戏,2.继续上局游戏
    如继续上局游戏,则从文件中读取上次游戏结束,保存到文件中的敌方坦克的坐标,然后将其绘制在地图上,以到达继续游戏的效果

    package com.qdu.tankgame;
    
    /**
     * @author dell
     * @version 1.0
     * 一个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;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    5. 新游戏开始时,播放经典的坦克大战音乐

    使用一个播放音乐的类

    package com.qdu.tankgame;
    
    import javax.sound.sampled.*;
    import java.io.File;
    import java.io.IOException;
    
    /**
     * @author dell
     * @version 1.0
     */
    public class AePlayWave extends Thread {
        private String filename;
    
        public AePlayWave(String wavfile) { //构造器 , 指定文件
            filename = wavfile;
    
        }
    
        public void run() {
    
            File soundFile = new File(filename);
    
            AudioInputStream audioInputStream = null;
            try {
                audioInputStream = AudioSystem.getAudioInputStream(soundFile);
            } catch (Exception e1) {
                e1.printStackTrace();
                return;
            }
    
            AudioFormat format = audioInputStream.getFormat();
            SourceDataLine auline = null;
            DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
    
            try {
                auline = (SourceDataLine) AudioSystem.getLine(info);
                auline.open(format);
            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
    
            auline.start();
            int nBytesRead = 0;
            //这是缓冲
            byte[] abData = new byte[512];
    
            try {
                while (nBytesRead != -1) {
                    nBytesRead = audioInputStream.read(abData, 0, abData.length);
                    if (nBytesRead >= 0)
                        auline.write(abData, 0, nBytesRead);
                }
            } catch (IOException e) {
                e.printStackTrace();
                return;
            } finally {
                auline.drain();
                auline.close();
            }
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
            new AePlayWave("src\\111.wav").start();
    
    • 1
  • 相关阅读:
    mysql高阶sql语句
    FITC荧光素标记氨基半乳糖/氨基葡萄糖定制合成
    鸡哥的 AI 驾驶 (Gym - 103186H)
    ctfshow学习记录-misc入门(图片篇-颜色通道50-59)
    (附源码)云南濒危动物知识问答系统的设计与实现 毕业设计 020833
    TCP 和 UDP哪个更好
    精品SpringCloud商品服务系统微服务分布式疫情下购物商城
    leetcode-200. 岛屿数量
    C# OpenCvSharp Yolov8 Pose 姿态识别
    【动手学深度学习】RNN浅记
  • 原文地址:https://blog.csdn.net/qq_52354698/article/details/126437845