大四JAVA实验(三)

好几天没有好好学习了!~~今儿个要崛起了...

实验三     警察抓小偷
 (一)、实验目的要求
    要求学生熟悉多线程编程,并能使用多线程实现程序,进一步了解和使用awt图形界面。充分的理解多线程的思想。
 (二)、实验设备
 (三)、实验步骤与内容
     定义小偷和警察类,可以用不同的图形来标示警察和小偷,初始化小偷和警察的位置。利用多线程同时调整警察和小偷的坐标,并根据坐标的变化在画面上显示小偷和警察移动路线,当他们之间的距离小于一定的值时,程序终止。
     利用多线程模拟警察抓小偷的游戏。使用AWT图形界面模仿警察抓小偷的过程。在图形界面上画出小偷逃跑的路线。警察按小偷逃跑的方向调整追击路线。

代码如下:
  1 /** */ /**
  2*title 警察抓小偷(多线程小测试)
  3*@author realsmy
  4*date 2006-11-1 16:56
  5*/

  6
  7 import  java.awt. * ;
  8 import  javax.swing. * ;
  9 import  java.util. * ;
 10
 11 // 创建警察类
 12 class  Police  implements  Runnable {
 13    private int px,py;
 14    public static int zhua = 0;
 15    Test t;
 16    Police(Test t){
 17        this.t = t;
 18        px = 30;
 19        py = 30;
 20    }

 21    public void run(){
 22        while(true){
 23            //synchronized(t){
 24                if(zhua == 0){
 25                    px++;
 26                    py++;
 27                }

 28                else{
 29                    if(px < t.thief.getTx()){
 30                        px++;
 31                    }

 32                    if(py < t.thief.getTy()){
 33                        py++;
 34                    }

 35                }

 36                try{
 37                    Thread.sleep(50);
 38                }
catch(InterruptedException e){}
 39            //}
 40            t.repaint();
 41        }

 42    }

 43    public int getPx(){
 44        return px;
 45    }

 46    public int getPy(){
 47        return py;
 48    }

 49}

 50
 51 // 创建小偷类
 52 class  Thief  implements  Runnable {
 53    private int tx,ty;
 54    public static int tao = 0;
 55    Test t;
 56    Thief(Test t){
 57        this.t = t;
 58        tx = 300;
 59        ty = 300;
 60    }

 61    public void run(){
 62        while(true){
 63            //synchronized(t){
 64                if(tao == 0){
 65                    tx--;
 66                    ty--;
 67                }

 68                else{
 69                    tx++;
 70                }

 71                try{
 72                    Thread.sleep(100);
 73                }
catch(InterruptedException e){}
 74            //}
 75            t.repaint();
 76        }

 77    }

 78    public int getTx(){
 79        return tx;
 80    }

 81    public int getTy(){
 82        return ty;
 83    }

 84}

 85
 86 // 测试类
 87 public   class  Test  extends  JFrame {
 88    private Container c;
 89    public Police police;
 90    public Thief thief;
 91    Thread p,t;
 92    int a =0;
 93    private Vector vt;
 94    Test(){
 95        super("老鹰抓小鸡");
 96        c = getContentPane();
 97        
 98        vt = new Vector();  //用来存储小偷逃跑坐标
 99        police  = new Police(this);
100        thief = new Thief(this);
101        p = new Thread(police);
102        t = new  Thread(thief);
103        p.start();
104        t.start();
105
106        setSize(400,400);
107        setLocation(200,200);
108        setVisible(true);
109    }

110
111    public void paint(Graphics g){
112        g.setColor(new Color(255255255));
113        g.fillRect(00400400);
114        draw_police();
115        draw_thief();
116        draw_guiji();
117        if(Math.hypot(police.getPx()-thief.getTx(), police.getPy()-thief.getTy()) < 80){
118            police.zhua = 1;
119            thief.tao = 1;
120        }

121        if(police.getPx() == thief.getTx() && police.getPy() == thief.getTy()){
122            p.stop();
123            t.stop();
124            try{
125                JOptionPane.showMessageDialog(null,"警察:小样,被我逮到了吧!~哈哈哈哈~");
126            }
catch(HeadlessException e){}
127            System.ex

你可能感兴趣的:(Java,多线程,游戏,thread,Swing)