Java游戏练习—打地鼠

  • Post author:
  • Post category:java


先看一下样子吧

这里写图片描述

然后是代码

package cn.hncu.games;

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
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;

/*
 * 一、显示图片(图像)
 * 1. JLabel: 构造方法 JLabel(Icon image) 
 * 2. Icon接口: 有一个实现类ImageIcon,它的构造方法是ImageIcon(String filename) 

       二、设置鼠标
 * 3. Window类中的一个方法: public void setCursor(Cursor cursor)
 * 
 * 4.java.awt.Toolkit抽象类中的方法
	 1)创建自定义光标: public Cursor createCustomCursor(Image cursor, Point hotSpot, String name)
     2)创建Image对象: public abstract Image createImage(String filename)
     3)创建一个Toolkit对象: public static Toolkit getDefaultToolkit()
   5. Image是表示图形图像的所有类的超类
   
       三、分层显示
   1. 通过JFrame中的方法" JLayeredPane getLayeredPane() ",获得一个分层面板
   2. 在分层面板中添加对象即分层显示: add(Component comp, int index)---把组件comp加在分层面板的index位置,如果index值越小放在越低层
   3. 分层显示时需把内容窗格设成透明(用的是JComponent中的方法):public void setOpaque(false)
 
 */


public class HitMouse extends JFrame implements ActionListener, MouseListener{
	private final String DIR = ""E:\\dir\\fangchan\\src\\images\\";//需要修改成你自己放图片的路径,比如我的是
	private JLabel jlbMouse;
	private Timer timer;
	private int delay=1000;
	private Random r = new Random( new Date().getTime() );
	
	private JLabel timesShowLabel,timesHitLabel,gradeLabel;
	private int timesShow=0,timesHit=0,grade=1;
	
	private boolean isHit=false;
	
	public HitMouse() {
		setTitle("打地鼠");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLocation(200, 200);
		setSize(438, 375);
		setResizable(false);
		//demo();
		setBak();//设置背景
		//setLayout(null);//屏蔽掉框架内容窗格的BorderLayout
		
		//设置光标 N   NJNJ
		mySetCursor(1);
		this.addMouseListener(this);
		
		//设置地鼠
		jlbMouse = new JLabel( new ImageIcon(DIR+"dishu.png"));
		jlbMouse.setLocation(100, 100);
		jlbMouse.setSize(80, 80);
		getContentPane().add(jlbMouse);
		jlbMouse.setVisible(false);
		jlbMouse.addMouseListener(this);
		
		//定时器
		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之后,前面就不需要屏蔽框架内容窗格的BorderLayout了。因为现在,内容窗格都被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 mySetCursor(int i) {
		Toolkit tk = Toolkit.getDefaultToolkit();
		Image img = null;
		if(i==1){
			img = tk.createImage(DIR+"chui1.png");
		}else{
			img = tk.createImage(DIR+"chui2.png");
		}
		Cursor cursor = tk.createCustomCursor(img, new Point(10,10), "xyz");
		setCursor(cursor);
	}

	private void setBak() {
		ImageIcon bgImg = new ImageIcon(DIR+"beijing.jpg");
		JLabel bgLabel = new JLabel(bgImg);
		bgLabel.setBounds(0, 0, bgImg.getIconWidth(), bgImg.getIconHeight());
		//this.getContentPane().add(bgLabel);//要以分层的方式添加
		this.getLayeredPane().add(bgLabel, new Integer(Integer.MIN_VALUE) );
		
		((JPanel)(this.getContentPane())).setOpaque(false);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		int pos = r.nextInt(9);//随机位置:0~8
		switch(pos){
			case 0: 
			  jlbMouse.setLocation(56,63);break;
		   case 1: 
			   jlbMouse.setLocation(322,204);break;
		   case 2: 
			   jlbMouse.setLocation(185,204);break;
		   case 3: 
			   jlbMouse.setLocation(48,203);break;
		   case 4: 
			   jlbMouse.setLocation(298,133);break;
		   case 5: 
			   jlbMouse.setLocation(162,133);break;
		   case 6: 
			   jlbMouse.setLocation(22,133);break;
		   case 7: 
			   jlbMouse.setLocation(311,63);break;
		   case 8: 
			   jlbMouse.setLocation(186,63);
		}
		
		jlbMouse.setIcon(new ImageIcon(DIR+"dishu.png"));//每次刚出来时应该是没有打中的状态显示
		jlbMouse.setVisible(true);
		
		//更新出现次数信息
		timesShow++;
		timesShowLabel.setText(timesShow+"");
		isHit=false;
		
		//Game Over的实现
		if(timesShow>15){
			
			int op = JOptionPane.showConfirmDialog(this, "太菜,挂了,还要再来一盘吗?!!!","Game Over",JOptionPane.YES_NO_OPTION);
			if(op==JOptionPane.YES_OPTION){
				mySetCursor(1);
				delay=1000;
				grade=1;
				timesShow=0;
				timesHit=0;
				timesShowLabel.setText(""+timesShow);
				timesHitLabel.setText(""+timesHit);
				gradeLabel.setText(""+grade);
				timer.start();
			}else{
				timer.stop();
			}			
		}	
		
	}
	
	public static void main(String[] args) {
		new HitMouse();
	}
	
	
	
	
	@Override
	public void mouseClicked(MouseEvent e) {
	}

	@Override
	public void mousePressed(MouseEvent e) {
		mySetCursor(2);
		if(e.getSource()==jlbMouse && !isHit ){
			isHit=true;
			jlbMouse.setIcon( new ImageIcon(DIR+"datou.png"));
			jlbMouse.setVisible(true);	
			
			timesHit++;
			timesHitLabel.setText(""+timesHit);
			Toolkit.getDefaultToolkit().beep();
			
			//升级
			if(timesHit>8){
				delay -=190;//
				if(delay<100){
					jlbMouse.setVisible(false);
					timer.stop();
					int op = JOptionPane.showConfirmDialog(this, "神啊,您已经打通关了,要重新来吗?","宇宙猛男",JOptionPane.YES_NO_OPTION);
					if(op==JOptionPane.YES_OPTION){
						grade=0;
						delay=1000;
					}else{
						System.exit(0);
					}
				}
				
				if(timer.isRunning()){
					timer.stop();
				}
				jlbMouse.setVisible(false);
				mySetCursor(1);//锤子复位
				grade++;
				JOptionPane.showConfirmDialog(this, "游戏进入第"+grade+"关, 加油!!!","游戏升级",JOptionPane.CLOSED_OPTION);
				timesShow=0;
				timesHit=0;
				timesShowLabel.setText(""+timesShow);
				timesHitLabel.setText(""+timesHit);
				gradeLabel.setText(""+grade);
				
				timer.start();
			}
			
		}
	}

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

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}
	
	
	/以下属开发前的技术讲解演示//
	private void demo() {
		//显示图片
		JLabel lb = new JLabel( new ImageIcon("images/a.gif"));
		getContentPane().add(lb);
		
		//设置光标
		Toolkit t = Toolkit.getDefaultToolkit();
		Image img = t.createImage("images/chui1.png");
		Cursor cursor = t.createCustomCursor(img, new Point(5,5), "a");
		setCursor(cursor);
		
		//JPanel设透明显示
		JPanel p = new JPanel();
		p.setOpaque(false);
		
		JPanel p2 = new JPanel(){
			@Override
			public void print(Graphics g) {
				//g.drawImage(img, x, y, observer);
			}
		};
		
		
	}
	
}

这里是需要的图片(图片需要正确的命名,不然找不到)正确命名

在这里插入图片描述

beijing.jpg

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述



版权声明:本文为weixin_37720904原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。