首頁 > 軟體

Java實現遊戲飛機大戰-III的範例程式碼

2022-02-11 16:01:46

前言

《飛機大戰-III》是一款融合了街機、競技等多種元素的經典射擊手遊。華麗精緻的遊戲畫面,超炫帶感的技能特效,超火爆畫面讓你腎上腺素爆棚,給你帶來全方位震撼感受,體驗飛行戰鬥的無限樂趣。

遊戲是用java語言實現,採用了swing技術進行了介面化處理,設計思路用了物件導向思想。

主要需求

基於Java Swing,以飛機大戰為原形,以抗日電視劇《亮劍》中的“李雲龍”為主題,實現選單、選關、難度、等級、技能等功能。

主要設計

①進入遊戲後,請按下shift鍵(將鍵盤改為英文模式)

②技能為數位1(攻擊)、2(治癒)、3(蓄氣)

③數位4、5、6、7、8為創意音效,空格鍵為暫停

④使用1、2技能會消耗相應的技能藍條,3技能消耗一定血量恢復一定藍

⑤觸碰遊戲中隨機出現的降落傘彈藥可以回藍

⑥觸碰遊戲中隨機出現的閃光粒子可以隨機更換1技能特效,且血量與藍加2

⑦由滑鼠控制角色位置,角色會自動發射相應等級的子彈

⑧每打敗一個boss,角色升一級,子彈同行升級

⑨每過一關,角色血量和藍自動增大並加滿

⑩按下空格暫停遊戲

本遊戲該版本共三個關卡和三種難度,任君挑選

功能截圖

遊戲開始

選擇戰機

開始遊戲

程式碼實現

遊戲面板類

把各遊戲物件集中繪製到此面板

public class GamePanel extends JFrame {
    // Keypoint 基本變數定義
    Image offScreenImage = null; // 定義雙快取
    int width = 1250, height = 725; // 視窗寬高
    public static int state = 0; // Keypoint 遊戲狀態 0未開始 1執行 2暫停 3通關失敗 4通關成功
    public static int score = 0; // 遊戲分數
    public static int count = 1; // 遊戲繪製次數(計時作用)
    static int enemyFrequency = 30;  // 敵機出現頻率(不同關卡速率不同)
    static int enemyShellFrequency = 40;  // 敵機子彈出現頻率(不同關卡速率不同)
    static int planeShellFrequency = 20;  // 戰機子彈出現頻率(不同關卡速率不同)
    public static int enemyCount = 1;  // 記錄敵機敵機出現數量
    static int bossAppear = 50;  // 定義出現多少敵機後boss出現
    public static boolean skill_release = false; // 判定技能是否已經釋放
    static boolean play_BOSS_sound = true; // 調控boss出場音效只播放一次
    static boolean play_warn_sound = true; // 調控boss出場預警音效只播放一次
    static boolean end = true; // 調控失敗或勝利音效只播放一次
    static boolean T = false; // 失敗或勝利時按T退出遊戲
    public static boolean R = false; // 返回主介面
    static boolean play = true; // 調控按紐二(點選播放,再點選暫停)
    static boolean prearranged = true; // 動態效果預載入
    public static boolean MAAppear = false;  // 神獸出現
    public static boolean MAExist = false;  // 神獸存在否
    public static boolean plane_create = false;  // 飛機建立

    // 難度
    static int skillConsume = 2; // 2 5 10 用技能消耗藍
    static int skillRecovery = 100; // 100 150 999 回藍速度
    static int lifeRecovery = 50; // 50 90 500 回血速度

    // Keypoint 元素實體定義
    // 背景實體類
    public GameObject backGroundObj = new BackGround(GameUtils.bgImg, 0, -1900, 2, this);
    // 戰機實體類
    public GameObject planeObj = new Plane(GameUtils.planeImg, 600, 550, 70, 100, 0, this);
    // boss實體類
    public GameObject BOSSObj = null;
    // 神獸實體類
    public GameObject mAnimalsObj = null;

    // Keypoint 按鈕宣告
    JButton button1 = new JButton("開始遊戲");
    JButton button2 = new JButton("聽聽歌吧");
    JButton button3 = new JButton("退出遊戲");
    JButton button4 = new JButton("選擇難度\關卡");
    JButton button5 = new JButton("遊戲說明");
    JButton button6 = new JButton("選擇戰機");
    JPanel frame = new JPanel(); // 按鈕面板

    // Keypoint 音效宣告
    Sound sound = new Sound(GameUtils.run_no1); // 背景音樂1(亮劍)
    //    Sound sound2 = new Sound(GameUtils.run_no2); // 開始按鈕(開炮)
    Sound sound3 = new Sound(GameUtils.run2_no1); // 第二關背景音樂(無人機群)
    Sound sound4 = new Sound(GameUtils.basic_no5); // 背景音樂2(槍林彈雨)

    public GamePanel() {
        // Keypoint 開始介面按鈕
        frame.setLayout(null);
        Sound s = new Sound(GameUtils.basic_no9); // 按紐二切歌

        button1.setIcon(new ImageIcon(GameUtils.button_start)); // 開始介面按鈕一
        button1.setBounds(530, 350, 245, 70);
        frame.add(button1);
        button1.addActionListener(e -> {
            if (!Popup.select) { // 如果返回主介面沒有選擇關卡,預設初始化第一關
                GamePanel.bossAppear = 50;
                BOSS.number = 0;
                GamePanel.play_BOSS_sound = true;
                Plane.lock = false;
                Plane.plane_level = 1;
            }
            state = 1;
            s.stop();
            sound.start();
            sound.loop();
            new Sound(GameUtils.run_no2).start();
        });

        button2.setIcon(new ImageIcon(GameUtils.button_selection_plane)); // 開始介面按鈕二
        button2.setBounds(530, 435, 245, 70);
        frame.add(button2);
        button2.addActionListener(e -> new Popup_SelectPlane());

        button3.setIcon(new ImageIcon(GameUtils.button_selection)); // 開始介面按鈕三
        button3.setBounds(530, 520, 245, 70);
        frame.add(button3);
        button3.addActionListener(e -> new Popup());

        button4.setIcon(new ImageIcon(GameUtils.button_exit)); // 開始介面按鈕四
        button4.setBounds(530, 605, 245, 70);
        frame.add(button4);
        button4.addActionListener(e -> System.exit(0));

        button5.setIcon(new ImageIcon(GameUtils.button_explain)); // 開始介面按鈕五
        button5.setBounds(20, 20, 45, 45);
        frame.add(button5);

        button6.setIcon(new ImageIcon(GameUtils.button_select)); // 開始介面按鈕二
        button6.setBounds(20, 80, 45, 45);
        frame.add(button6);
        button6.addActionListener(e -> {
            if (play) {
                sound.stop();
                s.start();
                s.loop();
                play = false;
            } else {
                s.stop();
                sound.start();
                play = true;
            }
        });

        button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                new Sound(GameUtils.basic_no10).start();
            }
        });
        button2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                new Sound(GameUtils.basic_no10).start();
            }
        });
        button3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                new Sound(GameUtils.basic_no10).start();
            }
        });
        button4.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                new Sound(GameUtils.basic_no10).start();
            }
        });
        button5.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                new Sound(GameUtils.basic_no11).start();
                JOptionPane.showMessageDialog(null,
                        "親愛的雲龍戰士,歡迎來到開炮大陸!!n" +
                                "-----------------------------------------------------2021-12-01-----------------------------------------------------n" +
                                "下面是操作說明:n" +
                                "    ①進入遊戲後,請按下shift鍵(將鍵盤改為英文模式)n" +
                                "    ②技能為數位1(攻擊)、2(治癒)、3(蓄氣)n" +
                                "    ③數位4、5、6、7、8為創意音效,空格鍵為暫停n" +
                                "    ④使用1、2技能會消耗相應的技能藍條,3技能消耗一定血量恢復一定藍n" +
                                "    ⑤觸碰遊戲中隨機出現的降落傘彈藥可以回藍n" +
                                "    ⑥觸碰遊戲中隨機出現的閃光粒子可以隨機更換1技能特效,且血量與藍加2n" +
                                "    ⑦由滑鼠控制角色位置,角色會自動發射相應等級的子彈n" +
                                "    ⑧每打敗一個boss,角色升一級,子彈同行升級n" +
                                "    ⑨每過一關,角色血量和藍自動增大並加滿n" +
                                "    ⑩按下空格暫停遊戲n" +
                                "本遊戲該版本共三個關卡和三種難度,任君挑選n" +
                                "-----------------------------------------------------2021-12-07-----------------------------------------------------n" +
                                "更新:n" +
                                "    ①在暫停、失敗、勝利介面可以按'R‘返回主介面,按‘T'退出遊戲n" +
                                "    ②新增在暫停介面按‘O'進入商店n" +
                                "    ③w s a d上下左右控制神獸移動n" +
                                "-----------------------------------------------------2021-12-08-----------------------------------------------------n" +
                                "更新:n" +
                                "    ①敵機會根據戰機的位置進行跟蹤移動n" +
                                "    ②修改部分技能效果n" +
                                "-----------------------------------------------------2021-12-11-----------------------------------------------------n" +
                                "更新:n" +
                                "    ①新增初始選單選擇戰機功能n" +
                                "-----------------------------------------------------2021-12-13-----------------------------------------------------n" +
                                "    ①修復一些已知問題n" +
                                "-------------------------------------------------------------------------------------------------------------------------n" +
                                "n" +
                                "注意事項:n" +
                                "    如果在遊戲中遇到任何問題,請關閉重啟!!!n" +
                                "n" +
                                "                                                                                                                Developer: 濤n", "※遊戲說明", JOptionPane.PLAIN_MESSAGE);
            }
        });
        button6.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                new Sound(GameUtils.basic_no10).start();
            }
        });
        if (prearranged) {
            Explode explodeObj = new Explode(1000, -1000);
            GameUtils.explodeList.add(explodeObj);
            GameUtils.removeList.add(explodeObj);
            Explode2 explodeObj2 = new Explode2(1000, -500);
            GameUtils.explodeList2.add(explodeObj2);
            GameUtils.removeList.add(explodeObj2);
            Explode3 explodeObj3 = new Explode3(1000, -200);
            GameUtils.explodeList3.add(explodeObj3);
            GameUtils.removeList.add(explodeObj3);
            prearranged = false;
        }
        this.add(frame); // 把開始介面按鈕新增進主面板
        launch();
    }

    // Keypoint 啟動
    public void launch() {
        this.setFocusable(true);
        this.setVisible(true);
        this.setBounds(0, 0, width, height);
        this.setTitle("雲龍開炮4.0");
        this.setIconImage(GameUtils.icon); // 程式圖示
        this.setResizable(false);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // 將遊戲物件新增到大集合
        GameUtils.gameObjList.add(backGroundObj);
        GameUtils.gameObjList.add(planeObj);

        // 滑鼠監聽
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("滑鼠監聽按下 ——> " + e.getButton());
                if (e.getButton() == 3) {
                    System.out.println("釋放技能1");   // 釋放技能(攻擊)
                    Skill.release = true;
                    Skill.number = 1;
                }
            }
        });

        // Keypoint 鍵盤監聽(暫停、釋放技能)
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
                switch (e.getKeyCode()) {
                    case 32:  // 空格暫停
                        switch (state) {
                            //執行改為暫停
                            case 1:
                                System.out.println("遊戲暫停");
                                state = 2;
                                sound.stop();
                                sound3.stop();
                                sound4.stop();
                                break;
                            //暫停改為執行
                            case 2:
                                System.out.println("繼續遊戲");
                                state = 1;
                                sound.start();
                                if (Plane.plane_level == 2) {
                                    sound3.start();
                                }
                                sound4.start();
                                break;
                        }
                        break;
                    case 49: // 1 釋放技能(攻擊)
                        if (Plane.plane_skill >= 5) {
                            System.out.println("釋放技能1(攻擊)");
                            Skill.release = true;
                            Skill.number = 1;
                        }
                        break;
                    case 50: // 2 釋放技能(治療)
                        if (Plane.plane_skill >= 5) {
                            System.out.println("釋放技能2(治療)");
                            Skill.release = true;
                            Skill.number = 2;
                        }
                        break;
                    case 51: // 3 蓄氣
                        System.out.println("釋放技能3(蓄氣)");
                        Skill.release = true;
                        Skill.number = 3;
                        break;
                    case 52: // 4 音效
                        new Sound(GameUtils.skill_no10).start();
                        break;
                    case 53: // 5 音效
                        new Sound(GameUtils.skill_no11).start();
                        break;
                    case 54: // 6 音效
                        new Sound(GameUtils.skill_no12).start();
                        break;
                    case 55: // 7 音效
                        new Sound(GameUtils.skill_no13).start();
                        break;
                    case 56: // 8 音效
                        new Sound(GameUtils.skill_no14).start();
                        break;
                    case 82: // 監聽R鍵
                        if (state == 2 || state == 3 || state == 4)
                            R = true;
                        break;
                    case 84: // 監聽T鍵
                        if (state == 2 || state == 3 || state == 4)
                            T = true;
                        break;
                    case 79: // 監聽O鍵
                        if (state == 2) {
                            new Popup_Shop();
                        }
                        break;
                    case 87: // W
                        if (mAnimalsObj != null) {
                            MAnimals.Direction = 1;
                        }
                        break;
                    case 83: // S
                        if (mAnimalsObj != null) {
                            MAnimals.Direction = 2;
                        }
                        break;
                    case 65: // A
                        if (mAnimalsObj != null) {
                            MAnimals.Direction = 3;
                        }
                        break;
                    case 68: // D
                        if (mAnimalsObj != null) {
                            MAnimals.Direction = 4;
                        }
                        break;
                    default:
                        System.out.println("鍵盤監聽按下 ——> " + e.getKeyCode());
                        break;
                }
            }
        });

        // Keypoint 重新整理螢幕
        while (true) {
            if (state > 0) {
                repaint();
                if (state == 1)
                    createObj();
            }
            try {
                Thread.sleep(10); // 10ms重新整理一次
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void init() {
        state = 0; // Keypoint 遊戲狀態 0未開始 1執行 2暫停 3通關失敗 4通關成功
        score = 0; // 遊戲分數
        count = 1; // 遊戲繪製次數(計時作用)
        enemyFrequency = 30;  // 敵機出現頻率(不同關卡速率不同)
        enemyShellFrequency = 40;  // 敵機子彈出現頻率(不同關卡速率不同)
        planeShellFrequency = 20;  // 戰機子彈出現頻率(不同關卡速率不同)
        enemyCount = 1;  // 記錄敵機敵機出現數量
        bossAppear = 50;  // 定義出現多少敵機後boss出現
        skill_release = false; // 判定技能是否已經釋放
        play_BOSS_sound = true; // 調控boss出場音效只播放一次
        play_warn_sound = true; // 調控boss出場預警音效只播放一次
        end = true; // 調控失敗或勝利音效只播放一次
        T = false; // 失敗或勝利時按T退出遊戲
        play = true; // 調控按紐二(點選播放,再點選暫停)
        prearranged = true; // 動態效果預載入
        MAAppear = false;  // 神獸出現
        Plane.goldNumber = 0; // 金幣數
        Plane.plane_life = 100;
        Plane.plane_skill = 100;
        frame.setVisible(true); // 隱藏按鈕
        if (BOSSObj != null) { // 初始化boss
            BOSSObj.setX(-500);
            BOSSObj.setY(700);
            GameUtils.removeList.add(BOSSObj);
            BOSS.number = 0;
            BOSSObj = null;
        }
        if (mAnimalsObj != null) { // 初始化神獸
            mAnimalsObj.setX(-500);
            mAnimalsObj.setY(700);
            GameUtils.removeList.add(mAnimalsObj);
            mAnimalsObj = null;
            GamePanel.MAExist = false;
        }
        Popup.select = false; // 初始化選項按鈕
        // 清空畫板殘留影象
        GameUtils.gameObjList.clear();
        GameUtils.gameObjList.add(backGroundObj);

        // 初始化戰機
        Plane.plane_number = 1;
        plane_create = true;
        repaint();
    }

    // Keypoint 繪製
    @Override
    public void paint(Graphics g) {
        // Keypoint 雙緩衝
        // 建立和容器一樣大小的Image
        if (offScreenImage == null)
            offScreenImage = this.createImage(width, height);
        Graphics gImage = offScreenImage.getGraphics(); // 獲得該圖片的畫布
        gImage.fillRect(0, 0, width, height); // 填充畫版

        // Keypoint 根據狀態關閉音效
        if (state == 2 || state == 3 || state == 4) {
            sound.stop();
            sound3.stop();
            sound4.stop();
        }

        // Keypoint 遊戲狀態
        switch (state) {
            case 0: // 遊戲未開始
                sound.start();
                sound.loop();
                gImage.drawImage(GameUtils.coverImg, 0, 0, null);
                // Keypoint 將按鈕獲取焦點的方法放在面板的重繪程式碼中(防止閃爍)
                button1.requestFocus();
                button2.requestFocus();
                button3.requestFocus();
                button4.requestFocus();
                button5.requestFocus();
                button6.requestFocus();
                break;
            case 1: // 遊戲開始
                // Keypoint 建立戰機物件
                if (plane_create) {
                    GameUtils.removeList.add(planeObj);
                    Image img = null;
                    int w = 0;
                    int h = 0;
                    switch (Plane.plane_number) {
                        case 1:
                            w = 120;
                            h = 68;
                            img = GameUtils.planeImg;
                            break;
                        case 2:
                            w = 120;
                            h = 89;
                            img = GameUtils.planeImg2;
                            break;
                        case 3:
                            w = 70;
                            h = 100;
                            img = GameUtils.planeImg3;
                            break;
                    }
                    planeObj = new Plane(img, 600, 550, w, h, 0, this);
                    plane_create = false;
                    GameUtils.gameObjList.add(planeObj);
                }
                sound4.start();
                sound4.loop();
                frame.setVisible(false); // 隱藏按鈕
                GameUtils.gameObjList.addAll(GameUtils.explodeList); // 動態效果繪製新增
                GameUtils.gameObjList.addAll(GameUtils.explodeList2);
                GameUtils.gameObjList.addAll(GameUtils.explodeList3);
                // 繪製所有遊戲物件
                try {
                    for (GameObject object : GameUtils.gameObjList)
                        object.paintSelf(gImage);
                } catch (Exception ignored) {  // FIXME 空指標異常(暫不處理)
                }
                GameUtils.gameObjList.removeAll(GameUtils.removeList); // 消失物件刪除(防止佔據記憶體過大)
                /* FIXME
                 * System.out.println("removeList1111:" + GameUtils.removeList.size());
                 * System.out.println("gameObjList1111:" + GameUtils.gameObjList.size());
                 * System.out.println("removeList2222:" + GameUtils.removeList.size());
                 * System.out.println("gameObjList2222:" + GameUtils.gameObjList.size());
                 * 目前看來應該是列表GameUtils.removeList記憶體無法清理,導致記憶體堆積
                 * 下次試試全用GameUtils.gameObjList.remove(Object o);
                 * 代替GameUtils.removeList.add(); GameUtils.gameObjList.removeAll(GameUtils.removeList);
                 */

                // boss出現預警
                if (enemyCount > bossAppear - 10 && enemyCount < bossAppear && BOSSObj == null) {
                    if (play_warn_sound) {
                        new Sound(GameUtils.basic_no12).start();
                        play_warn_sound = false;
                    }
                    gImage.drawImage(GameUtils.warning, 520, 100, null);
                }
                break;
            case 2: // 遊戲暫停
                gImage.drawImage(GameUtils.gamePauseImg, 5, 20, null);
                gImage.setColor(Color.black);
                gImage.setFont(new Font("微軟雅黑", Font.BOLD, 20));
                gImage.drawString("‘R'Menu  ‘T'Exit  ‘O'Shop", 430, 700);
                if (T) {
                    System.exit(0);
                } // 按T退出
                if (R) {
                    init();
                    R = false;
                } // 按R返回主介面
                break;
            case 3: // 遊戲失敗
                // 背景音樂關閉
                gImage.drawImage(GameUtils.gameFailureImg, 0, 0, null);
                // 改變畫筆的顏色
                gImage.setColor(Color.white);
                //改變文字大小和樣式
                gImage.setFont(new Font("微軟雅黑", Font.BOLD, 60));
                //新增文字
                gImage.drawString(score + "", 220, 268);
                gImage.drawString(Plane.plane_level + "", 334, 360);
                gImage.drawString(Plane.plane_life + "", 334, 500);
                gImage.drawString(Plane.plane_skill + "", 334, 575);

                gImage.setColor(Color.pink);
                gImage.drawString("您選擇的難度為" + Popup.level + ",", 545, 348);
                gImage.drawString("恭喜您在第" + Plane.plane_level + "級時被打爆,", 545, 448);
                gImage.drawString("可以說是十分菜!", 545, 548);

                gImage.setColor(Color.orange);
                gImage.setFont(new Font("微軟雅黑", Font.BOLD, 50));
                gImage.drawString("‘R'返回主選單  ‘T'退出遊戲", 200, 700);
                if (end) {
                    new Sound(GameUtils.basic_no7).start();
                    end = false;
                }
                if (T) {
                    System.exit(0);
                } // 按T退出
                if (R) {
                    init();
                    R = false;
                } // 按R返回主介面
                break;
            case 4: // 遊戲勝利
                // 繪畫勝利介面文字
                gImage.drawImage(GameUtils.gameVictoryImg, 5, 20, null);
                gImage.setColor(Color.white);
                // 得分
                gImage.setFont(new Font("微軟雅黑", Font.BOLD, 100));
                gImage.drawString(score + "", 200, 380);
                // 剩餘藍/紅
                gImage.setFont(new Font("微軟雅黑", Font.BOLD, 60));
                gImage.drawString(Plane.plane_life + "", 334, 535);
                gImage.drawString(Plane.plane_skill + "", 334, 610);
                // 評價
                gImage.setColor(Color.pink);
                gImage.setFont(new Font("微軟雅黑", Font.BOLD, 60));
                gImage.drawString("恭喜您在" + Popup.level + "模式中,", 560, 368);
                gImage.drawString("成功將敵人打爆,", 560, 468);
                gImage.drawString("可以說是十分犀利啊!", 560, 568);
                // R 退出
                gImage.setColor(Color.orange);
                gImage.setFont(new Font("微軟雅黑", Font.BOLD, 50));
                gImage.drawString("‘R'返回主選單  ‘T'退出遊戲", 200, 700);
                // 播放勝利音效(只播放一次)
                if (end) {
                    new Sound(GameUtils.basic_no8).start();
                    end = false;
                }
                if (T) {
                    System.exit(0);
                } // 按T退出
                if (R) {
                    init();
                    R = false;
                } // 按R返回主介面
                break;
        }

        // Keypoint 將緩衝區繪製好的圖形整個繪製到容器的畫布中
        g.drawImage(offScreenImage, 0, 0, null);
        count++;   // 畫面繪製次數自增
    }

    // Keypoint 生成遊戲物件
    public void createObj() {
        // Keypoint 生成我方子彈
        if (planeObj != null && count % planeShellFrequency == 0) {
            Image img = null;
            int x = planeObj.getX();
            if (Plane.plane_number == 1 || Plane.plane_number == 2) {
                x = planeObj.getX() + 22;
            }
            int y = planeObj.getY();
            int w = 0;
            int h = 0;
            int s = 0;
            switch (Plane.plane_level) {
                case 1:
                    img = GameUtils.planeShellImg;
                    x += 25;
                    y -= 10;
                    w = 21;
                    h = 40;
                    s = 8;
                    break;
                case 2:
                    planeShellFrequency = 15;
                    img = GameUtils.planeShellImg2;
                    x += 18;
                    y -= 15;
                    w = 28;
                    h = 27;
                    s = 10;
                    break;
                case 3:
                    planeShellFrequency = 12;
                    img = GameUtils.planeShellImg3;
                    x += 18;
                    y -= 15;
                    w = 45;
                    h = 27;
                    s = 13;
                    break;
                case 4:
                    planeShellFrequency = 12;
                    img = GameUtils.planeShellImg4;
                    x += 3;
                    y -= 30;
                    w = 65;
                    h = 33;
                    s = 13;
                    break;
                case 5:
                    planeShellFrequency = 12;
                    img = GameUtils.planeShellImg5;
                    x -= 36;
                    y -= 45;
                    w = 150;
                    h = 114;
                    s = 13;
                    break;
            }
            GameUtils.planeShellList.add(new PlaneShell(img, x, y, w, h, s, this));
            GameUtils.gameObjList.add(GameUtils.planeShellList.get(GameUtils.planeShellList.size() - 1));
        }
        // Keypoint 生成敵方子彈
        if (count % enemyShellFrequency == 0 && BOSSObj != null) {
            Image img = null;
            int x = BOSSObj.getX();
            int y = BOSSObj.getY();
            int w = 0;
            int h = 0;
            int s = 0;
            switch (BOSS.number) {
                case 1:
                    img = GameUtils.enemyShellImg;
                    x += 110;
                    y += 92;
                    w = 47;
                    h = 40;
                    s = 6;
                    break;
                case 2:
                    enemyShellFrequency = 25;
                    img = GameUtils.enemyShellImg2;
                    x += 50;
                    y += 130;
                    w = 25;
                    h = 25;
                    s = 8;
                    break;
                case 3:
                    enemyShellFrequency = 25;
                    img = GameUtils.enemyShellImg3;
                    x += 60;
                    y += 145;
                    w = 51;
                    h = 51;
                    s = 8;
                    break;
                case 4:
                    enemyShellFrequency = 30;
                    img = GameUtils.enemyShellImg4;
                    x += 60;
                    y += 90;
                    w = 105;
                    h = 50;
                    s = 6;
                    break;
                case 5:
                    enemyShellFrequency = 30;
                    img = GameUtils.enemyShellImg5;
                    x += 55;
                    y += 100;
                    w = 135;
                    h = 140;
                    s = 8;
                    break;
            }
            GameUtils.enemyShellList.add(new EnemyShell(img, x, y, w, h, s, this));
            GameUtils.gameObjList.add(GameUtils.enemyShellList.get(GameUtils.enemyShellList.size() - 1));
        }
        // Keypoint 生成敵機
        if (count % enemyFrequency == 0) {
            Image img = null;
            int w = 0;
            int h = 0;
            int s = 0;
            switch (Plane.plane_level) {
                case 1:
                    img = GameUtils.enemy1Img;
                    w = 128;
                    h = 128;
                    s = 6;
                    break;
                case 2:
                    sound3.start();
                    sound3.loop();
                    enemyFrequency = 22;
                    img = GameUtils.enemy2Img;
                    w = 63;
                    h = 46;
                    s = 9;
                    break;
                case 3:
                    enemyFrequency = 17;
                    img = GameUtils.enemy2Img;
                    w = 63;
                    h = 46;
                    s = 12;
                    break;
                case 4:
                    sound3.stop();
                    enemyFrequency = 25;
                    img = GameUtils.enemy3Img;
                    w = 100;
                    h = 57;
                    s = 8;
                    break;
                case 5:
                    enemyFrequency = 25;
                    img = GameUtils.enemy4Img;
                    w = 120;
                    h = 57;
                    s = 8;
                    break;
            }
            // x = (int) (Math.random() * 9) * 138;    // 敵機生成時隨機x座標
            GameUtils.enemyList.add(new Enemy(img, (int) (Math.random() * 9) * 138, -100, w, h, s, this));
            GameUtils.gameObjList.add(GameUtils.enemyList.get(GameUtils.enemyList.size() - 1));
            enemyCount++;
        }
        // Keypoint 生成敵機2(第三關)
        if (count % 40 == 0 && (BOSS.number == 4 || BOSS.number == 5)) {
            Image img;
            int w, h, s;
            if (BOSS.number == 4) {
                img = GameUtils.enemy4Img;
                w = 120;
                h = 57;
                s = 7;
            } else {
                img = GameUtils.enemy3Img;
                w = 100;
                h = 57;
                s = 6;
            }
            GameUtils.enemyList.add(new Enemy(img, (int) (Math.random() * 9) * 138, -100, w, h, s, this));
            GameUtils.gameObjList.add(GameUtils.enemyList.get(GameUtils.enemyList.size() - 1));
            enemyCount++;
        }

        // Keypoint 生成BOSS
        if (enemyCount % bossAppear == 0 && BOSSObj == null) {
            play_warn_sound = true;
            BOSS.number += 1;
            Image img = null;
            int w = 0;
            int h = 0;
            int s = 0;
            switch (BOSS.number) {
                case 1:
                    if (play_BOSS_sound) {
                        new Sound(GameUtils.run_no3).start();
                        play_BOSS_sound = false;
                    }
                    img = GameUtils.bossImg;
                    w = 234;
                    h = 170;
                    s = 3;
                    bossAppear = 100;
                    break;
                case 2:
                    if (!play_BOSS_sound) {
                        new Sound(GameUtils.run2_no2).start();
                        play_BOSS_sound = true;
                    }
                    img = GameUtils.bossImg2;
                    w = 125;
                    h = 157;
                    s = 4;
                    bossAppear = 100;
                    break;
                case 3:
                    if (play_BOSS_sound) {
                        new Sound(GameUtils.run2_no3).start();
                        play_BOSS_sound = false;
                    }
                    img = GameUtils.bossImg3;
                    w = 151;
                    h = 165;
                    s = 5;
                    bossAppear = 90;
                    break;
                case 4:
                    if (!play_BOSS_sound) {
                        new Sound(GameUtils.run3_no3).start();
                        play_BOSS_sound = true;
                    }
                    img = GameUtils.bossImg4;
                    w = 234;
                    h = 98;
                    s = 6;
                    bossAppear = 80;
                    break;
                case 5:
                    if (play_BOSS_sound) {
                        new Sound(GameUtils.run3_no4).start();
                        play_BOSS_sound = false;
                    }
                    img = GameUtils.bossImg5;
                    w = 234;
                    h = 114;
                    s = 6;
                    break;
            }
            BOSSObj = new BOSS(img, 250, 30, w, h, s, this);
            GameUtils.gameObjList.add(BOSSObj);
        }
        // Keypoint 生成禮物
        if (count % 500 == 0) {
            // x = (int) (Math.random() * 9) * 138;   禮物生成時隨機x座標
            GameUtils.giftList.add(new Gift(GameUtils.giftImg1, (int) (Math.random() * 9) * 138, -100, 60, 102, 4, this));
            GameUtils.gameObjList.add(GameUtils.giftList.get(GameUtils.giftList.size() - 1));
        }
        // Keypoint 生成禮物2(隨機技能)
        if (count % 850 == 0) {
            GameUtils.giftList2.add(new Gift2(GameUtils.giftImg2, (int) (Math.random() * 9) * 138, -100, 65, 63, 5, this));
            GameUtils.gameObjList.add(GameUtils.giftList2.get(GameUtils.giftList2.size() - 1));
        }
        // Keypoint 繪製技能
        if (Skill.release) {
            Skill.release = false;
            skill_release = true;
            assert planeObj != null;
            int x = planeObj.getX();
            int y = planeObj.getY();
            int w = 0;
            int h = 0;
            int s = 0;
            Image img = null;
            // Keypoint 標準技能
            switch (Skill.number) {
                case 1:
                    if (Plane.plane_skill >= skillConsume) {
                        Plane.plane_skill -= skillConsume;
                        // Keypoint 隨機技能
                        switch (Gift2.number) {
                            case 4:
                                x -= 40;
                                y -= 40;
                                w = 133;
                                h = 180;
                                s = 15;
                                img = GameUtils.skillImg4; // 雷電火卷 -1
                                new Sound(GameUtils.skill_no2).start();
                                break;
                            case 5:
                                x -= 50;
                                y -= 20;
                                w = 166;
                                h = 201;
                                s = 10;
                                img = GameUtils.skillImg5; // 火龍捲 -1 (跟隨戰機x移動)
                                new Sound(GameUtils.skill_no7).start();
                                break;
                            case 6:
                                x -= 60;
                                y -= 20;
                                w = 201;
                                h = 166;
                                s = 20;
                                img = GameUtils.skillImg6; // 閃電刃 -1
                                new Sound(GameUtils.skill_no3).start();
                                break;
                            case 7:
                                x -= 60;
                                y -= 80;
                                w = 201;
                                h = 201;
                                s = 5;
                                img = GameUtils.skillImg7; // 黑洞 -3
                                new Sound(GameUtils.skill_no4).start();
                                break;
                            case 8:
                                x -= 40;
                                y -= 80;
                                w = 157;
                                h = 165;
                                s = 10;
                                img = GameUtils.skillImg8; // 火星球 -2 (跟蹤boss)
                                new Sound(GameUtils.skill_no6).start();
                                break;
                            case 9:
                                x -= 40;
                                y -= 50;
                                w = 166;
                                h = 201;
                                s = 15;
                                img = GameUtils.skillImg9; // 星耀光環 -1
                                new Sound(GameUtils.skill_no5).start();
                                break;
                            case 10:
                            default:
                                x -= 60;
                                y -= 10;
                                w = 200;
                                h = 93;
                                s = 12;
                                img = GameUtils.skillImg1; // 不死鳥 -1
                                new Sound(GameUtils.skill_no1).start();
                                break;
                        }
                    }
                    break;
                case 2:
                    if (Plane.plane_skill >= skillConsume) {
                        Plane.plane_skill -= skillConsume;
                        x -= 60;
                        y -= 20;
                        img = null;
                        new Sound(GameUtils.skill_no8).start();
                    }
                    break;
                case 3:
                    x -= 15;
                    img = GameUtils.skillImg3;
                    new Sound(GameUtils.skill_no9).start();
                    break;
            }
            GameUtils.skillList.add(new Skill(img, x, y, w, h, s, this));
            GameUtils.gameObjList.add(GameUtils.skillList.get(GameUtils.skillList.size() - 1));
            Skill.release = false;
        }

        // Keypoint 神獸
        if (MAAppear) {
            MAAppear = false;
            Image img = null;
            int w = 63;
            int h = 63;
            int s = 0;
            switch (MAnimals.MAnimalsNumber) {
                case 1:
                    s = 5;
                    img = GameUtils.commodityImg1;
                    break;
                case 2:
                    s = 8;
                    img = GameUtils.commodityImg2;
                    break;
                case 3:
                    s = 15;
                    img = GameUtils.commodityImg3;
                    break;
            }
            assert planeObj != null;
            mAnimalsObj = new MAnimals(img, planeObj.getX(), planeObj.getY(), w, h, s, this);
            GameUtils.gameObjList.add(mAnimalsObj);
            MAExist = true;
        }

        // Keypoint 飛機回血/回藍
        if (count % lifeRecovery == 0 && Plane.plane_life < Plane.record_plane_life)
            Plane.plane_life++;
        if (count % skillRecovery == 0 && Plane.plane_skill < Plane.record_plane_skill)
            Plane.plane_skill++;
    }
}

商店類

public class Popup_Shop extends JFrame {
    boolean buy = false;
    int price1 = 500, price2 = 799, price3 = 888, price4 = 800, price5 = 800;

    public Popup_Shop() {
        new Sound(GameUtils.basic_no11).start();
        init();
    }

    public void init() {
        // 說明
        JLabel jLabel = new JLabel("選擇購買商品:");
        jLabel.setFont(new Font("acetone-family", Font.BOLD, 15));
        jLabel.setBounds(10, 10, 200, 20);

        JLabel jLabel2 = new JLabel("-------------------- 當前金幣數:" + Plane.goldNumber + "  --------------------");
        jLabel2.setFont(new Font("acetone-family", Font.BOLD, 15));
        jLabel2.setBounds(130, 150, 800, 20);

        // 標籤文字
        JRadioButton option1 = new JRadioButton("吞天紫金貂(" + price1 + "$)");
        JRadioButton option2 = new JRadioButton("九幽冥鳳(" + price2 + "$)");
        JRadioButton option3 = new JRadioButton("太虛古龍(" + price3 + "$)");
        JRadioButton option4 = new JRadioButton("血量+1(" + price4 + "$)");
        JRadioButton option5 = new JRadioButton("技能+1(" + price5 + "$)");
        option1.setBounds(5, 40, 132, 20);
        option2.setBounds(140, 40, 120, 20);
        option3.setBounds(265, 40, 120, 20);
        option4.setBounds(385, 40, 120, 20);
        option5.setBounds(505, 40, 120, 20);

        // 單選
        ButtonGroup group = new ButtonGroup();
        group.add(option1);
        group.add(option2);
        group.add(option3);
        group.add(option4);
        group.add(option5);

        // 標籤圖片
        JLabel commodity1 = new JLabel(new ImageIcon(GameUtils.commodityImg1));
        JLabel commodity2 = new JLabel(new ImageIcon(GameUtils.commodityImg2));
        JLabel commodity3 = new JLabel(new ImageIcon(GameUtils.commodityImg3));
        JLabel commodity4 = new JLabel(new ImageIcon(GameUtils.commodityImg4));
        JLabel commodity5 = new JLabel(new ImageIcon(GameUtils.commodityImg5));
        commodity1.setBounds(45, 80, 63, 63);
        commodity2.setBounds(165, 80, 63, 63);
        commodity3.setBounds(285, 80, 63, 63);
        commodity4.setBounds(405, 80, 63, 63);
        commodity5.setBounds(525, 80, 63, 63);

        // 購買按鈕
        JPanel jPanel = new JPanel();
        JButton button = new JButton("購買");
        button.setFont(new Font("acetone-family", Font.BOLD, 20));
        jPanel.setLayout(null);
        button.setBounds(255, 180, 90, 50);
        jPanel.add(button);
        jPanel.setVisible(true);

        // 將元件新增進面板
        this.add(jLabel);
        this.add(jLabel2);
        this.add(option1);
        this.add(option2);
        this.add(option3);
        this.add(option4);
        this.add(option5);
        this.add(commodity1);
        this.add(commodity2);
        this.add(commodity3);
        this.add(commodity4);
        this.add(commodity5);
        this.add(jPanel);

        // 面板設定
        this.setTitle("超級神祕商店");
        this.setVisible(true);
        this.setResizable(false);
        this.setSize(630, 280);
        this.setLocationRelativeTo(null);
        this.repaint();

        // 監聽
        button.addActionListener(e -> {
            if (option1.isSelected() && Plane.goldNumber >= price1) {
                buy = true;
                if (!GamePanel.MAExist) {
                    Plane.goldNumber -= price1;
                    MAnimals.MAnimalsNumber = 1;
                    GamePanel.MAAppear = true;
                    System.out.println("購買:吞天紫金貂(" + price1 + "$)");
                }
            } else if (option2.isSelected() && Plane.goldNumber >= price2) {
                buy = true;
                if (!GamePanel.MAExist) {
                    Plane.goldNumber -= price2;
                    MAnimals.MAnimalsNumber = 2;
                    GamePanel.MAAppear = true;
                    System.out.println("購買:九幽冥鳳(" + price2 + "$)");
                }
            } else if (option3.isSelected() && Plane.goldNumber >= price3) {
                buy = true;
                if (!GamePanel.MAExist) {
                    Plane.goldNumber -= price3;
                    MAnimals.MAnimalsNumber = 3;
                    GamePanel.MAAppear = true;
                    System.out.println("購買:太虛古龍(" + price3 + "$)");
                }
            } else if (option4.isSelected() && Plane.goldNumber >= price4) {
                buy = true;
                Plane.goldNumber -= price4;
                Plane.record_plane_life += 5;
                Plane.plane_life += 100;
                System.out.println("購買:(" + price4 + "$)");
            } else if (option5.isSelected() && Plane.goldNumber >= price5) {
                buy = true;
                Plane.goldNumber -= price5;
                Plane.record_plane_skill += 5;
                Plane.plane_skill += 100;
                System.out.println("購買:技能+1(" + price5 + "$)");
            }
            new Sound(GameUtils.basic_no10).start();

            // 提示彈窗
            boolean select = (option1.isSelected() || option2.isSelected() || option3.isSelected() || option4.isSelected() || option5.isSelected());

            if (!select) {
                JOptionPane.showMessageDialog(null, "請選擇所需商品!", "超級神祕商店溫馨提醒", JOptionPane.WARNING_MESSAGE);
            } else if (!buy) {
                JOptionPane.showMessageDialog(null, "兄弟,金幣不足啊!", "超級神祕商店溫馨提醒", JOptionPane.ERROR_MESSAGE);
            } else if ((option1.isSelected() || option2.isSelected() || option3.isSelected()) && GamePanel.MAExist) {
                JOptionPane.showMessageDialog(null, "已存在一隻神獸,不可貪多哦~", "超級神祕商店溫馨提醒", JOptionPane.ERROR_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(null, "購買成功!!" + "剩餘金幣:" + Plane.goldNumber, "超級神祕商店溫馨提醒", JOptionPane.INFORMATION_MESSAGE);
                this.dispose();
            }
        });
    }
}

總結

通過此次的《飛機大戰-III》遊戲實現,讓我對swing的相關知識有了進一步的瞭解,對java這門語言也有了比以前更深刻的認識。

java的一些基本語法,比如資料型別、運運算元、程式流程控制和陣列等,理解更加透徹。java最核心的核心就是物件導向思想,對於這一個概念,終於悟到了一些。

以上就是Java實現遊戲飛機大戰-III的範例程式碼的詳細內容,更多關於Java飛機大戰的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com