码迷,mamicode.com
首页 > 移动开发 > 详细

一个基于Myeclipse开发的Java打地鼠小游戏(Appletcation)

时间:2015-07-19 19:28:29      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

  1 package javaes.zixue.wangshang.daima;
  2
3
import java.awt.Cursor; 4 import java.awt.Image; 5 import java.awt.Point; 6 import java.awt.Toolkit; 7 import java.awt.event.ActionEvent; 8 import java.awt.event.ActionListener; 9 import java.awt.event.MouseEvent; 10 import java.awt.event.MouseListener; 11 import java.util.Random; 12 13 import javax.swing.ImageIcon; 14 import javax.swing.JFrame; 15 import javax.swing.JLabel; 16 import javax.swing.JMenu; 17 import javax.swing.JMenuBar; 18 import javax.swing.JMenuItem; 19 import javax.swing.JOptionPane; 20 import javax.swing.JPanel; 21 import javax.swing.Timer; 22 23 24 public class HitMouse extends JFrame implements ActionListener,MouseListener{ 25 26 private static final long serialVersionUID = 1L; 27 boolean isOver=false;//设置标记,游戏是否结束 28 private String dir="./images/";//图片目录,当前工程下 29 JLabel jlbMouse;//地鼠 30 Timer timer;//时间定时器 31 Random random;//随机数对象,即生成地鼠的位置 32 int delay=1000;//延迟时间 33 Toolkit tk; 34 Image image; 35 Cursor myCursor; 36 JLabel showNum,currentGrade,hitNum; 37 int showNumber=0,hitNumber=0,currentGrades=1; 38 39 public HitMouse(){ 40 super("打地鼠"); 41 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 42 this.setSize(500, 300); 43 this.setLocationRelativeTo(null);//设置窗口在屏幕中心 44 setbackground();//设置背景 45 this.getContentPane().setLayout(null);//设置框架布局模式为空,只有这样,才能知道图片的真正位置 46 //设置鼠标为锤子图片 47 tk = Toolkit.getDefaultToolkit(); 48 image = tk.createImage(dir+"chui1.png"); 49 myCursor = tk.createCustomCursor(image, new Point(30,30), "xxx"); 50 this.setCursor(myCursor); 51 52 setMessage();//设置一些提示信息 53 //在背景图片的基础上设置地鼠图片 54 ImageIcon imageMouse = new ImageIcon(dir+"datou.png"); 55 jlbMouse = new JLabel(imageMouse); 56 jlbMouse.setSize(100,100); 57 this.getContentPane().add(jlbMouse); 58 jlbMouse.setVisible(false); 59 jlbMouse.addMouseListener(this);//添加鼠标监听 60 //定时器 61 timer = new Timer(delay,this); 62 random = new Random(); 63 timer.start(); 64 65 addMenu();//添加菜单 66 67 this.setResizable(false);//设置窗口大小不能改变 68 this.setVisible(true); 69 } 70 71 private void addMenu() { 72 JMenuBar menubar = new JMenuBar(); 73 this.setJMenuBar(menubar); 74 JMenu game = new JMenu("游戏"); 75 JMenuItem jitemNew = new JMenuItem("新游戏"); 76 jitemNew.setActionCommand("new"); 77 jitemNew.addActionListener(this); 78 JMenuItem jitemPause = new JMenuItem("暂停"); 79 jitemPause.setActionCommand("pause"); 80 jitemPause.addActionListener(this); 81 JMenuItem jitemExit = new JMenuItem("退出"); 82 jitemExit.setActionCommand("exit"); 83 jitemExit.addActionListener(this); 84 /* JMenuItem jitemEasy= new JMenuItem("简单"); 85 jitemExit.setActionCommand("easy"); 86 jitemExit.addActionListener(this); 87 JMenuItem jitemYiban= new JMenuItem("中等"); 88 jitemExit.setActionCommand("yiban"); 89 jitemExit.addActionListener(this); 90 JMenuItem jitemKunnan= new JMenuItem("困难"); 91 jitemExit.setActionCommand("kunnan"); 92 jitemExit.addActionListener(this)*/ 93 game.add(jitemNew); 94 game.add(jitemPause); 95 game.addSeparator();//菜单里设置分隔线 96 game.add(jitemExit); 97 game.addSeparator();//菜单里设置分隔线 98 /* game.add(jitemEasy); 99 game.add(jitemYiban); 100 game.add(jitemKunnan); */ 101 menubar.add(game); 102 } 103 104 private void setbackground() { 105 ((JPanel)(this.getContentPane())).setOpaque(false);//如果为 true,则该组件绘制其边界内的所有像素。否则该组件可能不绘制部分或所有像素,从而允许其底层像素透视出来。 106 ImageIcon bgImage = new ImageIcon("images/beijing.jpg"); 107 JLabel bgLabel = new JLabel(bgImage); 108 bgLabel.setBounds(0, 25, bgImage.getIconWidth(), bgImage.getIconHeight()); 109 this.getLayeredPane().add(bgLabel, new Integer(Integer.MIN_VALUE));//设置背景图片的层次最低 110 111 } 112 113 private void setMessage() { 114 ImageIcon showNumb = new ImageIcon(dir+"chuxiancishu.png"); 115 JLabel showLabel = new JLabel(showNumb); 116 showLabel.setBounds(8, 8, 92, 80); 117 this.getContentPane().add(showLabel); 118 showNum = new JLabel("0"); 119 showNum.setBounds(8, 8, 92, 80); 120 this.getContentPane().add(showNum); 121 122 ImageIcon hitNumb = new ImageIcon(dir+"chuxiancishu.png"); 123 JLabel hitLabel = new JLabel(hitNumb); 124 hitLabel.setBounds(148, 8, 92, 80); 125 this.getContentPane().add(hitLabel); 126 hitNum = new JLabel("0"); 127 hitNum.setBounds(148, 8, 92, 80); 128 this.getContentPane().add(hitNum); 129 130 ImageIcon grade = new ImageIcon(dir+"dangqiandengji.png"); 131 JLabel gradeLabel = new JLabel(grade); 132 gradeLabel.setBounds(288, 8, 92, 80); 133 this.getContentPane().add(gradeLabel); 134 currentGrade = new JLabel("1"); 135 currentGrade.setBounds(288, 8, 92, 80); 136 this.getContentPane().add(currentGrade); 137 } 138 139 140 public static void main(String[] args) { 141 new HitMouse(); 142 } 143 144 public void actionPerformed(ActionEvent e) { 145 //对菜单项注册事件监听 146 if(e.getSource() instanceof JMenuItem){ 147 menuItemFun(e); 148 } 149 150 int ran=random.nextInt(9);//随机生成一个0~9(不包括9)的随机数 151 ImageIcon imageMouse = new ImageIcon(dir+"dishu.png");//保证每次随机生成的地鼠图片都是为没被打时的图片 152 jlbMouse.setIcon(imageMouse); 153 switch(ran){ 154 case 0:jlbMouse.setLocation(70, 40);break; 155 case 1:jlbMouse.setLocation(185, 35);break; 156 case 2:jlbMouse.setLocation(315,40);break; 157 case 3:jlbMouse.setLocation(55,95);break; 158 case 4:jlbMouse.setLocation(190,95);break; 159 case 5:jlbMouse.setLocation(315,90);break; 160 case 6:jlbMouse.setLocation(50, 155);break; 161 case 7:jlbMouse.setLocation(190, 160);break; 162 case 8:jlbMouse.setLocation(320, 160);break; 163 } 164 165 jlbMouse.setVisible(true); 166 167 showNumber++; 168 showNum.setText(""+showNumber); 169 170 if( !gamePlan() ){//判断游戏是否结束,并显示游戏进程 171 timer.stop(); 172 } 173 174 } 175 //监听菜单功能功能 176 private void menuItemFun(ActionEvent e) { 177 if (e.getActionCommand().equalsIgnoreCase("new")) {//新游戏 178 timer.stop(); 179 showNumber=0; 180 hitNumber=0; 181 currentGrades=1; 182 delay=1000; 183 isOver=false; 184 showNum.setText(""+showNumber); 185 hitNum.setText(""+hitNumber); 186 currentGrade.setText(""+currentGrades); 187 timer = new Timer(delay,this); 188 timer.start(); 189 } 190 if (e.getActionCommand().equalsIgnoreCase("exit")) {//退出 191 isOver=true; 192 System.exit(EXIT_ON_CLOSE); 193 } 194 195 if (e.getActionCommand().equalsIgnoreCase("pause")) {//暂停 196 timer.stop(); 197 JOptionPane.showMessageDialog(this, "继续请按“确定”"); 198 timer.start(); 199 } 200 201 /* if (e.getActionCommand().equalsIgnoreCase("easy")) { 202 timer.stop(); 203 showNumber=0; 204 hitNumber=0; 205 currentGrades=1; 206 delay=2500; 207 isOver=false; 208 showNum.setText(""+showNumber); 209 hitNum.setText(""+hitNumber); 210 currentGrade.setText(""+currentGrades); 211 timer = new Timer(delay,this); 212 timer.start(); 213 } 214 215 216 if (e.getActionCommand().equalsIgnoreCase("yiban")) { 217 timer.stop(); 218 showNumber=0; 219 hitNumber=0; 220 currentGrades=1; 221 delay=1000; 222 isOver=false; 223 showNum.setText(""+showNumber); 224 hitNum.setText(""+hitNumber); 225 currentGrade.setText(""+currentGrades); 226 timer = new Timer(delay,this); 227 timer.start(); 228 } 229 230 if (e.getActionCommand().equalsIgnoreCase("kunnan")) { 231 timer.stop(); 232 showNumber=0; 233 hitNumber=0; 234 currentGrades=1; 235 delay=100; 236 isOver=false; 237 showNum.setText(""+showNumber); 238 hitNum.setText(""+hitNumber); 239 currentGrade.setText(""+currentGrades); 240 timer = new Timer(delay,this); 241 timer.start(); 242 243 } */ 244 245 } 246 247 private boolean gamePlan() { 248 if(showNumber-hitNumber > 8){ 249 JOptionPane.showMessageDialog(this, "Game Over !"); 250 isOver=true; 251 return false; 252 } 253 if(hitNumber > 5){ 254 hitNumber=0; 255 showNumber=0; 256 currentGrades++; 257 if(delay>100){ 258 delay-=50; 259 }else if(delay>=500){ 260 delay=500; 261 } 262 timer.setDelay(delay); 263 hitNum.setText(""+hitNumber); 264 showNum.setText(""+showNumber); 265 currentGrade.setText(""+currentGrades); 266 } 267 return true; 268 } 269 270 public void mouseClicked(MouseEvent e) { 271 272 } 273 274 public void mousePressed(MouseEvent e) { 275 if(isOver){ 276 return ; 277 } 278 image = tk.createImage(dir+"chui2.png"); 279 myCursor = tk.createCustomCursor(image, new Point(10,10), "xxx"); 280 this.setCursor(myCursor);//鼠标按下时,鼠标显示打下去的图片,模拟打的动作 281 //如果打中地鼠,则地鼠换成被打中的图片,模拟地鼠被打 282 if(e.getSource()==jlbMouse){ 283 ImageIcon imageIconHit = new ImageIcon(dir+"datou.png"); 284 jlbMouse.setIcon(imageIconHit); 285 jlbMouse.setVisible(true); 286 } 287 288 hitNumber++; 289 hitNum.setText(""+hitNumber); 290 } 291 292 public void mouseReleased(MouseEvent e) { 293 if(isOver){ 294 return ; 295 } 296 //当鼠标放松以后,鼠标变回原来没按下时的图片 297 image = tk.createImage(dir+"chui1.png"); 298 myCursor = tk.createCustomCursor(image, new Point(10,10), "xxx"); 299 this.setCursor(myCursor); 300 } 301 302 public void mouseEntered(MouseEvent e) { 303 304 } 305 306 public void mouseExited(MouseEvent e) { 307 308 } 309 310 311 }

技术分享技术分享技术分享技术分享技术分享技术分享技术分享

一个基于Myeclipse开发的Java打地鼠小游戏(Appletcation)

标签:

原文地址:http://www.cnblogs.com/servlet/p/4658940.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!