/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package tank; import java.util.Vector; class Tank { int x = 0, y = 0; int fangxiang; int sudu = 9; boolean shengming = true; public int getSudu() { return sudu; } public void setSudu(int sudu) { this.sudu = sudu; } public int getFangxiang() { return fangxiang; } public void setFangxiang(int fangxiang) { this.fangxiang = fangxiang; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public Tank(int x, int y) { this.x = x; this.y = y; } } class DiTank extends Tank implements Runnable { int sudu = 2; Vector dzd = new Vector<>(); Vector dtk = new Vector<>(); public DiTank(int x, int y) { super(x, y); } public boolean huxiangpengzhuang() { boolean b = false; switch (this.fangxiang) { case 0: for (int i = 0; i < dtk.size(); i++) { DiTank dt = dtk.get(i); if (dt != this) { if (dt.fangxiang == 0 || dt.fangxiang == 2) { if (this.x > dt.x && this.x < dt.x + 20 && this.y >= dt.y && this.y <= dt.y + 30) { return true; } if (this.x + 20 >= dt.x && this.x + 20 <= dt.x + 20 && this.y >= dt.y && this.y <= dt.y + 30) { return true; } } if (dt.fangxiang == 3 || dt.fangxiang == 1) { if (this.x >= dt.x && this.x <= dt.x + 30 && this.y >= dt.y && this.y <= dt.y + 20) { return true; } if (this.x + 20 >= dt.x && this.x + 20 <= dt.x + 30 && this.y >= dt.y && this.y <= dt.y + 20) { return true; } } } } case 1: for (int i = 0; i < dtk.size(); i++) { DiTank dt = dtk.get(i); if (dt != this) { if (dt.fangxiang == 0 || dt.fangxiang == 2) { if (this.x > dt.x && this.x < dt.x + 20 && this.y >= dt.y && this.y <= dt.y + 30) { return true; } if (this.x >= dt.x && this.x <= dt.x + 20 && this.y + 20 >= dt.y && this.y + 20 <= dt.y + 30) { return true; } } if (dt.fangxiang == 3 || dt.fangxiang == 1) { if (this.x >= dt.x && this.x <= dt.x + 30 && this.y >= dt.y && this.y <= dt.y + 20) { return true; } if (this.x >= dt.x && this.x <= dt.x + 30 && this.y + 20 >= dt.y && this.y + 20 <= dt.y + 20) { return true; } } } } case 2: for (int i = 0; i < dtk.size(); i++) { DiTank dt = dtk.get(i); if (dt != this) { if (dt.fangxiang == 0 || dt.fangxiang == 2) { if (this.x > dt.x && this.x < dt.x + 20 && this.y + 30 >= dt.y && this.y + 30 <= dt.y + 30) { return true; } if (this.x + 20 >= dt.x && this.x + 20 <= dt.x + 20 && this.y + 30 >= dt.y && this.y <= dt.y + 30) { return true; } } if (dt.fangxiang == 3 || dt.fangxiang == 1) { if (this.x >= dt.x && this.x <= dt.x + 30 && this.y + 30 >= dt.y && this.y <= dt.y + 20) { return true; } if (this.x + 20 >= dt.x && this.x + 20 <= dt.x + 30 && this.y + 30 >= dt.y && this.y <= dt.y + 20) { return true; } } } } case 3: for (int i = 0; i < dtk.size(); i++) { DiTank dt = dtk.get(i); if (dt != this) { if (dt.fangxiang == 0 || dt.fangxiang == 2) { if (this.x + 30 > dt.x && this.x + 30 < dt.x + 20 && this.y >= dt.y && this.y <= dt.y + 30) { return true; } if (this.x + 30 >= dt.x && this.x + 30 <= dt.x + 20 && this.y + 20 >= dt.y && this.y + 20 <= dt.y + 30) { return true; } } if (dt.fangxiang == 3 || dt.fangxiang == 1) { if (this.x + 30 >= dt.x && this.x + 30 <= dt.x + 30 && this.y >= dt.y && this.y <= dt.y + 20) { return true; } if (this.x + 30 >= dt.x && this.x + 30 <= dt.x + 30 && this.y + 20 >= dt.y && this.y + 20 <= dt.y + 20) { return true; } } } } break ; } return false; } @Override public void run() { while (true) { switch (this.fangxiang) { case 0: for (int i = 0; i < 30; i++) { if (y > 0 && !huxiangpengzhuang()) { y -= sudu; } try { Thread.sleep(50); } catch (Exception e) { } } break; case 1: { for (int i = 0; i < 30; i++) { if (x > 0&& !huxiangpengzhuang()) { x -= sudu; } try { Thread.sleep(50); } catch (Exception e) { } } break; } case 2: { for (int i = 0; i < 30; i++) { if (y < 250 && !huxiangpengzhuang()) { y += sudu; } try { Thread.sleep(50); } catch (Exception e) { } } break; } case 3: { for (int i = 0; i < 30; i++) { if (x < 360 && !huxiangpengzhuang() ) { x += sudu; } try { Thread.sleep(50); } catch (Exception e) { } } break; } } this.fangxiang = (int) (Math.random() * 4); if (this.shengming == false) { break; } if (shengming) { if (dzd.size() < 100) { zidan zd = null; switch (fangxiang) { case 0: zd = new zidan(x + 10, y, 0); dzd.add(zd); break; case 1: zd = new zidan(x, y + 10, 1); dzd.add(zd); break; case 2: zd = new zidan(x + 10, y + 30, 2); dzd.add(zd); break; case 3: zd = new zidan(x + 30, y + 10, 3); dzd.add(zd); break; } Thread t4 = new Thread(zd); t4.start(); } } } } } class MyTank extends Tank { Vector aa = new Vector(); zidan zd = null; public MyTank(int x, int y) { super(x, y); } public void xiangshang() { y -= sudu; } public void xiangzuo() { x -= sudu; } public void xiangxia() { y += sudu; } public void xiangyou() { x += sudu; } public void fszd() { switch (this.fangxiang) { case 0: zd = new zidan(x + 10, y, 0); aa.add(zd); break; case 1: zd = new zidan(x, y + 10, 1); aa.add(zd); break; case 2: zd = new zidan(x + 10, y + 30, 2); aa.add(zd); break; case 3: zd = new zidan(x + 30, y + 10, 3); aa.add(zd); break; } Thread t = new Thread(zd); t.start(); } } class zidan implements Runnable { int x, y; int fangxiang; int sudu = 10; boolean shengming = true; public zidan(int x, int y, int fangxiang) { this.x = x; this.y = y; this.fangxiang = fangxiang; } @Override public void run() { while (true) { try { Thread.sleep(100); // System.out.println("!!!"); } catch (InterruptedException e) { System.out.println("ERROR:" + e); } switch (this.fangxiang) { case 0: y -= sudu; break; case 1: x -= sudu; break; case 2: y += sudu; break; case 3: x += sudu; break; } if (x < 0 || x > 500 || y < 0 || y > 400) { this.shengming = false; break; } } } } class BaoZha { int x, y; int shengcunqi = 9; boolean shengming = true; public BaoZha(int x, int y) { this.x = x; this.y = y; } public void suqsd() { if (shengcunqi > 0) { shengcunqi--; } else { this.shengming = false; } } }