Java實現的打 地鼠遊戲
import java.awt.Cursor; 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.MouseEvent; import java.awt.event.MouseListener; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.Timer; 公共類HitMouse擴展JFrame實現ActionListener,MouseListener { boolean isOver = false; //設置標記,遊戲是否結束 private String dir =「./ images /」; //圖片目錄,當前工程下 JLabel jlbMouse; //地鼠 定時器定時器; //時間定時器 隨機隨機; //隨機數對象,即生成地鼠的位置 int delay = 1100; //延遲時間 工具包tk 圖像; 游標myCursor; JLabel showNum,currentGrade,hitNum; int showNumber = 0,hitNumber = 0,currentGrades = 1; public HitMouse(){ 超級(「打地鼠」); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(449395); this.setLocationRelativeTo(NULL); //設置窗口在屏幕中心 的的setBackground(); //設置背景 this.getContentPane()的setLayout的(NULL); //設置框架布局模式為空,只有這樣,才能知道圖片的真正位置 //設置滑鼠為鎚子圖片 tk = Toolkit.getDefaultToolkit(); image = tk.createImage(dir +「chui1.png」); myCursor = tk.createCustomCursor(image,new Point(10,10),「xxx」); this.setCursor(myCursor); setMessage(); //設置一些提示信息 //在背景圖片的基礎上設置地鼠圖片 ImageIcon imageMouse = new ImageIcon(dir +「dishu.png」); jlbMouse = new JLabel(imageMouse); jlbMouse.setSize(80,80); this.getContentPane()添加(jlbMouse)。 jlbMouse.setVisible(假); jlbMouse.addMouseListener(本); //添加滑鼠監聽 // 定時器timer = new Timer(delay,this); random = new Random(); timer.start(); addMenu(); //添加菜單 this.setResizable(假); //設置窗口大小不能改變 this.setVisible(真); } private void addMenu(){ JMenuBar menubar = new JMenuBar(); this.setJMenuBar(菜單欄); JMenu game = new JMenu(「遊戲」); JMenuItem jitemNew = new JMenuItem(「新遊戲」); jitemNew.setActionCommand(「新」); jitemNew.addActionListener(本); JMenuItem jitemPause = new JMenuItem(「暫停」); jitemPause.setActionCommand(「暫停」); jitemPause.addActionListener(本); JMenuItem jitemExit = new JMenuItem(「退出」); jitemExit.setActionCommand(「退出」); jitemExit.addActionListener(本); game.add(jitemNew); game.add(jitemPause); game.addSeparator(); //菜單里設置分隔線 game.add(jitemExit); menubar.add(遊戲); } private void setbackground(){ ((JPanel中)(this.getContentPane()))setOpaque(假); //如果為真,則該組件繪製其邊界內的所有像素。否則該組件可能不繪製部分或所有像素,從而允許其底層像素透視出來 .ImageIcon bgImage = new ImageIcon(「images / beijing.jpg」); JLabel bgLabel = new JLabel(bgImage); bgLabel.setBounds(0,25,bgImage.getIconWidth(),bgImage.getIconHeight()); this.getLayeredPane()。add(bgLabel,new Integer(Integer.MIN_VALUE)); //設置背景圖片的層次最低 } private void setMessage(){ ImageIcon showNumb = new ImageIcon(dir +「chuxiancishu.png」); JLabel showLabel = new JLabel(showNumb); showLabel.setBounds(8,8,92,80); this.getContentPane()添加(showLabel)。 showNum = new JLabel(「0」); showNum.setBounds(110,8,92,80); this.getContentPane()添加(showNum)。 ImageIcon hitNumb =新的ImageIcon(dir +「chuxiancishu.png」); JLabel hitLabel = new JLabel(hitNumb); hitLabel.setBounds(148,8,92,80); this.getContentPane()添加(hitLabel)。 hitNum = new JLabel(「0」); hitNum.setBounds(251,8,92,80); this.getContentPane()添加(hitNum)。 ImageIcon等級=新ImageIcon(dir +「dangqiandengji.png」); JLabel gradeLabel = new JLabel(grade); gradeLabel.setBounds(288,8,92,80); this.getContentPane()添加(gradeLabel)。 currentGrade = new JLabel(「1」); currentGrade.setBounds(391,8,92,80); this.getContentPane()添加(currentGrade)。 } public static void main(String [] args){ 新的HitMouse(); } public void actionPerformed(ActionEvent e){ //對菜單項註冊事件監聽 if(e.getSource()instanceof JMenuItem){ menuItemFun(E); } int run = random.nextInt(9); //隨機生成一個0不9(不包括9)的隨機數 ImageIcon imageMouse = new ImageIcon(dir +「dishu.png」); //保證每次隨機生成的地鼠圖片都是為沒被打時的圖片 jlbMouse.setIcon(imageMouse); 開關(RAN){ case 0:jlbMouse.setLocation(55,63); 打破; 情況1:jlbMouse.setLocation(321204); 打破; 情況2:jlbMouse.setLocation(184204); 打破; 情況3:jlbMouse.setLocation(47203); 打破; 情況4:jlbMouse.setLocation(297133); 打破; 情況5:jlbMouse.setLocation(161133); 打破; 情況6:jlbMouse.setLocation(21,133); 打破; 情況7:jlbMouse.setLocation(310,63); 打破; 情況8:jlbMouse.setLocation(185,63); 打破; } jlbMouse.setVisible(真); showNumber ++; showNum.setText(「」+ showNumber); 如果(!Gameplan的()){//判斷遊戲是否結束,並顯示遊戲進程 timer.stop(); } } //監聽菜單功能功能 private void menuItemFun(ActionEvent e){ 如果(e.getActionCommand()。equalsIgnoreCase(「新」)){//新遊戲 timer.stop(); showNumber = 0; hitNumber = 0; currentGrades = 1; 延遲= 1000; ISOVER = FALSE; showNum.setText(「」+ showNumber); hitNum.setText(「」+ hitNumber); currentGrade.setText(「」+ currentGrades); timer = new Timer(delay,this); timer.start(); } 如果(e.getActionCommand()。equalsIgnoreCase(「退出」)){//退出 System.exit(EXIT_ON_CLOSE); } 如果(e.getActionCommand()。equalsIgnoreCase(「暫停」)){//暫停 timer.stop(); JOptionPane.showMessageDialog(這一點,「繼續請按」確定「」); timer.start(); } } private boolean gamePlan(){ if(showNumber-hitNumber> 8){ JOptionPane.showMessageDialog(this,「Game Over!」); ISOVER = TRUE; 返回假 } if(hitNumber> 5){ hitNumber = 0; showNumber = 0; currentGrades ++; if(delay> 100){ delay- = 50; } else if(delay> = 500){ 延遲= 500; } timer.setDelay(延遲); hitNum.setText(「」+ hitNumber); showNum.setText(「」+ showNumber); currentGrade.setText(「」+ currentGrades); } 返回真 } public void mouseClick(MouseEvent e){ } public void mousePressed(MouseEvent e){ 如果(ISOVER){ 返回; } image = tk.createImage(dir +「chui2.png」); myCursor = tk.createCustomCursor(image,new Point(10,10),「xxx」); this.setCursor(myCursor); //滑鼠按下時,滑鼠顯示打下去的圖片,模擬打的動作 //如果打中地鼠,則地鼠換成被打中的圖片,模擬地鼠被打 如果(e.getSource()== jlbMouse){ ImageIcon imageIconHit = new ImageIcon(dir +「datou.png」); jlbMouse.setIcon(imageIconHit); jlbMouse.setVisible(真); } hitNumber ++; hitNum.setText(「」+ hitNumber); } public void mouseReleased(MouseEvent e){ 如果(ISOVER){ 返回; } //當滑鼠放鬆以後,滑鼠變回原來沒下按時的圖片 image = tk.createImage(dir +「chui1.png」); myCursor = tk.createCustomCursor(image,new Point(10,10),「xxx」); this.setCursor(myCursor); } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e){ } }


※全方位解讀Java反射 reflection
※學了Java開發能給大家多少崗位從事
※學習分享 Java爬蟲偽代碼
※Java——面向對象基礎
※如何成為一名合格的 Java 程序員
TAG:java吧 |
※iFTY&4AM都翻了車 絕地求生變成打地鼠和海賊王?QGC新春邀請賽
※用emoji表情賺錢了?賽馬套圈打地鼠……劇情好豐富!撩妹撩漢新技能……
※貓小樂:阿衰和同學受困「放屁挖地道逃生」,庄酷被當打地鼠!
※美國的加密市場監管——一個「打地鼠遊戲」
※喵星人鑽下水道出不來的囧樣:打地鼠秒變打地貓遊戲,好慘啊!
※打什麼地鼠!來玩打地貓啊!真貓版打地貓!鏟屎官,勞資這麼萌!你下得了手嗎!
※毛孔大的能打地鼠怎麼辦?
※真實版貓咪地鼠,見過嗎
※搞笑GIF:你要不要也來玩一下,這比打地鼠有趣多了!
※街機遊戲:讓人無語的獎勵關!砸汽車打地鼠吃包子都是什麼梗?
※打地鼠遊戲?美媒:特朗普現在就該結束三場戰爭
※繼承「瘋狂地鼠城」的遊戲?這遊戲比地鼠城還喪心病狂
※做出這台打地鼠機,你就是貓咪眼中最好的鏟屎官
※炸彈成了鑽地鼠?落到地上不爆炸反而鑽進去,它是如何實現的?
※貓咪玩了三個小時打地鼠遊戲後,場面讓人亮瞎!網友:這姿勢一看就是社會貓
※貓咪玩打地鼠遊戲,玩三個小時後,場面讓人亮瞎了!
※小老鼠挖窩自住,一隻一個洞,版圖就像打地鼠一樣!
※主人跟金毛玩打地鼠,沒幾下它就...
※給貓弄了個打地鼠玩具,它玩兩下就當場倒下了:捉不著,打擊貓了
※柴犬碰到了地鼠,整隻狗都懵逼了!