java游戏源码(打地鼠游戏实现)

java游戏源码(打地鼠游戏实现)_第1张图片

图片文件夹:


java游戏源码(打地鼠游戏实现)_第2张图片

eclipse里的显示截图

java游戏源码(打地鼠游戏实现)_第3张图片



源代码:

package com.iotek.mouse;

import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.sound.sampled.LineUnavailableException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import org.omg.CORBA.PUBLIC_MEMBER;

public class PlayMouse extends JFrame implements Runnable {
    private int num=0;
	private JLabel back;
	private JLabel [] mouses;
	private ImageIcon imgMouse;
	private JLabel jtf;
	public PlayMouse(){
		this.setResizable(false);//不能够修改大小
		this.getContentPane().setLayout(null);
		this.setTitle("打地鼠");
		this.setBounds(300,100,800,480);
	    back=new JLabel();
	    ImageIcon icon=new ImageIcon(this.getClass().getResource("5.gif"));
	    back.setIcon(icon);
	    back.setBounds(0,-35,800,480);
	    this.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(Toolkit.getDefaultToolkit().getImage("src/com/iotek/mouse/chui.png"), new Point(),"self"));
	  
	    imgMouse=new ImageIcon(this.getClass().getResource("4.png"));
	    mouses=new JLabel[9];
	    for(int i=0;i<9;i++){
	    	mouses[i]=new JLabel();
	    	mouses[i].setSize(imgMouse.getIconWidth(),imgMouse.getIconHeight());
	    	//mouses[i].setIcon(imgMouse);
	    	mouses[i].addMouseListener(new MouseListener() {
				
				@Override
				public void mouseReleased(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void mousePressed(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void mouseExited(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void mouseEntered(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void mouseClicked(MouseEvent e) {
					Object object=e.getSource();
					if(object instanceof JLabel){
						JLabel label=(JLabel)object;
						if(label.getIcon()!=null){
							num++;
							jtf.setText("您的得分是"+num+" 分");//加分功能
						}
						label.setIcon(null);
					}
					
				}
			});
	    	
	    	this.getContentPane().add(mouses[i]);
	    }
	    
	    jtf=new JLabel();
	    jtf.setBounds(360, 10,230,50);
	    jtf.setFont(new Font("", 20, 25));
	    jtf.setForeground(Color.RED);
	    jtf.setText("您的得分是:    分");
	    
	    
	    mouses[0].setLocation(88,53);
	    mouses[1].setLocation(240,53);
	    mouses[2].setLocation(390,53);
	    mouses[3].setLocation(55,160);
	    mouses[4].setLocation(245,160);
	    mouses[5].setLocation(430,160);
	    mouses[6].setLocation(36,296);
	    mouses[7].setLocation(230,296);
	    mouses[8].setLocation(445,296);
	    this.getContentPane().add(jtf);
	    this.getContentPane().add(back);
   
		this.setVisible(true);
	}
	
	
   public void run(){
	   while(true){
		  try {
			Thread.sleep(500);
			int index=(int)(Math.random()*9);
//			double abc=Math.random();
//			/******/
//			System.out.println(abc);
			/******/
			if(mouses[index].getIcon()==null){
				mouses[index].setIcon(imgMouse);
				Thread.sleep(900);//老鼠出现的速度,根据反应时间
				if(mouses[index].isShowing()){
					mouses[index].setIcon(null);
				}
				
			}
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	   }
   }
   public static void main(String [] args){
	   PlayMouse p1=new PlayMouse();
	   Thread t1=new Thread(p1);
	   t1.start();
   }

}
由于涉及的东西比较多,本人有的功能也不太了解,遇到不懂得函数调用,百度即可得到完美解答,这是头一次亲自了解游戏的设计思路及其代码实现,也是头一次在java程序中实现了图片的插入和代码逻辑的结合,,也深刻的了解到了java基础对于以后实战开发的重要性,越来越有趣了

你可能感兴趣的:(java)