功能:随机产生一个地鼠,可以使用锤子敲击,并记录地鼠出现的次数,击中的次数,得到的分数
代码:
1.进入游戏的界面:功能——实现了页面跳转
/** * */ package com.niit.hitmouse; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.Image; import java.awt.Label; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.Box; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; /** * @author: Annie * @date:2016年6月2日 * @description: */ public class GameEntrance extends JFrame implements ActionListener{ GridLayout gl; JButton btn_begin,btn_help; Box box_begin,box_help,baseBox; BackGroundPane jp ; public static void main(String[] args) { new GameEntrance(); } public GameEntrance() { setTitle("欢迎进入铭霞制作的地鼠游戏"); setLocation(400,400); setSize(400, 400); init(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } private void init() { Image img = new ImageIcon("./src/picture/dsbg.jpg").getImage(); jp = new BackGroundPane(img ); btn_begin = new JButton("开始游戏"); btn_help = new JButton("游戏帮助"); box_begin = Box.createHorizontalBox(); box_help = Box.createHorizontalBox(); baseBox = Box.createVerticalBox(); addFunction(); setFunction(); listenrFunction(); } private void addFunction() { box_begin.add(btn_begin); box_help.add(btn_help); baseBox.add(Box.createVerticalStrut(100)); baseBox.add(box_begin); baseBox.add(Box.createVerticalStrut(20)); baseBox.add(box_help); jp.add(baseBox); add(jp); } private void setFunction() { } private void listenrFunction() { btn_begin.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { new HitMouseGame(); } }
2.打击地鼠的类 :实现了鼠标监听,活动监听,使用Timer类进行计时
/** * */ package com.niit.hitmouse; import java.awt.Color; 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.JTextField; import javax.swing.Timer; /** * @author: Annie * @date:2016年6月2日 * @description: */ public class HitMouseGame extends JFrame implements ActionListener,MouseListener{ BackGroundPane jp; JLabel mouse,lab_showTimer,lab_hitTimer,lab_gradesTimer; JTextField jt_showTimer,jt_hitTimer,jt_gradesTimer; Timer timer; //计时器,隔一段时间去触发某一个事件 Random random; Toolkit kit;//工具类 Cursor myCursor; int showTimes,hitTimes,grades;//出现的次数,打中的次数,分数 int delay = 1000;//关卡 ImageIcon bgImage,mouseImage; Image hammerImageOne,hammerImageTow; public HitMouseGame() { setSize(450,400); setTitle("打地鼠"); getContentPane().setLayout(null); init(); getContentPane().add(jp, new Integer(Integer.MIN_VALUE));//背景放在最底层 setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void init() { bgImage = new ImageIcon("./src/picture/bg.jpg"); mouseImage = new ImageIcon("./src/picture/dishu.png"); jp = new BackGroundPane(bgImage.getImage()); mouse = new JLabel(mouseImage); lab_showTimer = new JLabel("出现的次数:"); jt_showTimer = new JTextField(10); lab_hitTimer = new JLabel("打中的次数:"); jt_hitTimer = new JTextField(10); lab_gradesTimer = new JLabel("得到分数:"); jt_gradesTimer= new JTextField(10); random = new Random(); timer = new Timer(1000, this); timer.start(); //初始化鼠标的外观:锤子 kit = Toolkit.getDefaultToolkit(); hammerImageOne = kit.createImage("./src/picture/chui1.png"); myCursor = kit.createCustomCursor(hammerImageOne, new Point(30, 30), "chuizi"); setCursor(myCursor); setBoundsFunction(); addFunction(); addlistenerFunction(); } /**为控件设置值得方法*/ public void setBoundsFunction(){ jp.setBounds(0, 0, bgImage.getIconWidth(), bgImage.getIconHeight()); mouse.setSize(80,80); mouse.setVisible(false); lab_showTimer.setBounds(10, 10, 80, 80); jt_showTimer.setBounds(90, 40, 30, 20); jt_showTimer.setBackground(new Color(122,156,202)); lab_hitTimer.setBounds(130, 10, 80, 80); jt_hitTimer.setBounds(220, 40, 30, 20); jt_hitTimer.setBackground(new Color(122,156,202)); lab_gradesTimer.setBounds(270, 10, 80, 80); jt_gradesTimer.setBounds(340, 40, 30, 20); jt_gradesTimer.setBackground(new Color(122,156,202)); } /**将控件添加到面板上的方法*/ public void addFunction(){ getContentPane().add(mouse); getContentPane().add(lab_showTimer); getContentPane().add(jt_showTimer); getContentPane().add(lab_hitTimer); getContentPane().add(jt_hitTimer); getContentPane().add(lab_gradesTimer); getContentPane().add(jt_gradesTimer); getContentPane().add(jp, new Integer(Integer.MIN_VALUE));//背景放在最底层 } /**给控件设置监听的方法*/ public void addlistenerFunction(){ mouse.addMouseListener(this); } /***********事件处理方法******************/ @Override public void actionPerformed(ActionEvent e) { //这个方法会定时执行,每秒执行一次 //随机生成地鼠坐标 int number = random.nextInt(9); //确保每次出现的老鼠没被打过 mouseImage = new ImageIcon("./src/picture/dishu.png"); mouse.setIcon(mouseImage); switch (number) { case 0: mouse.setLocation(55,63); break; case 1: mouse.setLocation(321,204); break; case 2: mouse.setLocation(184,204); break; case 3: mouse.setLocation(47,203); break; case 4: mouse.setLocation(279,133); break; case 5: mouse.setLocation(161,133); break; case 6: mouse.setLocation(21,133); break; case 7: mouse.setLocation(310,63); break; case 8: mouse.setLocation(185,63); break; } mouse.setVisible(true); showTimes++; jt_showTimer.setText(showTimes+""); } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { //打下去时,修改鼠标的外观 hammerImageTow =kit.createImage("./src/picture/chui2.png"); myCursor = kit.createCustomCursor(hammerImageTow, new Point(30, 30), "随便写"); setCursor(myCursor); //修改老鼠的外观,先判断是否打到老鼠 if(e.getSource().equals(mouse)){ ImageIcon image = new ImageIcon("./src/picture/datou.png"); mouse.setIcon(image); hitTimes++; String jtNum = hitTimes+""; jt_hitTimer.setText(jtNum); grades+=5;//一个给5分 jt_gradesTimer.setText(grades+""); } if(grades>0 && grades%50 == 0){ delay -= 100; timer.setDelay(delay); } if(showTimes - hitTimes>=10){//如果漏打了十个就结束游戏 timer.stop(); } } @Override public void mouseReleased(MouseEvent e) { hammerImageOne =kit.createImage("./src/picture/chui1.png"); myCursor = kit.createCustomCursor(hammerImageOne, new Point(30, 30), "随便写"); setCursor(myCursor); } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } }
3.自定义一个可以设置背景图片的类:
/** * */ package com.niit.hitmouse; import java.awt.Graphics; import java.awt.Image; import javax.swing.JPanel; /** * @author: Annie * @date:2016年6月2日 * @description: 自定义的背景图片类 */ public class BackGroundPane extends JPanel{ Image img; public BackGroundPane(Image img) { this.img = img; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(),this); } }
实现效果图:
第一个界面效果图:
第二个界面效果图: