[Java] 坦克大战2 →→→→→进入此内容的聊天室

来自 , 2021-03-05, 写在 Java, 查看 142 次.
URL http://www.code666.cn/view/3a15c7d0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package tank;
  7.  
  8. import java.util.Vector;
  9.  
  10. class Tank {
  11.  
  12.     int x = 0, y = 0;
  13.     int fangxiang;
  14.     int sudu = 9;
  15.     boolean shengming = true;
  16.  
  17.     public int getSudu() {
  18.         return sudu;
  19.     }
  20.  
  21.     public void setSudu(int sudu) {
  22.         this.sudu = sudu;
  23.     }
  24.  
  25.     public int getFangxiang() {
  26.         return fangxiang;
  27.     }
  28.  
  29.     public void setFangxiang(int fangxiang) {
  30.         this.fangxiang = fangxiang;
  31.     }
  32.  
  33.     public int getX() {
  34.         return x;
  35.     }
  36.  
  37.     public void setX(int x) {
  38.         this.x = x;
  39.     }
  40.  
  41.     public int getY() {
  42.         return y;
  43.     }
  44.  
  45.     public void setY(int y) {
  46.         this.y = y;
  47.     }
  48.  
  49.     public Tank(int x, int y) {
  50.         this.x = x;
  51.         this.y = y;
  52.     }
  53. }
  54.  
  55. class DiTank extends Tank implements Runnable {
  56.  
  57.     int sudu = 2;
  58.  
  59.     Vector<zidan> dzd = new Vector<>();
  60.     Vector<DiTank> dtk = new Vector<>();
  61.  
  62.     public DiTank(int x, int y) {
  63.         super(x, y);
  64.     }
  65.  
  66.     public boolean huxiangpengzhuang() {
  67.         boolean b = false;
  68.         switch (this.fangxiang) {
  69.             case 0:
  70.                 for (int i = 0; i < dtk.size(); i++) {
  71.                     DiTank dt = dtk.get(i);
  72.                     if (dt != this) {
  73.                         if (dt.fangxiang == 0 || dt.fangxiang == 2) {
  74.                             if (this.x > dt.x && this.x < dt.x + 20 && this.y >= dt.y && this.y <= dt.y + 30) {
  75.                                 return true;
  76.                             }
  77.                             if (this.x + 20 >= dt.x && this.x + 20 <= dt.x + 20 && this.y >= dt.y && this.y <= dt.y + 30) {
  78.                                 return true;
  79.                             }
  80.                         }
  81.                         if (dt.fangxiang == 3 || dt.fangxiang == 1) {
  82.                             if (this.x >= dt.x && this.x <= dt.x + 30 && this.y >= dt.y && this.y <= dt.y + 20) {
  83.                                 return true;
  84.                             }
  85.                             if (this.x + 20 >= dt.x && this.x + 20 <= dt.x + 30 && this.y >= dt.y && this.y <= dt.y + 20) {
  86.                                 return true;
  87.                             }
  88.                         }
  89.                     }
  90.                 }
  91.             case 1:
  92.                 for (int i = 0; i < dtk.size(); i++) {
  93.                     DiTank dt = dtk.get(i);
  94.                     if (dt != this) {
  95.                         if (dt.fangxiang == 0 || dt.fangxiang == 2) {
  96.                             if (this.x > dt.x && this.x < dt.x + 20 && this.y >= dt.y && this.y <= dt.y + 30) {
  97.                                 return true;
  98.                             }
  99.                             if (this.x >= dt.x && this.x <= dt.x + 20 && this.y + 20 >= dt.y && this.y + 20 <= dt.y + 30) {
  100.                                 return true;
  101.                             }
  102.                         }
  103.                         if (dt.fangxiang == 3 || dt.fangxiang == 1) {
  104.                             if (this.x >= dt.x && this.x <= dt.x + 30 && this.y >= dt.y && this.y <= dt.y + 20) {
  105.                                 return true;
  106.                             }
  107.                             if (this.x >= dt.x && this.x <= dt.x + 30 && this.y + 20 >= dt.y && this.y + 20 <= dt.y + 20) {
  108.                                 return true;
  109.                             }
  110.                         }
  111.                     }
  112.                 }
  113.             case 2:
  114.                 for (int i = 0; i < dtk.size(); i++) {
  115.                     DiTank dt = dtk.get(i);
  116.                     if (dt != this) {
  117.                         if (dt.fangxiang == 0 || dt.fangxiang == 2) {
  118.                             if (this.x > dt.x && this.x < dt.x + 20 && this.y + 30 >= dt.y && this.y + 30 <= dt.y + 30) {
  119.                                 return true;
  120.                             }
  121.                             if (this.x + 20 >= dt.x && this.x + 20 <= dt.x + 20 && this.y + 30 >= dt.y && this.y <= dt.y + 30) {
  122.                                 return true;
  123.                             }
  124.                         }
  125.                         if (dt.fangxiang == 3 || dt.fangxiang == 1) {
  126.                             if (this.x >= dt.x && this.x <= dt.x + 30 && this.y + 30 >= dt.y && this.y <= dt.y + 20) {
  127.                                 return true;
  128.                             }
  129.                             if (this.x + 20 >= dt.x && this.x + 20 <= dt.x + 30 && this.y + 30 >= dt.y && this.y <= dt.y + 20) {
  130.                                 return true;
  131.                             }
  132.                         }
  133.                     }
  134.                 }
  135.             case 3:
  136.                 for (int i = 0; i < dtk.size(); i++) {
  137.                     DiTank dt = dtk.get(i);
  138.                     if (dt != this) {
  139.                         if (dt.fangxiang == 0 || dt.fangxiang == 2) {
  140.                             if (this.x + 30 > dt.x && this.x + 30 < dt.x + 20 && this.y >= dt.y && this.y <= dt.y + 30) {
  141.                                 return true;
  142.                             }
  143.                             if (this.x + 30 >= dt.x && this.x + 30 <= dt.x + 20 && this.y + 20 >= dt.y && this.y + 20 <= dt.y + 30) {
  144.                                 return true;
  145.                             }
  146.                         }
  147.                         if (dt.fangxiang == 3 || dt.fangxiang == 1) {
  148.                             if (this.x + 30 >= dt.x && this.x + 30 <= dt.x + 30 && this.y >= dt.y && this.y <= dt.y + 20) {
  149.                                 return true;
  150.                             }
  151.                             if (this.x + 30 >= dt.x && this.x + 30 <= dt.x + 30 && this.y + 20 >= dt.y && this.y + 20 <= dt.y + 20) {
  152.                                 return true;
  153.                             }
  154.                         }
  155.                     }
  156.                 }
  157.             break ;
  158.         }
  159.         return false;
  160.     }
  161.  
  162.     @Override
  163.     public void run() {
  164.         while (true) {
  165.             switch (this.fangxiang) {
  166.                 case 0:
  167.                     for (int i = 0; i < 30; i++) {
  168.                         if (y > 0 && !huxiangpengzhuang()) {
  169.                             y -= sudu;
  170.                         }
  171.                         try {
  172.                             Thread.sleep(50);
  173.                         } catch (Exception e) {
  174.  
  175.                         }
  176.                     }
  177.                     break;
  178.                 case 1: {
  179.                     for (int i = 0; i < 30; i++) {
  180.                         if (x > 0&& !huxiangpengzhuang()) {
  181.                             x -= sudu;
  182.                         }
  183.                         try {
  184.                             Thread.sleep(50);
  185.                         } catch (Exception e) {
  186.  
  187.                         }
  188.                     }
  189.                     break;
  190.  
  191.                 }
  192.                 case 2: {
  193.                     for (int i = 0; i < 30; i++) {
  194.                         if (y < 250 && !huxiangpengzhuang()) {
  195.                             y += sudu;
  196.                         }
  197.                         try {
  198.                             Thread.sleep(50);
  199.                         } catch (Exception e) {
  200.  
  201.                         }
  202.                     }
  203.                     break;
  204.                 }
  205.                 case 3: {
  206.                     for (int i = 0; i < 30; i++) {
  207.                         if (x < 360 && !huxiangpengzhuang() ) {
  208.                             x += sudu;
  209.                         }
  210.                         try {
  211.                             Thread.sleep(50);
  212.                         } catch (Exception e) {
  213.  
  214.                         }
  215.                     }
  216.                     break;
  217.  
  218.                 }
  219.             }
  220.             this.fangxiang = (int) (Math.random() * 4);
  221.  
  222.             if (this.shengming == false) {
  223.                 break;
  224.             }
  225.  
  226.             if (shengming) {
  227.                 if (dzd.size() < 100) {
  228.                     zidan zd = null;
  229.                     switch (fangxiang) {
  230.                         case 0:
  231.                             zd = new zidan(x + 10, y, 0);
  232.                             dzd.add(zd);
  233.                             break;
  234.  
  235.                         case 1:
  236.                             zd = new zidan(x, y + 10, 1);
  237.                             dzd.add(zd);
  238.                             break;
  239.                         case 2:
  240.                             zd = new zidan(x + 10, y + 30, 2);
  241.                             dzd.add(zd);
  242.                             break;
  243.                         case 3:
  244.                             zd = new zidan(x + 30, y + 10, 3);
  245.                             dzd.add(zd);
  246.                             break;
  247.                     }
  248.                     Thread t4 = new Thread(zd);
  249.                     t4.start();
  250.                 }
  251.             }
  252.         }
  253.     }
  254.  
  255. }
  256.  
  257. class MyTank extends Tank {
  258.  
  259.     Vector<zidan> aa = new Vector<zidan>();
  260.     zidan zd = null;
  261.  
  262.     public MyTank(int x, int y) {
  263.         super(x, y);
  264.     }
  265.  
  266.     public void xiangshang() {
  267.         y -= sudu;
  268.     }
  269.  
  270.     public void xiangzuo() {
  271.         x -= sudu;
  272.     }
  273.  
  274.     public void xiangxia() {
  275.         y += sudu;
  276.     }
  277.  
  278.     public void xiangyou() {
  279.         x += sudu;
  280.     }
  281.  
  282.     public void fszd() {
  283.         switch (this.fangxiang) {
  284.             case 0:
  285.                 zd = new zidan(x + 10, y, 0);
  286.                 aa.add(zd);
  287.                 break;
  288.             case 1:
  289.                 zd = new zidan(x, y + 10, 1);
  290.                 aa.add(zd);
  291.                 break;
  292.             case 2:
  293.                 zd = new zidan(x + 10, y + 30, 2);
  294.                 aa.add(zd);
  295.                 break;
  296.             case 3:
  297.                 zd = new zidan(x + 30, y + 10, 3);
  298.                 aa.add(zd);
  299.                 break;
  300.         }
  301.         Thread t = new Thread(zd);
  302.         t.start();
  303.     }
  304. }
  305.  
  306. class zidan implements Runnable {
  307.  
  308.     int x, y;
  309.     int fangxiang;
  310.     int sudu = 10;
  311.     boolean shengming = true;
  312.  
  313.     public zidan(int x, int y, int fangxiang) {
  314.         this.x = x;
  315.         this.y = y;
  316.         this.fangxiang = fangxiang;
  317.     }
  318.  
  319.     @Override
  320.     public void run() {
  321.         while (true) {
  322.             try {
  323.                 Thread.sleep(100);
  324.                 // System.out.println("!!!");
  325.             } catch (InterruptedException e) {
  326.                 System.out.println("ERROR:" + e);
  327.             }
  328.  
  329.             switch (this.fangxiang) {
  330.                 case 0:
  331.                     y -= sudu;
  332.                     break;
  333.                 case 1:
  334.                     x -= sudu;
  335.                     break;
  336.                 case 2:
  337.                     y += sudu;
  338.                     break;
  339.                 case 3:
  340.                     x += sudu;
  341.                     break;
  342.             }
  343.             if (x < 0 || x > 500 || y < 0 || y > 400) {
  344.                 this.shengming = false;
  345.                 break;
  346.             }
  347.         }
  348.  
  349.     }
  350. }
  351.  
  352. class BaoZha {
  353.  
  354.     int x, y;
  355.     int shengcunqi = 9;
  356.     boolean shengming = true;
  357.  
  358.     public BaoZha(int x, int y) {
  359.         this.x = x;
  360.         this.y = y;
  361.     }
  362.  
  363.     public void suqsd() {
  364.         if (shengcunqi > 0) {
  365.             shengcunqi--;
  366.         } else {
  367.             this.shengming = false;
  368.         }
  369.     }
  370. }
  371.  
  372.  

回复 "坦克大战2"

这儿你可以回复上面这条便签

captcha