Java實(shí)現(xiàn)游戲飛機(jī)大戰(zhàn)-III的示例代碼
前言
《飛機(jī)大戰(zhàn)-III》是一款融合了街機(jī)、競(jìng)技等多種元素的經(jīng)典射擊手游。華麗精致的游戲畫面,超炫帶感的技能特效,超火爆畫面讓你腎上腺素爆棚,給你帶來全方位震撼感受,體驗(yàn)飛行戰(zhàn)斗的無限樂趣。
游戲是用java語言實(shí)現(xiàn),采用了swing技術(shù)進(jìn)行了界面化處理,設(shè)計(jì)思路用了面向?qū)ο笏枷搿?/p>
主要需求
基于Java Swing,以飛機(jī)大戰(zhàn)為原形,以抗日電視劇《亮劍》中的“李云龍”為主題,實(shí)現(xiàn)菜單、選關(guān)、難度、等級(jí)、技能等功能。
主要設(shè)計(jì)
①進(jìn)入游戲后,請(qǐng)按下shift鍵(將鍵盤改為英文模式)
②技能為數(shù)字1(攻擊)、2(治愈)、3(蓄氣)
③數(shù)字4、5、6、7、8為創(chuàng)意音效,空格鍵為暫停
④使用1、2技能會(huì)消耗相應(yīng)的技能藍(lán)條,3技能消耗一定血量恢復(fù)一定藍(lán)
⑤觸碰游戲中隨機(jī)出現(xiàn)的降落傘彈藥可以回藍(lán)
⑥觸碰游戲中隨機(jī)出現(xiàn)的閃光粒子可以隨機(jī)更換1技能特效,且血量與藍(lán)加2
⑦由鼠標(biāo)控制角色位置,角色會(huì)自動(dòng)發(fā)射相應(yīng)等級(jí)的子彈
⑧每打敗一個(gè)boss,角色升一級(jí),子彈同行升級(jí)
⑨每過一關(guān),角色血量和藍(lán)自動(dòng)增大并加滿
⑩按下空格暫停游戲
本游戲該版本共三個(gè)關(guān)卡和三種難度,任君挑選
功能截圖
游戲開始
選擇戰(zhàn)機(jī)
開始游戲
代碼實(shí)現(xiàn)
游戲面板類
把各游戲?qū)ο蠹欣L制到此面板
public class GamePanel extends JFrame { // Keypoint 基本變量定義 Image offScreenImage = null; // 定義雙緩存 int width = 1250, height = 725; // 窗口寬高 public static int state = 0; // Keypoint 游戲狀態(tài) 0未開始 1運(yùn)行 2暫停 3通關(guān)失敗 4通關(guān)成功 public static int score = 0; // 游戲分?jǐn)?shù) public static int count = 1; // 游戲繪制次數(shù)(計(jì)時(shí)作用) static int enemyFrequency = 30; // 敵機(jī)出現(xiàn)頻率(不同關(guān)卡速率不同) static int enemyShellFrequency = 40; // 敵機(jī)子彈出現(xiàn)頻率(不同關(guān)卡速率不同) static int planeShellFrequency = 20; // 戰(zhàn)機(jī)子彈出現(xiàn)頻率(不同關(guān)卡速率不同) public static int enemyCount = 1; // 記錄敵機(jī)敵機(jī)出現(xiàn)數(shù)量 static int bossAppear = 50; // 定義出現(xiàn)多少敵機(jī)后boss出現(xiàn) public static boolean skill_release = false; // 判定技能是否已經(jīng)釋放 static boolean play_BOSS_sound = true; // 調(diào)控boss出場(chǎng)音效只播放一次 static boolean play_warn_sound = true; // 調(diào)控boss出場(chǎng)預(yù)警音效只播放一次 static boolean end = true; // 調(diào)控失敗或勝利音效只播放一次 static boolean T = false; // 失敗或勝利時(shí)按T退出游戲 public static boolean R = false; // 返回主界面 static boolean play = true; // 調(diào)控按紐二(點(diǎn)擊播放,再點(diǎn)擊暫停) static boolean prearranged = true; // 動(dòng)態(tài)效果預(yù)加載 public static boolean MAAppear = false; // 神獸出現(xiàn) public static boolean MAExist = false; // 神獸存在否 public static boolean plane_create = false; // 飛機(jī)創(chuàng)建 // 難度 static int skillConsume = 2; // 2 5 10 用技能消耗藍(lán) static int skillRecovery = 100; // 100 150 999 回藍(lán)速度 static int lifeRecovery = 50; // 50 90 500 回血速度 // Keypoint 元素實(shí)體定義 // 背景實(shí)體類 public GameObject backGroundObj = new BackGround(GameUtils.bgImg, 0, -1900, 2, this); // 戰(zhàn)機(jī)實(shí)體類 public GameObject planeObj = new Plane(GameUtils.planeImg, 600, 550, 70, 100, 0, this); // boss實(shí)體類 public GameObject BOSSObj = null; // 神獸實(shí)體類 public GameObject mAnimalsObj = null; // Keypoint 按鈕聲明 JButton button1 = new JButton("開始游戲"); JButton button2 = new JButton("聽聽歌吧"); JButton button3 = new JButton("退出游戲"); JButton button4 = new JButton("選擇難度\\關(guān)卡"); JButton button5 = new JButton("游戲說明"); JButton button6 = new JButton("選擇戰(zhàn)機(jī)"); 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); // 第二關(guān)背景音樂(無人機(jī)群) 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) { // 如果返回主界面沒有選擇關(guān)卡,默認(rèn)初始化第一關(guān) 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, "親愛的云龍戰(zhàn)士,歡迎來到開炮大陸?。n" + "-----------------------------------------------------2021-12-01-----------------------------------------------------\n" + "下面是操作說明:\n" + " ①進(jìn)入游戲后,請(qǐng)按下shift鍵(將鍵盤改為英文模式)\n" + " ②技能為數(shù)字1(攻擊)、2(治愈)、3(蓄氣)\n" + " ③數(shù)字4、5、6、7、8為創(chuàng)意音效,空格鍵為暫停\n" + " ④使用1、2技能會(huì)消耗相應(yīng)的技能藍(lán)條,3技能消耗一定血量恢復(fù)一定藍(lán)\n" + " ⑤觸碰游戲中隨機(jī)出現(xiàn)的降落傘彈藥可以回藍(lán)\n" + " ⑥觸碰游戲中隨機(jī)出現(xiàn)的閃光粒子可以隨機(jī)更換1技能特效,且血量與藍(lán)加2\n" + " ⑦由鼠標(biāo)控制角色位置,角色會(huì)自動(dòng)發(fā)射相應(yīng)等級(jí)的子彈\n" + " ⑧每打敗一個(gè)boss,角色升一級(jí),子彈同行升級(jí)\n" + " ⑨每過一關(guān),角色血量和藍(lán)自動(dòng)增大并加滿\n" + " ⑩按下空格暫停游戲\n" + "本游戲該版本共三個(gè)關(guān)卡和三種難度,任君挑選\n" + "-----------------------------------------------------2021-12-07-----------------------------------------------------\n" + "更新:\n" + " ①在暫停、失敗、勝利界面可以按'R‘返回主界面,按‘T'退出游戲\n" + " ②新增在暫停界面按‘O'進(jìn)入商店\n" + " ③w s a d上下左右控制神獸移動(dòng)\n" + "-----------------------------------------------------2021-12-08-----------------------------------------------------\n" + "更新:\n" + " ①敵機(jī)會(huì)根據(jù)戰(zhàn)機(jī)的位置進(jìn)行跟蹤移動(dòng)\n" + " ②修改部分技能效果\n" + "-----------------------------------------------------2021-12-11-----------------------------------------------------\n" + "更新:\n" + " ①新增初始菜單選擇戰(zhàn)機(jī)功能\n" + "-----------------------------------------------------2021-12-13-----------------------------------------------------\n" + " ①修復(fù)一些已知問題\n" + "-------------------------------------------------------------------------------------------------------------------------\n" + "\n" + "注意事項(xiàng):\n" + " 如果在游戲中遇到任何問題,請(qǐng)關(guā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); // 把開始界面按鈕添加進(jìn)主面板 launch(); } // Keypoint 啟動(dòng) public void launch() { this.setFocusable(true); this.setVisible(true); this.setBounds(0, 0, width, height); this.setTitle("云龍開炮4.0"); this.setIconImage(GameUtils.icon); // 程序圖標(biāo) this.setResizable(false); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // 將游戲?qū)ο筇砑拥酱蠹? GameUtils.gameObjList.add(backGroundObj); GameUtils.gameObjList.add(planeObj); // 鼠標(biāo)監(jiān)聽 this.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { System.out.println("鼠標(biāo)監(jiān)聽按下 ——> " + e.getButton()); if (e.getButton() == 3) { System.out.println("釋放技能1"); // 釋放技能(攻擊) Skill.release = true; Skill.number = 1; } } }); // Keypoint 鍵盤監(jiān)聽(暫停、釋放技能) this.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { super.keyPressed(e); switch (e.getKeyCode()) { case 32: // 空格暫停 switch (state) { //運(yùn)行改為暫停 case 1: System.out.println("游戲暫停"); state = 2; sound.stop(); sound3.stop(); sound4.stop(); break; //暫停改為運(yùn)行 case 2: System.out.println("繼續(xù)游戲"); 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: // 監(jiān)聽R鍵 if (state == 2 || state == 3 || state == 4) R = true; break; case 84: // 監(jiān)聽T鍵 if (state == 2 || state == 3 || state == 4) T = true; break; case 79: // 監(jiān)聽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("鍵盤監(jiān)聽按下 ——> " + 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 游戲狀態(tài) 0未開始 1運(yùn)行 2暫停 3通關(guān)失敗 4通關(guān)成功 score = 0; // 游戲分?jǐn)?shù) count = 1; // 游戲繪制次數(shù)(計(jì)時(shí)作用) enemyFrequency = 30; // 敵機(jī)出現(xiàn)頻率(不同關(guān)卡速率不同) enemyShellFrequency = 40; // 敵機(jī)子彈出現(xiàn)頻率(不同關(guān)卡速率不同) planeShellFrequency = 20; // 戰(zhàn)機(jī)子彈出現(xiàn)頻率(不同關(guān)卡速率不同) enemyCount = 1; // 記錄敵機(jī)敵機(jī)出現(xiàn)數(shù)量 bossAppear = 50; // 定義出現(xiàn)多少敵機(jī)后boss出現(xiàn) skill_release = false; // 判定技能是否已經(jīng)釋放 play_BOSS_sound = true; // 調(diào)控boss出場(chǎng)音效只播放一次 play_warn_sound = true; // 調(diào)控boss出場(chǎng)預(yù)警音效只播放一次 end = true; // 調(diào)控失敗或勝利音效只播放一次 T = false; // 失敗或勝利時(shí)按T退出游戲 play = true; // 調(diào)控按紐二(點(diǎn)擊播放,再點(diǎn)擊暫停) prearranged = true; // 動(dòng)態(tài)效果預(yù)加載 MAAppear = false; // 神獸出現(xiàn) Plane.goldNumber = 0; // 金幣數(shù) 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; // 初始化選項(xiàng)按鈕 // 清空畫板殘留圖像 GameUtils.gameObjList.clear(); GameUtils.gameObjList.add(backGroundObj); // 初始化戰(zhàn)機(jī) Plane.plane_number = 1; plane_create = true; repaint(); } // Keypoint 繪制 @Override public void paint(Graphics g) { // Keypoint 雙緩沖 // 創(chuàng)建和容器一樣大小的Image if (offScreenImage == null) offScreenImage = this.createImage(width, height); Graphics gImage = offScreenImage.getGraphics(); // 獲得該圖片的畫布 gImage.fillRect(0, 0, width, height); // 填充畫版 // Keypoint 根據(jù)狀態(tài)關(guān)閉音效 if (state == 2 || state == 3 || state == 4) { sound.stop(); sound3.stop(); sound4.stop(); } // Keypoint 游戲狀態(tài) switch (state) { case 0: // 游戲未開始 sound.start(); sound.loop(); gImage.drawImage(GameUtils.coverImg, 0, 0, null); // Keypoint 將按鈕獲取焦點(diǎn)的方法放在面板的重繪代碼中(防止閃爍) button1.requestFocus(); button2.requestFocus(); button3.requestFocus(); button4.requestFocus(); button5.requestFocus(); button6.requestFocus(); break; case 1: // 游戲開始 // Keypoint 創(chuàng)建戰(zhàn)機(jī)對(duì)象 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); // 動(dòng)態(tài)效果繪制添加 GameUtils.gameObjList.addAll(GameUtils.explodeList2); GameUtils.gameObjList.addAll(GameUtils.explodeList3); // 繪制所有游戲?qū)ο? try { for (GameObject object : GameUtils.gameObjList) object.paintSelf(gImage); } catch (Exception ignored) { // FIXME 空指針異常(暫不處理) } GameUtils.gameObjList.removeAll(GameUtils.removeList); // 消失對(duì)象刪除(防止占據(jù)內(nèi)存過大) /* 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()); * 目前看來應(yīng)該是列表GameUtils.removeList內(nèi)存無法清理,導(dǎo)致內(nèi)存堆積 * 下次試試全用GameUtils.gameObjList.remove(Object o); * 代替GameUtils.removeList.add(); GameUtils.gameObjList.removeAll(GameUtils.removeList); */ // boss出現(xiàn)預(yù)警 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: // 游戲失敗 // 背景音樂關(guān)閉 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 + "級(jí)時(shí)被打爆,", 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); // 剩余藍(lán)/紅 gImage.setFont(new Font("微軟雅黑", Font.BOLD, 60)); gImage.drawString(Plane.plane_life + "", 334, 535); gImage.drawString(Plane.plane_skill + "", 334, 610); // 評(píng)價(jià) 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 將緩沖區(qū)繪制好的圖形整個(gè)繪制到容器的畫布中 g.drawImage(offScreenImage, 0, 0, null); count++; // 畫面繪制次數(shù)自增 } // Keypoint 生成游戲?qū)ο? 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 生成敵機(jī) 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; // 敵機(jī)生成時(shí)隨機(jī)x坐標(biāo) 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 生成敵機(jī)2(第三關(guān)) 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; 禮物生成時(shí)隨機(jī)x坐標(biāo) 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(隨機(jī)技能) 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 標(biāo)準(zhǔn)技能 switch (Skill.number) { case 1: if (Plane.plane_skill >= skillConsume) { Plane.plane_skill -= skillConsume; // Keypoint 隨機(jī)技能 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 (跟隨戰(zhàn)機(jī)x移動(dòng)) 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; // 星耀光環(huán) -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 飛機(jī)回血/回藍(lán) 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("-------------------- 當(dāng)前金幣數(shù):" + Plane.goldNumber + " --------------------"); jLabel2.setFont(new Font("acetone-family", Font.BOLD, 15)); jLabel2.setBounds(130, 150, 800, 20); // 標(biāo)簽文字 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); // 標(biāo)簽圖片 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); // 將組件添加進(jìn)面板 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); // 面板設(shè)置 this.setTitle("超級(jí)神秘商店"); this.setVisible(true); this.setResizable(false); this.setSize(630, 280); this.setLocationRelativeTo(null); this.repaint(); // 監(jiān)聽 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, "請(qǐng)選擇所需商品!", "超級(jí)神秘商店溫馨提醒", JOptionPane.WARNING_MESSAGE); } else if (!buy) { JOptionPane.showMessageDialog(null, "兄弟,金幣不足啊!", "超級(jí)神秘商店溫馨提醒", JOptionPane.ERROR_MESSAGE); } else if ((option1.isSelected() || option2.isSelected() || option3.isSelected()) && GamePanel.MAExist) { JOptionPane.showMessageDialog(null, "已存在一只神獸,不可貪多哦~", "超級(jí)神秘商店溫馨提醒", JOptionPane.ERROR_MESSAGE); } else { JOptionPane.showMessageDialog(null, "購買成功??!" + "剩余金幣:" + Plane.goldNumber, "超級(jí)神秘商店溫馨提醒", JOptionPane.INFORMATION_MESSAGE); this.dispose(); } }); } }
總結(jié)
通過此次的《飛機(jī)大戰(zhàn)-III》游戲?qū)崿F(xiàn),讓我對(duì)swing的相關(guān)知識(shí)有了進(jìn)一步的了解,對(duì)java這門語言也有了比以前更深刻的認(rèn)識(shí)。
java的一些基本語法,比如數(shù)據(jù)類型、運(yùn)算符、程序流程控制和數(shù)組等,理解更加透徹。java最核心的核心就是面向?qū)ο笏枷?,?duì)于這一個(gè)概念,終于悟到了一些。
以上就是Java實(shí)現(xiàn)游戲飛機(jī)大戰(zhàn)-III的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Java飛機(jī)大戰(zhàn)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- java版飛機(jī)大戰(zhàn)實(shí)戰(zhàn)項(xiàng)目詳細(xì)步驟
- java實(shí)現(xiàn)飛機(jī)大戰(zhàn)案例詳解
- java實(shí)戰(zhàn)之飛機(jī)大戰(zhàn)小游戲(源碼加注釋)
- Java實(shí)現(xiàn)飛機(jī)大戰(zhàn)-連接數(shù)據(jù)庫并把得分寫入數(shù)據(jù)庫
- java實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲
- 一天時(shí)間用Java寫了個(gè)飛機(jī)大戰(zhàn)游戲,朋友直呼高手
- Java實(shí)現(xiàn)經(jīng)典游戲飛機(jī)大戰(zhàn)-I的示例代碼
- 基于Java語言在窗體上實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲的完整步驟
- Java實(shí)現(xiàn)飛機(jī)大戰(zhàn)-II游戲詳解
- java實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲
相關(guān)文章
Spring思維導(dǎo)圖助你輕松學(xué)習(xí)Spring
這篇文章主要為大家詳細(xì)介紹了Spring思維導(dǎo)圖,幫助你輕松學(xué)習(xí)Spring的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03Java實(shí)戰(zhàn)之王者榮耀的英雄是怎么產(chǎn)生的?
這篇文章主要介紹了Java實(shí)戰(zhàn)之王者榮耀的英雄是怎么產(chǎn)生的?文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05java 實(shí)現(xiàn) stack詳解及實(shí)例代碼
這篇文章主要介紹了java 實(shí)現(xiàn) stack詳解的相關(guān)資料,需要的朋友可以參考下2016-09-09Springboot-注解-操作日志的實(shí)現(xiàn)方式
這篇文章主要介紹了Springboot-注解-操作日志的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03