编程语言
首页 > 编程语言> > Java---打地鼠

Java---打地鼠

作者:互联网

今天打卡时写了一个打地鼠游戏:

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 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;

public class HitMouse extends JFrame implements MouseListener, ActionListener{
    private final String DIR="D:\\IDEA\\DitMouseSuCai";//存储该程序所需背景图片和素材
    private JLabel lbMouse;
    private int delay;
    private Timer timer;
    private JLabel timesShowLabel,timesHitLabel,gradeLabel;
    private int timesShow,timesHit,grade;
    private boolean isHit=false;	//点击地鼠只能一次

    public HitMouse() {
        super("打地鼠");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setBounds(200,200,438,375);
        setResizable(false);
        setBak();	//设置背景图片
        setMySetCursor(1);	//设置鼠标  1表示初始状态
        //添加地鼠
        ImageIcon imgMouse=new ImageIcon(DIR+"/dishu.png");
        lbMouse=new JLabel(imgMouse);
        lbMouse.setLocation(100, 100);
        lbMouse.setSize(80, 80);
        getContentPane().add(lbMouse);
        lbMouse.setVisible(false);
        lbMouse.addMouseListener(this);

        //添加定时器,改变地鼠位置
        delay=1000;
        timer=new Timer(delay, this);
        timer.start();
        JPanel panel =new JPanel();
        //边框效果
        panel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
        panel.setPreferredSize(new Dimension(438, 375));
        getContentPane().add(panel);
        panel.setOpaque(false);		//在将面板设置为透明
        //出现次数
        timesShowLabel=new JLabel("0", new ImageIcon(DIR+"/chuxiancishu.png"), SwingConstants.CENTER);
        timesShowLabel.setFont(new Font("幼圆", Font.BOLD, 20));
        timesShowLabel.setSize(146, 40);
        panel.add(timesShowLabel);

        //打中次数
        timesHitLabel = new JLabel("0", new ImageIcon(DIR+"/dazhongcishu.png"), SwingConstants.CENTER);
        timesHitLabel.setFont(new Font("幼圆", Font.BOLD, 20));
        timesHitLabel.setSize(146, 40);
        panel.add(timesHitLabel);

        //等级
        gradeLabel = new JLabel("1", new ImageIcon(DIR+"/dangqiandengji.png"), SwingConstants.CENTER);
        gradeLabel.setFont(new Font("幼圆", Font.BOLD, 20));
        gradeLabel.setSize(146, 40);
        panel.add(gradeLabel);
        setVisible(true);
    }

    private void setBak() {
        ((JPanel)getContentPane()).setOpaque(false);		//设置面板为透明

        JLabel bgLabel=new JLabel(new ImageIcon(DIR+"/beijing.jpg"));
        bgLabel.setBounds(0, 0, bgLabel.getIcon().getIconWidth(), bgLabel.getIcon().getIconHeight());
        //分层技术
        getLayeredPane().add(bgLabel, new Integer(Integer.MIN_VALUE));
    }

    private void setMySetCursor(int i) {
        //设置鼠标 1表示初始状态 2表示锤下状态
        Toolkit toolkit=Toolkit.getDefaultToolkit();
        Image image;
        if(i==1){
            image=toolkit.createImage(DIR+"/chui1.png");
        }else{
            image=toolkit.createImage(DIR+"/chui2.png");
        }
        Cursor cursor=toolkit.createCustomCursor(image, new Point(10, 10), "aa");
        setCursor(cursor);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int pos=(int) (System.currentTimeMillis()%9);
        System.out.println(pos);
        switch(pos){
            case 0:
                lbMouse.setLocation(56,63);break;
            case 1:
                lbMouse.setLocation(322,204);break;
            case 2:
                lbMouse.setLocation(185,204);break;
            case 3:
                lbMouse.setLocation(48,203);break;
            case 4:
                lbMouse.setLocation(298,133);break;
            case 5:
                lbMouse.setLocation(162,133);break;
            case 6:
                lbMouse.setLocation(22,133);break;
            case 7:
                lbMouse.setLocation(311,63);break;
            case 8:
                lbMouse.setLocation(186,63);
        }

        //每次都要刷新地鼠的图片为未打中
        ImageIcon imageMouse=new ImageIcon(DIR+"/dishu.png");
        lbMouse.setIcon(imageMouse);
        isHit=false;
        lbMouse.setVisible(true);

        //改变出现次数
        timesShow++;
        timesShowLabel.setText(""+timesShow);

        //出现的时候判断游戏是否失败
        if(timesShow>15){
            timer.stop();
            int a=JOptionPane.showConfirmDialog(this, "游戏失败,是否再来一局");
            if(a==JOptionPane.YES_OPTION){
                timesShow=0;
                timesHit=0;
                grade=0;
                delay=1000;
                setMySetCursor(1);
                timesShowLabel.setText(""+timesShow);
                timesHitLabel.setText(""+timesHit);
                gradeLabel.setText(""+grade);
                timer.restart();		//定时器重新启动
            }else{
                System.exit(0);
            }
        }

    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
        setMySetCursor(2);

        if(e.getSource()==lbMouse && !isHit){
            isHit=true;

            timesHit++;
            timesHitLabel.setText(""+timesHit);

            getToolkit().beep();
            ImageIcon imageDaTou=new ImageIcon(DIR+"/datou.png");
            lbMouse.setIcon(imageDaTou);
            lbMouse.setVisible(true);

            //当击中次数大于8次时游戏升级
            if(timesHit>8){
                delay=delay-50;
                if(delay==50){
                    lbMouse.setVisible(false);
                    timer.stop();
                    int a=JOptionPane.showConfirmDialog(this, "你已经通过所有关卡,是否重新开始一局新的游戏");
                    if(a==JOptionPane.YES_OPTION){
                        grade=0;
                        delay=1000;
                    }
                }
                timer.stop();
                grade++;
                timesShow=0;
                timesHit=0;
                setMySetCursor(1);
                JOptionPane.showConfirmDialog(this, "进入"+grade+"关,加油",
                        "游戏升级!!",JOptionPane.CLOSED_OPTION);
                timesShowLabel.setText(""+timesShow);
                timesHitLabel.setText(""+timesHit);
                gradeLabel.setText(""+grade);
                timer.setDelay(delay);
                timer.start();
            }
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }


    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    public static void main(String[] args) {
        new HitMouse();
    }
}

  注:其中添加图片的部分自己选择图片下载并命名,放在自己选好的目录下,将目录地址复制下来修改即可,图片名一定要和后面代码中出现的名称一样,否则识别不了。

标签:ImageIcon,Java,java,lbMouse,---,地鼠,setLocation,new,import
来源: https://www.cnblogs.com/Yjiaw/p/15779212.html