打地鼠游戏(java)

package day12_24;
import java.awt.Color;import java.awt.Cursor;


import java.awt.Dimension;
import java.awt.Font;
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.Date;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import javax.swing.border.BevelBorder;

import sun.swing.BakedArrayList;

public class HitMouse extends JFrame implements MouseListener,ActionListener {

 /**
  * @param args
  */
 private JLabel jblMouse;
 //需要一个随机数来设置地鼠出现的位置
 private Random r=new Random(new Date().getTime());
 private Timer timer;
 private int delay=1000;
 private JLabel showTimes,hitTimes,grades;
 private int timeshow=0,timehit=0,nowgrade=1;
 private boolean isHit=false;
 
 public HitMouse(){
  setBounds(200, 200, 438, 375);
  setTitle("打地鼠");
  setDefaultCloseOperation(EXIT_ON_CLOSE);
  setResizable(false);
  //JFrame默认的布局是边布局,会影响地鼠设置的位置,此处要设空布局去屏蔽边布局;JPanel默认的布局是流布局,如果采用了JPanel,则不需要设为空布局
  //setLayout(null);
  
  //设置背景
  setBak();
  
  //设置鼠标
  mySetCursor(1);
  this.addMouseListener(this);
  //设置地鼠
  jblMouse=new JLabel(new ImageIcon("images/dishu.png"));
  jblMouse.setBounds(100,100, 80, 80);
  getContentPane().add(jblMouse);
  jblMouse.setVisible(false);
  //为老鼠添加鼠标监听
  jblMouse.addMouseListener(this);
  
  //设置定时器
  timer=new Timer(delay, this);
  timer.start();
  
  //设置出现次数,打击次数,当前等级等信息
  JPanel jPanel=new JPanel();
  jPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
  jPanel.setPreferredSize(new Dimension(438, 375));
  getContentPane().add(jPanel);
  //加了panel之后,前面就不需要屏蔽框架内容窗格的BorderLayout了。因为现在,内容窗格都被panel盖住了
  jPanel.setOpaque(false);
  
  //出现次数
  showTimes=new JLabel("1", new ImageIcon("images/chuxiancishu.png"),SwingConstants.CENTER );
  showTimes.setFont(new Font("幼圆", Font.BOLD, 20));
  showTimes.setSize(146, 40);
  jPanel.add(showTimes);
  //打中次数
  hitTimes = new JLabel("0", new ImageIcon("images/dazhongcishu.png"), SwingConstants.CENTER);
  hitTimes.setFont(new Font("幼圆", Font.BOLD, 20));
  hitTimes.setSize(146, 40);
  jPanel.add(hitTimes);
  
  //等级
  grades = new JLabel("1", new ImageIcon("images/dangqiandengji.png"), SwingConstants.CENTER);
  grades.setFont(new Font("幼圆", Font.BOLD, 20));
  grades.setSize(146, 40);
  jPanel.add(grades);

  this.setVisible(true);
 }
 
 
 //设置鼠标
 private void mySetCursor(int i) {
  //Toolkit类无法new对象,采用工厂方法获取对象
  Toolkit toolkit=Toolkit.getDefaultToolkit();
  Image image=null;
  if(i==1){
   image=toolkit.createImage("images/chui1.png");
  }else{
   image=toolkit.createImage("images/chui2.png");
  }
  Cursor cursor =toolkit.createCustomCursor(image, new Point(10, 10), "hua");
  setCursor(cursor);
 }
 
 //设置背景
 private void setBak() {
  ImageIcon bacImageIcon=new ImageIcon("images/beijing.jpg");
  //JPanel p=new JPanel();//error
  JLabel jLabel=new JLabel(bacImageIcon);
  jLabel.setBounds(0, 0, bacImageIcon.getIconWidth(), bacImageIcon.getIconHeight());
  //要用分层的方式添加背景图片,index的值越小,所在的层越靠底
  this.getLayeredPane().add(jLabel, new Integer(Integer.MIN_VALUE));
  //把内容窗格设置为透明,这样分层就能显示上层的背景图,没有此句则显示不出背景图
  ((JPanel)(this.getContentPane())).setOpaque(false);
  
 }
 public static void main(String[] args) {
  HitMouse hitMouse=new HitMouse();
 }

 //鼠标监听,当鼠标按下的时候地鼠图片改变
 @Override
 public void mouseClicked(MouseEvent e) {
 }

 @Override
 public void mousePressed(MouseEvent e) {
  mySetCursor(2);
  if(e.getSource()==jblMouse&&!isHit){//当点击地鼠且没有打头时,执行以下
   isHit=true;
   jblMouse.setIcon(new ImageIcon("images/datou.png"));
   jblMouse.setVisible(true);
   
   timehit++;
   hitTimes.setText(""+timehit);
   Toolkit.getDefaultToolkit().beep();//??????
   
   //升级
   if(timehit>8 ){
    delay-=200;
    if(delay<100){
     jblMouse.setVisible(false);
     timer.stop();
     int op=JOptionPane.showConfirmDialog(this, "您已经打通关了,要重新来吗?", "tongguan", JOptionPane.YES_NO_OPTION);
     if(op==JOptionPane.YES_OPTION){
      nowgrade=0;
      delay=1000;
     }else{
      System.exit(0);
     }
    }
    if(timer.isRunning()){
     timer.stop();
    }
    jblMouse.setVisible(false);
    mySetCursor(1);
    nowgrade++;
    JOptionPane.showConfirmDialog(this, "游戏进入第"+nowgrade+"关, 加油!!!","游戏升级",JOptionPane.CLOSED_OPTION);
    timeshow=0;
    timehit=0;
    grades.setText(""+nowgrade);
    showTimes.setText(""+timeshow);
    hitTimes.setText(""+timehit);

    timer.start();
   }
  }
 }


 @Override
 public void mouseReleased(MouseEvent e) {
  mySetCursor(1);
 }


 @Override
 public void mouseEntered(MouseEvent e) {
 }

 @Override
 public void mouseExited(MouseEvent e) {
 }

 //定时器添加事件监听,设置地鼠出现的位置
 @Override
 public void actionPerformed(ActionEvent e) {
  int pos=r.nextInt(9);//生成0-8的随机数
  switch(pos){
  case 0:
     jblMouse.setLocation(56,63);break;
     case 1:
      jblMouse.setLocation(322,204);break;
     case 2:
      jblMouse.setLocation(185,204);break;
     case 3:
      jblMouse.setLocation(48,203);break;
     case 4:
      jblMouse.setLocation(298,133);break;
     case 5:
      jblMouse.setLocation(162,133);break;
     case 6:
      jblMouse.setLocation(22,133);break;
     case 7:
      jblMouse.setLocation(311,63);break;
     case 8:
      jblMouse.setLocation(186,63);
  }
  jblMouse.setIcon(new ImageIcon("images/dishu.png"));
  jblMouse.setVisible(true);
  //定时器启动时,才设为可见
  
  //更新出现信息
  timeshow++;
  showTimes.setText(""+timeshow);
  isHit=false;
  
  //GAME OVER
  if(timeshow>15){
   int op=JOptionPane.showConfirmDialog(this, "GAME OVER!要再来一次吗?", "Game Over", JOptionPane.YES_NO_OPTION);
   if(op==JOptionPane.YES_OPTION){
    mySetCursor(1);
    delay=1000;
    nowgrade=1;
    timehit=0;
    timeshow=0;
    grades.setText(""+nowgrade);
    hitTimes.setText(""+timehit);
    showTimes.setText(""+timeshow);
    timer.start();
   }else{
    timer.stop();
   }
  }
 }

}

猜你喜欢

转载自blog.csdn.net/hua_yan_ling/article/details/62037653