2012年6月19日星期二

Game


import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.Random;
import java.util.Timer;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Catch_Me extends JFrame implements Runnable
{
    Random R=new Random();
    Random RR=new Random();
ImageIcon bird_img = new ImageIcon("03.gif");//小鼠图片
ImageIcon mouse_img = new ImageIcon("07.gif");//老猫图片
ImageIcon milipic = new ImageIcon("star1.gif");//星星图片
ImageIcon sun= new ImageIcon("sunshine_0.png");//太阳图片
ImageIcon dog=new ImageIcon("dog.gif");
ImageIcon bread=new ImageIcon("bread.gif");
ImageIcon bread2=new ImageIcon("bread2.gif");
ImageIcon bread3=new ImageIcon("bread3.gif");
ImageIcon bread4=new ImageIcon("bread4.gif");
ImageIcon bread5=new ImageIcon("bread5.gif");



//图片装载到标签中
JLabel jb_bird = new JLabel(bird_img);
JLabel jb2_mouse = new JLabel(mouse_img);
JLabel mili = new JLabel(milipic);
JLabel sunl = new JLabel(sun);
JLabel dogl=new JLabel(dog);
JLabel breadl=new JLabel(bread);
JLabel breadl2=new JLabel(bread2);
JLabel breadl3=new JLabel(bread3);
JLabel breadl4=new JLabel(bread4);
JLabel breadl5=new JLabel(bread5);
//JButton button=new JButton("再次挑战");
//JLabel buttonlabel=new JLabel(button);
//设置线程是否运行
static boolean isContinue=true;
static boolean noContinue=true;
static int i=0;
static int j=0;
//初始化对象

public Catch_Me() {
setBak(); // 调用背景方法
this.setSize(980, 670);// 设置窗口大小
this.setTitle("小鼠快跑");
//this.setLayout(null);
this.setResizable(false);

this.setLocationRelativeTo(null);
//设置游戏层

JPanel jp = new JPanel();
jp.setOpaque(false);
this.add(breadl);
        this.add(breadl2);
        this.add(breadl3);
        this.add(breadl4);
        this.add(breadl5);
        //this.getContentPane().setLayout(null);


//jp.add(breadl);

jp.add(sunl);
        jp.add(jb_bird);
        jp.add(jb2_mouse);
        jp.add(mili);
        jp.add(dogl);
        //button.setBounds(440,330,75,45);
        //jp.add(button);
this.getContentPane().add(jp);
breadl.setBounds(R.nextInt(960), R.nextInt(640),205,154);
breadl2.setBounds(R.nextInt(960), R.nextInt(640),205,154);
breadl3.setBounds(R.nextInt(960), R.nextInt(640),205,154);
breadl4.setBounds(R.nextInt(960), R.nextInt(640),205,154);
breadl5.setBounds(R.nextInt(960), R.nextInt(640),205,154);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//增加鼠标动作
this.addMouseMotionListener(new MouseMotionListener() {
//增加鼠标移动执行的方法
public void mouseMoved(MouseEvent arg0) {
if (arg0.getY() <=670&& arg0.getX() <=980) {
jb_bird.setLocation(arg0.getX() - 50, arg0.getY() - 80);
}
if((jb_bird.getLocation().x>=mili.getLocation().x-15&&jb_bird.getLocation().x<=mili.getLocation().x+15)&&(jb_bird.getLocation().y>=mili.getLocation().y-15&&jb_bird.getLocation().y<=mili.getLocation().y+15)){
Random rnd = new Random();
Random rnd2=new Random();
mili.setLocation(rnd.nextInt(300)+1, rnd2.nextInt(300)+1);
//jb2_mouse.setLocation(rnd.nextInt(544), rnd2.nextInt(420));
}
}

//吃面包
public void mouseDragged(MouseEvent arg0)
{
                if(Math.abs(arg0.getX()-breadl.getX())<=2||Math.abs(arg0.getY()-breadl.getY())<=2)
                {
                    breadl.setVisible(false);
                    j++;
                   
                }
                if(Math.abs(arg0.getX()-breadl2.getX())<=2||Math.abs(arg0.getY()-breadl2.getY())<=2)
                {
                    breadl2.setVisible(false);
                    j++;
                }
                if(Math.abs(arg0.getX()-breadl3.getX())<=2||Math.abs(arg0.getY()-breadl3.getY())<=2)
                {
                    breadl3.setVisible(false);
                    j++;
                }
                if(Math.abs(arg0.getX()-breadl4.getX())<=2||Math.abs(arg0.getY()-breadl4.getY())<=2)
                {
                    breadl4.setVisible(false);
                    j++;
                }
                if(Math.abs(arg0.getX()-breadl5.getX())<=2||Math.abs(arg0.getY()-breadl5.getY())<=2)
                {
                    breadl5.setVisible(false);
                    j++;
                }
               
}
//鼠标拖拉组件执行的方法

});
this.setVisible(true);
}

//设置背靠图片
public void setBak() {
((JPanel) this.getContentPane()).setOpaque(false);//设置透明度的
ImageIcon img = new ImageIcon("body_bg.jpg"); // 添加图片
JLabel background = new JLabel(img);
//JButton button=new JButton("再次挑战");
//background.add(button);
this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
}
//线程开始
public void start() {
   new timetest_noeasy(this);
        Timer timer=new Timer();
        timer.schedule(new timetest_noeasy(this), 15000);
while (noContinue) {
if(isContinue)
{
 
Thread thr = new Thread(this);
try {
 
thr.sleep(400);
} catch (InterruptedException e) {
e.printStackTrace();
}
thr.start();

}
}
}
//执行线程
public void run()
{
   this.add(breadl3);
   this.add(breadl4);
   breadl3.setBounds(R.nextInt(960), R.nextInt(640),205,154);
   breadl4.setBounds(R.nextInt(960), R.nextInt(640),205,154);
 
int x = jb_bird.getLocation().x;
int y = jb_bird.getLocation().y;

if(x<490||y>335)
jb2_mouse.setLocation(x, y);
if(y<335||y>490)
dogl.setLocation(x, y);
try {
           Thread.sleep(300);
       } catch (InterruptedException e) {
           e.printStackTrace();
       }

if ((jb_bird.getLocation().x == jb2_mouse.getLocation().x
&& jb_bird.getLocation().y == jb2_mouse.getLocation().y)
||(jb_bird.getLocation().x ==dogl.getLocation().x
               && jb_bird.getLocation().y ==dogl.getLocation().y))
{
isContinue=true;
//JOptionPane.showMessageDialog(null, "小鼠被抓住啦!");
final JFrame frame=new JFrame();
       frame.setTitle("提示信息");
     
       JPanel pane=new JPanel();
       JLabel label=new JLabel("小鼠被抓住啦!");
     
       pane.add(label);
            //pane.add(button);
            frame.add(pane);
            frame.setVisible(true);
            frame.setBounds(500, 300,250, 90);
            frame.setLocationRelativeTo(null);
            try
            {
                Thread.sleep(500);
            }
            catch(InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
         
            frame.setVisible(false);
     
     
     

i++;

}
}
}








import java.util.TimerTask;


public class timetest_noeasy extends TimerTask
{
    Catch_Me me;
    public timetest_noeasy(Catch_Me catch_Me_Difficult)
    {
        this.me=catch_Me_Difficult;
    }
  public void run()
    {
        me.noContinue=false;
       
    }
}













import javax.swing.JOptionPane;


public class Catch_Me_Main
{
  //main方法
    public static void main(String[] args){
        int j=0;
        Catch_Me c = new Catch_Me();
        c.start();
        j=c.i;
        //JFrame frame=new JFrame(); 
       // frame.setTitle("提示信息");
        
       /* JPanel pane=new JPanel();
        JLabel text=new JLabel("游戏时间到--- ---"+"小鼠被猫捉了"+c.i+"次");
        JLabel text1=new JLabel("再接再厉,下次见");
        JLabel text2=new JLabel("小鼠吃了"+c.j+"个面包");
        pane.add(text);
        pane.add(text2);
        pane.add(text1);*/ 
        JOptionPane.showMessageDialog(null, "游戏时间到,小鼠被猫捉了"+c.i+"次,吃了"+c.j+"个面包","消息",JOptionPane.WARNING_MESSAGE);
       // frame.add(pane);
        //frame.setVisible(true);
        //frame.setBounds(500, 300,250, 90);
        //frame.setLocationRelativeTo(null);
        
    }
}


















没有评论:

发表评论