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

来自 , 2019-10-24, 写在 Java, 查看 135 次.
URL http://www.code666.cn/view/c6036a69
  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.awt.Color;
  9. import java.awt.Graphics;
  10. import java.awt.event.KeyEvent;
  11. import java.awt.event.KeyListener;
  12. import java.util.Vector;
  13. import javax.swing.ImageIcon;
  14. import javax.swing.JFrame;
  15. import javax.swing.JPanel;
  16.  
  17. /**
  18.  *
  19.  * @author Dell-
  20.  */
  21. public class Tank1 extends JFrame {
  22.  
  23.     MyPanel mp = null;
  24. //    GuanQia gqmb  =null;
  25.     public static void main(String[] args) {
  26.         Tank1 t = new Tank1();
  27.     }
  28.  
  29.     public Tank1() {
  30.        mp = new MyPanel();
  31.        Thread th = new Thread(mp);
  32.        th.start();
  33.        
  34.    //     gqmb = new GuanQia();
  35.        
  36.    //     this.add(gqmb);
  37.        this.add(mp);
  38.      this.addKeyListener(mp);
  39.         this.setSize(600,500);
  40.         this.setLocation(300, 280);
  41.         this.setDefaultCloseOperation(3);
  42.         this.setVisible(true);
  43.     }
  44. }
  45.  
  46. class MyPanel extends JPanel implements KeyListener, Runnable {
  47.  
  48.     MyTank mt = null;
  49.     Vector<DiTank> dtk = new Vector<>();
  50.     Vector<BaoZha> baozha = new Vector<>();
  51.     int tks = 5;
  52.     ImageIcon tp1 = new ImageIcon("e:\\b1.gif");
  53.     ImageIcon tp2 = new ImageIcon("e:\\b2.gif");
  54.     ImageIcon tp3 = new ImageIcon("e:\\b3.gif");
  55.  
  56.     public MyPanel() {
  57.         //给定坦克左上角的坐标,也就是坦克出现的地方!
  58.         mt = new MyTank(250,300);
  59.        
  60.         for (int i = 0; i < tks; i++) {
  61.             DiTank dt = new DiTank((i) * 100 + 5, 0);
  62.             Thread t = new Thread(dt);
  63.             t.start();
  64.             zidan zd = new zidan(dt.x + 10, dt.y + 30, 2);
  65.             dt.dzd.add(zd);
  66.             Thread t2 = new Thread(zd);
  67.             t2.start();
  68.             dtk.add(dt);
  69.         }
  70.  
  71.     }
  72.  
  73.     @Override
  74.     public void paint(Graphics g) {
  75.         super.paint(g);
  76.         g.fillRect(0, 0, 500, 400);
  77.         if (this.mt.shengming) {
  78.             this.drawTank(mt.getX(), mt.getY(), g, mt.fangxiang, 0);
  79.         }
  80.         for (int i = 0; i < dtk.size(); i++) {
  81.  
  82.             if (dtk.get(i).shengming) {
  83.                 this.drawTank(dtk.get(i).getX(), dtk.get(i).getY(), g, dtk.get(i).fangxiang, 1);
  84.  
  85.             }
  86.         }
  87.  
  88.         for (int i = 0; i < mt.aa.size(); i++) {
  89.             zidan zd = mt.aa.get(i);
  90.             if (zd != null && zd.shengming == true) {
  91.                 g.setColor(Color.red);
  92.                 g.fill3DRect(zd.x, zd.y, 3, 3, false);
  93.             }
  94.             if (zd.shengming == false) {
  95.                 mt.aa.remove(zd);
  96.             }
  97.         }
  98.  
  99.         for (int i = 0; i < dtk.size(); i++) {
  100.             DiTank dt = dtk.get(i);
  101.             for (int j = 0; j < dt.dzd.size(); j++) {
  102.                 zidan zd1 = dt.dzd.get(j);
  103.                 if (zd1 != null && zd1.shengming == true) {
  104.                     g.setColor(Color.white);
  105.                     g.fill3DRect(zd1.x, zd1.y, 3, 3, false);
  106.                 }
  107.                 if (zd1.shengming == false) {
  108.                     mt.aa.remove(zd1);
  109.                 }
  110.             }
  111.         }
  112.  
  113.         for (int i = 0; i < baozha.size(); i++) {
  114.             System.out.println("!!!");
  115.             BaoZha bz = baozha.get(i);
  116.  
  117.             if (bz.shengcunqi > 6) {
  118.                 g.drawImage(tp1.getImage(), bz.x, bz.y, this);
  119.             } else if (bz.shengcunqi > 3) {
  120.                 g.drawImage(tp2.getImage(), bz.x, bz.y, this);
  121.             } else if (bz.shengcunqi > 0) {
  122.                 g.drawImage(tp3.getImage(), bz.x, bz.y, this);
  123.             }
  124.             bz.suqsd();
  125.             if (bz.shengcunqi == 0) {
  126.                 baozha.remove(bz);
  127.             }
  128.         }
  129.     }
  130.  
  131.     public void jw(){
  132.         for(int i = 0 ;i<this.dtk.size();i++ ){
  133.             DiTank dt = dtk.get(i);
  134.             for(int j =0;j<dt.dzd.size();j++){
  135.                 zidan zd = dt.dzd.get(j);
  136.                 this.jzdf(zd, mt);
  137.             }
  138.         }
  139.     }
  140.     public void jd(){
  141.         for(int i = 0 ;i<mt.aa.size();i++){
  142.             zidan zd = mt.aa.get(i);
  143.             if(zd.shengming){
  144.                 for(int j=0 ; j<dtk.size();j++){
  145.                     DiTank dt = dtk.get(j);
  146.                     if(dt.shengming){
  147.                         this.jzdf(zd, dt);
  148.                     }
  149.                 }
  150.             }
  151.         }
  152.     }
  153.    
  154.     public void jzdf(zidan zd, Tank dt) {
  155.         switch (dt.fangxiang) {
  156.             case 0:
  157.             case 2:
  158.                 if (zd.x > dt.x && zd.x < dt.x + 20 && zd.y > dt.y && zd.y < dt.y + 30) {
  159.                     zd.shengming = false;
  160.                     dt.shengming = false;
  161.                     BaoZha b = new BaoZha(dt.x, dt.y);
  162.                     baozha.add(b);
  163.                 }
  164.                 break;
  165.             case 1:
  166.             case 3:
  167.                 if (zd.x > dt.x && zd.x < dt.x + 30 && zd.y > dt.y && zd.y < dt.y + 20) {
  168.                     zd.shengming = false;
  169.                     dt.shengming = false;
  170.                     BaoZha b = new BaoZha(dt.x, dt.y);
  171.                     baozha.add(b);
  172.                 }
  173.         }
  174.     }
  175.  
  176.     public void drawTank(int x, int y, Graphics g, int fangxiang, int leixing) {
  177.  
  178.         switch (leixing) {
  179.             case 0:
  180.                 g.setColor(Color.ORANGE);
  181.                 break;
  182.             case 1:
  183.                 g.setColor(Color.red);
  184.                 break;
  185.         }
  186.  
  187.         switch (fangxiang) {
  188.             case 0:
  189.                 g.fill3DRect(x, y, 5, 30, false);
  190.                 g.fill3DRect(x + 15, y, 5, 30, false);
  191.                 g.fill3DRect(x + 5, y + 5, 10, 20, false);
  192.                 g.fillOval(x + 5, y + 10, 10, 10);
  193.                 g.drawLine(x + 10, y + 15, x + 10, y - 5);
  194.                 break;
  195.             case 1:
  196.                 g.fill3DRect(x, y, 30, 5, false);
  197.                 g.fill3DRect(x, y + 15, 30, 5, false);
  198.                 g.fill3DRect(x + 5, y + 5, 20, 10, true);
  199.                 g.fillOval(x + 10, y + 5, 10, 10);
  200.                 g.drawLine(x + 15, y + 10, x - 6, y + 10);
  201.                 break;
  202.             case 2:
  203.                 g.fill3DRect(x, y, 5, 30, false);
  204.                 g.fill3DRect(x + 15, y, 5, 30, false);
  205.                 g.fill3DRect(x + 5, y + 5, 10, 20, false);
  206.                 g.fillOval(x + 5, y + 10, 10, 10);
  207.                 g.drawLine(x + 10, y + 15, x + 10, y + 33);
  208.                 break;
  209.             case 3:
  210.                 g.fill3DRect(x, y, 30, 5, false);
  211.                 g.fill3DRect(x, y + 15, 30, 5, false);
  212.                 g.fill3DRect(x + 5, y + 5, 20, 10, true);
  213.                 g.fillOval(x + 10, y + 5, 10, 10);
  214.                 g.drawLine(x + 15, y + 10, x + 33, y + 10);
  215.                 break;
  216.  
  217.         }
  218.  
  219.     }
  220.  
  221.     @Override
  222.     public void keyTyped(KeyEvent e) {
  223.     }
  224.  
  225.     @Override
  226.     public void keyReleased(KeyEvent e) {
  227.     }
  228.  
  229.     @Override
  230.     public void keyPressed(KeyEvent e) {
  231.         if (e.getKeyCode() == KeyEvent.VK_W) {
  232.             this.mt.setFangxiang(0);
  233.             this.mt.xiangshang();
  234.         } else if (e.getKeyCode() == KeyEvent.VK_A) {
  235.             this.mt.setFangxiang(1);
  236.             this.mt.xiangzuo();
  237.         } else if (e.getKeyCode() == KeyEvent.VK_S) {
  238.             this.mt.setFangxiang(2);
  239.             this.mt.xiangxia();
  240.  
  241.         } else if (e.getKeyCode() == KeyEvent.VK_D) {
  242.             this.mt.setFangxiang(3);
  243.             this.mt.xiangyou();
  244.         }
  245.         if (e.getKeyCode() == KeyEvent.VK_SPACE) {
  246.  
  247.             if (this.mt.aa.size() < 8) {
  248.                 this.mt.fszd();
  249.             }
  250.         }
  251.         this.repaint();
  252.     }
  253.  
  254.     @Override
  255.     public void run() {
  256.         while (true) {
  257.             try {
  258.                 Thread.sleep(100);
  259.             } catch (InterruptedException e) {
  260.                 System.out.println("ERROR:" + e);
  261.             }
  262.             jd();
  263.             jw();
  264.             this.repaint();
  265.  
  266.         }
  267.     }
  268.  
  269. }
  270.  

回复 "坦克大战1"

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

captcha