如何使用Java語言編寫打地鼠游戲全過程
關(guān)于游戲的介紹
打地鼠游戲是一款非常受歡迎的敏捷類游戲,它的基本規(guī)則簡單易懂,同時又充滿了挑戰(zhàn)性和趣味性。
在游戲中,玩家通常需要在一個方形區(qū)域內(nèi),面對多個地洞,這些地洞中會不定時地冒出地鼠。玩家的主要任務(wù)就是在地鼠冒出頭來的短時間內(nèi),用工具(如錘子)迅速而準(zhǔn)確地?fù)舸蛩鼈?。成功打中地鼠,玩家就能獲得相應(yīng)的分?jǐn)?shù)。
玩者可以自己設(shè)置游戲的難度,地鼠的出現(xiàn)頻率和速度都會加快,這就要求玩家不僅要有快速的反應(yīng)能力,還需要有良好的手眼協(xié)調(diào)能力和預(yù)判能力。
打地鼠游戲不僅鍛煉了玩家的反應(yīng)速度和手眼協(xié)調(diào)能力,同時也考驗了他們的注意力和耐心。因此,無論是孩子還是成年人,都能在這款游戲中找到屬于自己的樂趣和挑戰(zhàn)。
《打地鼠游戲》Java代碼的實現(xiàn)
PlayMouse.java
package com.briup.game; import java.awt.BorderLayout; import java.awt.Cursor; import java.awt.GridLayout; import java.awt.Image; import java.awt.Point; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.File; import java.net.MalformedURLException; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer; /** * 打地鼠 1.布局 2.功能 * */ public class PlayMouse implements ActionListener { // 構(gòu)建容器 private JFrame jFrame; // 構(gòu)建面板northJpanel centerJpanel private JPanel northJPanel, centerJPanel; // 構(gòu)建六個標(biāo)簽,分別用來顯示等級,分?jǐn)?shù),時間,音效 private JLabel leveLabel, timeLabel, timeVaLabel, centerJLabel, cenVaLabel, musicLabel; // 構(gòu)建下拉框,分別用來顯示等級、音效 private JComboBox<String> jComboBox, musicBox; // 構(gòu)建開始按鈕 private JButton jButton; // 構(gòu)建按鈕組,保存十六個按鈕 private JButton[] btsButtons; // 構(gòu)建時間定時器、老鼠定時器 private Timer timer, mouseTimer; // 構(gòu)建老鼠、老鼠洞圖片 private ImageIcon holeiImage, mouseImgIcon; // 記錄上一次老鼠出現(xiàn)的位置 private int last; // 設(shè)置游戲等級 初級:900毫秒 中級:600毫秒 高級:100毫秒 private int level; // 記錄老鼠是否加過分 flase:新老鼠 ,沒加過分 true:舊老鼠 private boolean flag; 鼠標(biāo)的形狀 private Cursor cursor, cursor2; // 文件對象 private File file; // 背景音樂對象 private MusicPlay play; // 游戲暫停按鈕 private JButton pauseButton; private boolean isPaused; // 構(gòu)建游戲繼續(xù)按鈕 private JButton resumeButton; // 初始化JFrame public PlayMouse() { // 1.創(chuàng)建容器 jFrame = new JFrame("打地鼠"); // 2.創(chuàng)建面板 northJPanel = new JPanel(); centerJPanel = new JPanel(); // 3.設(shè)置布局管理器 // JFrame默認(rèn)是邊界管理器 // JPanel默認(rèn)是流式布局管理器1 centerJPanel.setLayout(new GridLayout(4, 4)); // 4.創(chuàng)建組件 leveLabel = new JLabel("等級"); jComboBox = new JComboBox<String>(new String[] { "初級", "中級", "高級" }); // 等級選擇框綁定點擊事件 jComboBox.addActionListener(this); timeLabel = new JLabel("time:"); timeVaLabel = new JLabel("10"); centerJLabel = new JLabel("cent:"); cenVaLabel = new JLabel("0"); musicLabel = new JLabel("音效"); musicBox = new JComboBox<String>(new String[] { "開", "關(guān)" }); // 音效下拉框綁定點擊事件 musicBox.addActionListener(this); jButton = new JButton("開始游戲"); pauseButton = new JButton("暫停游戲"); pauseButton.addActionListener(this); resumeButton = new JButton("繼續(xù)游戲"); resumeButton.addActionListener(this); resumeButton.setVisible(false); holeiImage = new ImageIcon("src/com/briup/game/hole.png"); mouseImgIcon = new ImageIcon("src/com/briup/game/mouse.png"); cursor = create("src/com/briup/game/hammer1.png"); cursor2 = create("src/com/briup/game/hammer2.png"); // 點擊開始按鈕,游戲開始 jButton.addActionListener(this); btsButtons = new JButton[16]; for (int i = 0; i < btsButtons.length; i++) { btsButtons[i] = new JButton(); // 禁用十六個按鈕 btsButtons[i].setEnabled(false); // 給十六個按鈕綁定點擊事件 btsButtons[i].addActionListener(this); // 給按鈕綁定鼠標(biāo)按下或松開事件 btsButtons[i].addMouseListener(new MyMouse(btsButtons[i])); // 添加到面板中 centerJPanel.add(btsButtons[i]); } // 創(chuàng)建時間定時器 timer = new Timer(1000, this); level = 1000; // 創(chuàng)建時間定時器 mouseTimer = new Timer(level, this); // 5.添加組件到面板 northJPanel.add(leveLabel); northJPanel.add(jComboBox); northJPanel.add(timeLabel); northJPanel.add(timeVaLabel); northJPanel.add(centerJLabel); northJPanel.add(cenVaLabel); northJPanel.add(musicLabel); northJPanel.add(musicBox); northJPanel.add(jButton); northJPanel.add(pauseButton); northJPanel.add(resumeButton); // 6.添加面板到容器中 jFrame.add(northJPanel, BorderLayout.NORTH); jFrame.add(centerJPanel, BorderLayout.CENTER); // 7.設(shè)置容器的寬高 jFrame.setSize(1100, 1000); // 8.設(shè)置容器窗口可變 jFrame.setResizable(false); // 9.設(shè)置容器居中 jFrame.setLocationRelativeTo(null); // 11.設(shè)置容器可見性 jFrame.setVisible(true); // 10.設(shè)置容器關(guān)閉窗口 jFrame.setDefaultCloseOperation(3); // 12.設(shè)置縮放圖片,圖片大小適配按鈕大小 mouseImgIcon.setImage(mouseImgIcon.getImage().getScaledInstance(btsButtons[0].getWidth(), btsButtons[0].getHeight(), Image.SCALE_DEFAULT)); holeiImage.setImage(holeiImage.getImage().getScaledInstance(btsButtons[0].getWidth(), btsButtons[0].getHeight(), Image.SCALE_DEFAULT)); for (int i = 0; i < btsButtons.length; i++) { btsButtons[i].setIcon(holeiImage); } try { // 13.開啟背景音樂 // 創(chuàng)建文件對象 file = new File("src/com/briup/game/打地鼠背景音樂.wav"); // 創(chuàng)建背景音樂對象 play = new MusicPlay(file.toURL()); // 開啟背景音樂 play.start(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { PlayMouse playMouse = new PlayMouse(); } // 開啟或禁用十六個按鈕 public void setButton(boolean b) { for (int i = 0; i < btsButtons.length; i++) { btsButtons[i].setEnabled(b); } } // 創(chuàng)建鼠標(biāo)的形狀 private Cursor create(String imagePath) { Image myCursor = new ImageIcon(imagePath).getImage(); // 參數(shù)一:自定義鼠標(biāo)樣式對應(yīng)的圖片對象 // 參數(shù)二:對應(yīng)的位置坐標(biāo) // 參數(shù)三:字符串,給自定義鼠標(biāo)樣式起的名字 return Toolkit.getDefaultToolkit().createCustomCursor(myCursor, new Point(0, 0), null); } // 鼠標(biāo)按下或松開的形狀 private class MyMouse extends MouseAdapter { private JButton btnButton; public MyMouse(JButton btnButton) { this.btnButton = btnButton; } // 鼠標(biāo)按下 public void mousePressed(MouseEvent e) { btnButton.setCursor(cursor2); } // 鼠標(biāo)松開 public void mouseReleased(MouseEvent e) { btnButton.setCursor(cursor); } } public void pauseGame() { timer.stop(); mouseTimer.stop(); setButton(false); pauseButton.setVisible(false); resumeButton.setVisible(true); } private void resumeGame() { timer.start(); mouseTimer.start(); setButton(true); resumeButton.setVisible(false); pauseButton.setVisible(true); isPaused = false; } @Override public void actionPerformed(ActionEvent e) { // 獲取事件源,看是誰點擊的 Object object = e.getSource(); if (object == pauseButton) { isPaused = !isPaused; if (isPaused) { pauseGame(); resumeButton.setVisible(true); } else { resumeGame(); resumeButton.setVisible(true); } } if (object == resumeButton) { isPaused = !isPaused; if (isPaused) { pauseGame(); } else { resumeGame(); } } // 點擊開始按鈕,游戲開始 if (object == jButton) { // 隨機產(chǎn)生一個老鼠 Random random = new Random(); int index = random.nextInt(btsButtons.length); flag = false; btsButtons[index].setIcon(mouseImgIcon); // 記錄上一只老鼠出現(xiàn)的位置 last = index; // 定時器開始,執(zhí)行以下else if里的內(nèi)容 timer.start(); // 開啟老鼠定時器 mouseTimer.start(); // 開啟十六個按鈕 setButton(true); // 禁用等級下拉框 jComboBox.setEditable(false); // 禁用開始按鈕 jButton.setEnabled(false); } else if (object == timer) { // 開啟時間定時器后執(zhí)行 // 獲取按鈕文本值 String -->int int num = Integer.parseInt(timeVaLabel.getText()); if (num > 0) { num--; // 重新設(shè)置時間 timeVaLabel.setText(num + ""); } else { // 游戲結(jié)束 // 清除掉最后一只老鼠,將老鼠蹄片設(shè)置為老鼠洞 btsButtons[last].setIcon(holeiImage); // 關(guān)閉時間定時器 timer.stop(); // 關(guān)閉老鼠定時器 mouseTimer.stop(); // 開啟開始按鈕 jButton.setEnabled(true); // 開啟等級按鈕 jComboBox.setEditable(true); // 禁用十六個按鈕 setButton(false); // 關(guān)閉背景音樂 play.stop(); // 重置游戲時間,分?jǐn)?shù) timeVaLabel.setText("10"); cenVaLabel.setText("0.0"); } } else if (object == mouseTimer) { // 開啟老鼠定時器執(zhí)行 // 將上一次老鼠出現(xiàn)的位置設(shè)為老鼠洞 btsButtons[last].setIcon(holeiImage); // 每隔1s隨機產(chǎn)生一只老鼠 Random random = new Random(); int index = random.nextInt(btsButtons.length); last = index; flag = false; // 將按鈕的背景圖設(shè)置為老鼠 btsButtons[index].setIcon(mouseImgIcon); } else if (object == jComboBox) { // 獲取下拉框中選擇的文本值 String string = (String) jComboBox.getSelectedItem(); // 判斷那個等級 if ("高級".equals(string)) { level = 500; } else if ("中級".equals(string)) { level = 1000; } else if ("初級".equals(string)) { level = 1400; } // 重新創(chuàng)建等級定時器 mouseTimer = new Timer(level, this); } else if (object == musicBox) { // 獲取下拉框中選擇的文本框 String chooseString = (String) musicBox.getSelectedItem(); if ("關(guān)".equals(chooseString)) { play.stop(); } else if ("開".equals(chooseString)) { play.start(); } } else { // 點擊十六個按鈕 for (int i = 0; i < btsButtons.length; i++) { // 判斷是否點擊老鼠按鈕并且該老鼠沒加過分 // 判斷是否點擊老鼠的按鈕 if (object == btsButtons[i] && btsButtons[i].getIcon() == mouseImgIcon && !flag) { // 獲取分?jǐn)?shù)的文本值 String -->int int cent = Integer.parseInt(cenVaLabel.getText()); // 分值加1 cent++; flag = true; // 設(shè)置分?jǐn)?shù)值 cenVaLabel.setText(cent + "");// 將int--》轉(zhuǎn)換成String } } } } }
開始游戲界面
StartGame.java
package com.briup.game; /** *開始游戲的界面 * */ import java.awt.GridLayout; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.net.MalformedURLException; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import sun.net.www.content.text.plain; public class StartGame implements ActionListener { // 構(gòu)建容器對象 private JFrame jFrame; // 構(gòu)建面板 private JPanel jPanel; // 構(gòu)建按鈕組件 private JButton jButton; // 構(gòu)建文件對象 private File file; // 構(gòu)建背景音樂對象 private MusicPlay play; // 構(gòu)建背景圖片 private ImageIcon startIcon; // 構(gòu)造器 初始化Jframe public StartGame() { // 1.創(chuàng)建容器 jFrame = new JFrame("開始界面"); // 2.創(chuàng)建面板 默認(rèn)流式布局管理器 jPanel = new JPanel(); // 3.面板設(shè)置布局管理器 網(wǎng)格布局管理器 jPanel.setLayout(new GridLayout(1, 1)); // 4.創(chuàng)建組件 jButton = new JButton(); jButton.addActionListener(this); // 5.添加組件到按鈕 jPanel.add(jButton); // 6.添加面板到容器 jFrame.add(jPanel); // 7.設(shè)置容器大小 jFrame.setSize(1100, 1000); // 8.設(shè)置容器居中 jFrame.setLocationRelativeTo(null); // 9.設(shè)置容器大小不可調(diào)整 jFrame.setResizable(false); // 10.設(shè)置容器可見性 jFrame.setVisible(true); // 11.設(shè)置容器關(guān)閉方式 jFrame.setDefaultCloseOperation(3); // 12.設(shè)置背景圖片 startIcon = new ImageIcon("src/com/briup/game/start.jpg"); startIcon.setImage( startIcon.getImage().getScaledInstance(jButton.getWidth(), jButton.getHeight(), Image.SCALE_DEFAULT)); jButton.setIcon(startIcon); // 13.開啟背景音樂 try { file = new File("src/com/briup/game/開始游戲背景音樂.wav"); play = new MusicPlay(file.toURL()); play.start(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { new StartGame(); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub // 關(guān)閉背景音樂 play.stop(); // 關(guān)閉當(dāng)前窗口 jFrame.dispose(); // 開啟游戲窗口 new PlayMouse(); } }
添加音樂
MusicPlay.java
package com.briup.game; /** * 背景音樂 *IO流: * 類型:字節(jié)流 字符流 * 流向:輸入流 輸出流 *異常: * Error:錯誤 不可解決 * Exception:異常 可解決 * -編譯時異常 * -運行時異常 */ import java.io.IOException; import java.net.URL; import javafx.scene.chart.PieChart.Data; import sun.audio.AudioData; import sun.audio.AudioPlayer; import sun.audio.AudioStream; import sun.audio.ContinuousAudioDataStream; public class MusicPlay { //單次播放聲音 private AudioStream aStream; //循環(huán)播放聲音 private ContinuousAudioDataStream casAudioDataStream; //構(gòu)造器 public MusicPlay(URL url) { //打開一個聲音文件流作為輸入 try { aStream = new AudioStream(url.openStream()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //一次播放開始 public void start() { if (aStream == null) { System.out.println("AudioStream object is not created"); return; }else { AudioPlayer.player.start(aStream); } } //一次播放 停止 public void stop() { if (aStream == null) { System.out.println("AudioStream object is not created"); return; }else { AudioPlayer.player.stop(aStream); } } //循環(huán)播放 開始 public void continueStart() { AudioData data = null; try { data = aStream.getData(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } casAudioDataStream = new ContinuousAudioDataStream(data); AudioPlayer.player.start(casAudioDataStream); } //循環(huán)播放 停止 public void continueStop() { if (casAudioDataStream == null) { System.out.println("ContinousAudioDataStream object is not created"); return; }else { AudioPlayer.player.stop(casAudioDataStream); } } }
測試界面
MusicPlayTest.java
package com.briup.game; import java.io.File; import java.net.MalformedURLException; public class MusicPlayTest { @SuppressWarnings("deprecation") public static void main(String[] args) throws Exception { try { // 1.創(chuàng)建文件對象 File file = new File("src/com/briup/game/打地鼠背景音樂.wav"); // 2.創(chuàng)建MusicPlay對象 MusicPlay play = new MusicPlay(file.toURL()); // 3.一次背景音樂 開啟 // play.start(); // 4.循環(huán)播放背景音樂 開啟 play.continueStart(); Thread.sleep(2000); play.continueStop(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
游戲所需圖片
1.開始界面
2.打地鼠布局
3.小錘子
未擊打時的形狀
擊打老鼠時的形狀
4.老鼠的家
5.老鼠
居中的圖片:
運行效果
總結(jié)
到此這篇關(guān)于如何使用Java語言編寫打地鼠游戲的文章就介紹到這了,更多相關(guān)Java編寫打地鼠游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Java應(yīng)用程序添加或刪除 PDF 中的附件
當(dāng)我們在制作PDF文件或者PPT演示文稿的時候,為了讓自己的文件更全面詳細(xì),就會在文件中添加附件,那么如何添加或刪除PDF中的附件呢,今天通過本文給大家詳細(xì)講解,需要的朋友參考下吧2023-01-01Java WebSocket客戶端接收大量數(shù)據(jù)的三種方案
WebSocket是一種基于TCP協(xié)議的全雙工通信協(xié)議,它能夠在客戶端和服務(wù)器之間建立一個持久連接,實現(xiàn)實時的雙向數(shù)據(jù)傳輸,在實際應(yīng)用中,有時候我們需要處理大量的數(shù)據(jù),所以本文將介紹如何使用 Java WebSocket 客戶端接收大量數(shù)據(jù),并提供一些優(yōu)化方案2023-11-11