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

来自 , 2019-09-14, 写在 Java, 查看 135 次.
URL http://www.code666.cn/view/d63fbf8c
  1. package tankwar;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.Graphics;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.event.KeyListener;
  8. import java.io.File;
  9. import java.io.FileInputStream;
  10. import java.util.ArrayList;
  11. import java.util.Random;
  12.  
  13. import javax.swing.BorderFactory;
  14. import javax.swing.ImageIcon;
  15. import javax.swing.JFrame;
  16. import javax.swing.JPanel;
  17.  
  18. import tankwar.Tank.Direction;
  19. import barrier.EnemyBorn;
  20. import barrier.Gold;
  21. import barrier.Home;
  22. import barrier.Iron;
  23. import barrier.SelfBorn;
  24. import barrier.Wall;
  25.  
  26. public class TankWar implements KeyListener {
  27.     static boolean TIMEOUT = false;
  28.     private JFrame f;
  29.     private JPanel gamePanel;
  30.     private PanelShow messgePanel;
  31.     private myPanel p;
  32.     private Tank myTank;
  33.     public static final int AREA_WIDTH = 830;
  34.     public static final int AREA_HEIGHT = 800;
  35.     private ArrayList<Missle> missles = new ArrayList<Missle>();
  36.     private ArrayList<Tank> allTanks = new ArrayList<Tank>();
  37.     private ArrayList<Boom> booms = new ArrayList<Boom>();
  38.     private ArrayList<Wall> walls = new ArrayList<Wall>();
  39.     private ArrayList<Iron> irons = new ArrayList<Iron>();
  40.     private ArrayList<Gold> golds = new ArrayList<Gold>();
  41.     private ArrayList<Item> items = new ArrayList<Item>();
  42.     private ArrayList<EnemyBorn> enemyBorns = new ArrayList<EnemyBorn>();
  43.     private SelfBorn selfBorn;
  44.     private Home home;
  45.     private Tank enemyTank;
  46.     private Random r;
  47.     private ImageIcon backGround;
  48.     private final String map;
  49.     private int tankMax;
  50.     private boolean over = false;
  51.     private static int selfMax = 3;
  52.     private boolean win;
  53.     private boolean flash = false;
  54.     private TankWar tw = this;
  55.     static int SCORE = 0;
  56.  
  57.     private final JFrame mainF;
  58.     private int style;
  59.  
  60.     public TankWar(String map, int tankMax, JFrame mainF, int style)
  61.             throws Exception {
  62.         this.map = map;
  63.         this.tankMax = tankMax;
  64.         this.mainF = mainF;
  65.         this.style = style;
  66.         init();
  67.     }
  68.  
  69.     private void init() {
  70.         f = new JFrame("坦克大战 V3.0");
  71.         gamePanel = new JPanel(null);
  72.         p = new myPanel();
  73.         p.setBackground(Color.WHITE);
  74.         r = new Random();
  75.         messgePanel = new PanelShow();
  76.         initMap(new File("map/" + map));
  77.  
  78.         try {
  79.             myTank = new Tank(selfBorn.getX(), selfBorn.getY(), true, allTanks,
  80.                     walls, irons, golds, missles, home, booms, style);
  81.         } catch (Exception e1) {
  82.         }
  83.         myTank.setDir(Direction.U);
  84.  
  85.         allTanks.add(myTank);
  86.         addTank();
  87.         try {
  88.             backGround = new ImageIcon(
  89.                     TankWar.class.getResource("/pic/whiteback.jpg"));
  90.         } catch (Exception e) {
  91.         }
  92.  
  93.         p.setBorder(BorderFactory.createEtchedBorder(Color.BLACK, Color.WHITE));
  94.         p.setSize(AREA_WIDTH, AREA_HEIGHT);
  95.         messgePanel.setBounds(AREA_WIDTH, 0, 200, AREA_HEIGHT);
  96.         gamePanel.add(messgePanel);
  97.         gamePanel.add(p);
  98.         f.add(gamePanel);
  99.         f.setBounds(0, 0, AREA_WIDTH + 200, AREA_HEIGHT);
  100.         f.setDefaultCloseOperation(3);
  101.         f.setResizable(true);
  102.         f.setFocusable(true);
  103.         f.addKeyListener(this);
  104.         f.setVisible(true);
  105.  
  106.         new Thread(new Runnable() {
  107.             public void run() {
  108.                 while (!over) {
  109.                     if (!myTank.isLive()) {
  110.                         selfMax--;
  111.                         if (selfMax < 0) {
  112.                             f.removeKeyListener(tw);
  113.                             over = true;
  114.                             win = false;
  115.                             break;
  116.                         } else {
  117.                             myTank.setLevel(1);
  118.                             myTank.setX(selfBorn.getX());
  119.                             myTank.setY(selfBorn.getY());
  120.                             myTank.setDir(Direction.U);
  121.                             myTank.setHp(50);
  122.                             myTank.setLive(true);
  123.                         }
  124.                     }
  125.                     if (tankMax <= 0 && allTanks.size() == 1) {
  126.                         f.removeKeyListener(tw);
  127.                         over = true;
  128.                         win = true;
  129.                     }
  130.                     if (!home.isLive()) {
  131.                         f.removeKeyListener(tw);
  132.                         over = true;
  133.                         win = false;
  134.                     }
  135.                     p.repaint();
  136.                     myTank.move();
  137.                     for (int i = 1; i < allTanks.size(); i++) {
  138.                         allTanks.get(i).move();
  139.                         allTanks.get(i).setNoFire(myTank.getNoFire() + 1);
  140.                         // if(allTanks.get(i).getX()%5==0&&allTanks.get(i).getY()%5==0)
  141.                         aI(allTanks.get(i));
  142.                     }
  143.                     if (allTanks.size() <= enemyBorns.size() + 1)
  144.                         addTank();
  145.                     myTank.setNoFire(myTank.getNoFire() + 1);
  146.                     messgePanel.setEnemyCount(tankMax);
  147.                     messgePanel.setSelfCount(selfMax);
  148.                     messgePanel.setScore(SCORE);
  149.                     if (SCORE % 500 == 0) {
  150.                         SCORE += 100;
  151.                         Item item = new Item(allTanks, booms, irons, home);
  152.                         items.add(item);
  153.                         item.start();
  154.                     }
  155.                     try {
  156.                         Thread.sleep(30);
  157.                     } catch (InterruptedException e) {
  158.                     }
  159.                 }
  160.  
  161.                 over();
  162.             }
  163.  
  164.         }).start();
  165.  
  166.     }
  167.  
  168.     private class myPanel extends JPanel {
  169.         /**
  170.          *
  171.          */
  172.         private static final long serialVersionUID = 4408440723797225328L;
  173.  
  174.         @Override
  175.         public void paintComponent(Graphics g) {
  176.             super.paintComponent(g);
  177.             g.drawImage(backGround.getImage(), 0, 0, null);
  178.             for (int j = 0; j < walls.size(); j++) {
  179.                 walls.get(j).draw(g);
  180.             }
  181.             for (int j = 0; j < irons.size(); j++) {
  182.                 irons.get(j).draw(g);
  183.             }
  184.             for (int j = 0; j < golds.size(); j++) {
  185.                 golds.get(j).draw(g);
  186.             }
  187.             for (int j = 0; j < enemyBorns.size(); j++) {
  188.                 enemyBorns.get(j).draw(g);
  189.             }
  190.             home.draw(g);
  191.             selfBorn.draw(g);
  192.  
  193.             for (int j = 0; j < allTanks.size(); j++) {
  194.                 allTanks.get(j).drawTank(g);
  195.             }
  196.             for (int j = 0; j < irons.size(); j++) {
  197.                 irons.get(j).draw(g);
  198.             }
  199.  
  200.             for (int i = 0; i < missles.size(); i++) {
  201.                 missles.get(i).drawMissle(g);
  202.                 if (!missles.get(i).isLive())
  203.                     missles.remove(i);
  204.             }
  205.             for (int i = 0; i < booms.size(); i++) {
  206.                 if (booms.get(i).isLive())
  207.                     booms.get(i).drawBoom(g);
  208.                 else
  209.                     booms.remove(i);
  210.             }
  211.             for (int j = 0; j < items.size(); j++) {
  212.                 if (!items.get(j).isLive()) {
  213.                     items.remove(j);
  214.                     continue;
  215.                 }
  216.                 items.get(j).draw(g);
  217.             }
  218.             if (over)
  219.                 drawOver(g);
  220.             messgePanel.repaint();
  221.         }
  222.     }
  223.  
  224.     @Override
  225.     public void keyTyped(KeyEvent e) {
  226.         // TODO Auto-generated method stub
  227.  
  228.     }
  229.  
  230.     @Override
  231.     public void keyPressed(KeyEvent e) {
  232.         if (over) {
  233.             if (e.getKeyCode() == KeyEvent.VK_F1) {
  234.                 over = false;
  235.                 missles.clear();
  236.                 allTanks.clear();
  237.                 booms.clear();
  238.                 walls.clear();
  239.                 irons.clear();
  240.                 golds.clear();
  241.                 enemyBorns.clear();
  242.                 try {
  243.                     init();
  244.                 } catch (Exception e1) {
  245.                 }
  246.             } else {
  247.                 f.setVisible(false);
  248.                 mainF.setSize(800, 800);
  249.                 mainF.setVisible(true);
  250.             }
  251.         } else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
  252.             myTank.fire();
  253.         } else {
  254.             myTank.keyPress(e);
  255.         }
  256.     }
  257.  
  258.     @Override
  259.     public void keyReleased(KeyEvent e) {
  260.  
  261.         myTank.keyReleased(e);
  262.     }
  263.  
  264.     public void aI(Tank tank) {
  265.         if (TIMEOUT) {
  266.             tank.setUp(false);
  267.             tank.setLeft(false);
  268.             tank.setDown(false);
  269.             tank.setRight(false);
  270.             return;
  271.         }
  272.  
  273.         if (r.nextInt(40) == 0)
  274.             tank.fire();
  275.         if (r.nextInt(10) == 0) {
  276.             if (tank.getX() >= myTank.getX()
  277.                     && tank.getX() <= myTank.getX() + Tank.SIZE
  278.                     && tank.getY() > myTank.getY()) {
  279.                 tank.setUp(true);
  280.                 tank.setLeft(false);
  281.                 tank.setDown(false);
  282.                 tank.setRight(false);
  283.                 tank.setDir(Direction.U);
  284.                 return;
  285.             } else if (tank.getX() >= myTank.getX()
  286.                     && tank.getX() <= myTank.getX() + Tank.SIZE
  287.                     && tank.getY() < myTank.getY()) {
  288.                 tank.setUp(false);
  289.                 tank.setLeft(false);
  290.                 tank.setDown(true);
  291.                 tank.setRight(false);
  292.                 tank.setDir(Direction.D);
  293.                 return;
  294.             }
  295.  
  296.             else if (tank.getX() > myTank.getX()
  297.                     && tank.getY() >= myTank.getY()
  298.                     && tank.getY() <= myTank.getY() + Tank.SIZE) {
  299.                 tank.setUp(false);
  300.                 tank.setLeft(true);
  301.                 tank.setDown(false);
  302.                 tank.setRight(false);
  303.                 tank.setDir(Direction.L);
  304.                 return;
  305.             } else if (tank.getX() < myTank.getX()
  306.                     && tank.getY() >= myTank.getY()
  307.                     && tank.getY() <= myTank.getY() + Tank.SIZE) {
  308.                 tank.setUp(false);
  309.                 tank.setLeft(false);
  310.                 tank.setDown(false);
  311.                 tank.setRight(true);
  312.                 tank.setDir(Direction.R);
  313.                 return;
  314.             }
  315.         }
  316.         if (tank.getX() <= 0) {
  317.             tank.setUp(false);
  318.             tank.setLeft(false);
  319.             tank.setDown(false);
  320.             tank.setRight(true);
  321.             tank.setDir(Direction.R);
  322.         }
  323.         if (tank.getY() <= 0) {
  324.             tank.setUp(false);
  325.             tank.setLeft(false);
  326.             tank.setDown(true);
  327.             tank.setRight(false);
  328.             tank.setDir(Direction.D);
  329.         }
  330.         if (tank.getX() >= AREA_WIDTH - Tank.SIZE) {
  331.             tank.setUp(false);
  332.             tank.setLeft(true);
  333.             tank.setDown(false);
  334.             tank.setRight(false);
  335.             tank.setDir(Direction.L);
  336.         }
  337.         if (tank.getY() >= AREA_HEIGHT - Tank.SIZE) {
  338.             tank.setUp(true);
  339.             tank.setLeft(false);
  340.             tank.setDown(false);
  341.             tank.setRight(false);
  342.             tank.setDir(Direction.U);
  343.         } else if (r.nextInt(300) == 1) {
  344.             tank.setUp(true);
  345.             tank.setLeft(false);
  346.             tank.setDown(false);
  347.             tank.setRight(false);
  348.             tank.setDir(Direction.U);
  349.         }
  350.  
  351.         else if (r.nextInt(300) == 2) {
  352.             tank.setUp(false);
  353.             tank.setLeft(true);
  354.             tank.setDown(false);
  355.             tank.setRight(false);
  356.             tank.setDir(Direction.L);
  357.         } else if (r.nextInt(300) == 3) {
  358.             tank.setUp(false);
  359.             tank.setLeft(false);
  360.             tank.setDown(true);
  361.             tank.setRight(false);
  362.             tank.setDir(Direction.D);
  363.         } else if (r.nextInt(300) == 4) {
  364.             tank.setUp(false);
  365.             tank.setLeft(false);
  366.             tank.setDown(false);
  367.             tank.setRight(true);
  368.             tank.setDir(Direction.R);
  369.         }
  370.     }
  371.  
  372.     public void initMap(File file) {
  373.         try {
  374.             FileInputStream read = new FileInputStream(file);
  375.             for (int i = 0; i < AREA_HEIGHT / 50; i++) {
  376.                 for (int j = 0; j < AREA_WIDTH / 50; j++) {
  377.                     switch (read.read()) {
  378.                     case 1:
  379.                         walls.add(new Wall(j * 50, i * 50));
  380.                         break;
  381.                     case 2:
  382.                         irons.add(new Iron(j * 50, i * 50));
  383.                         break;
  384.                     case 3:
  385.                         golds.add(new Gold(j * 50, i * 50));
  386.                         break;
  387.                     case 4:
  388.                         selfBorn = new SelfBorn(j * 50, i * 50);
  389.                         break;
  390.                     case 5:
  391.                         enemyBorns.add(new EnemyBorn(j * 50, i * 50));
  392.                         break;
  393.                     case 6:
  394.                         home = new Home(j * 50, i * 50);
  395.                         break;
  396.                     }
  397.                 }
  398.             }
  399.             read.close();
  400.         } catch (Exception e) {
  401.         }
  402.         ;
  403.     }
  404.  
  405.     public void addTank() {
  406.         if (tankMax <= 0)
  407.             return;
  408.         for (int i = allTanks.size(); i < enemyBorns.size() + 1; i++) {
  409.             try {
  410.                 int temp = r.nextInt(enemyBorns.size());
  411.                 enemyTank = new Tank(enemyBorns.get(temp).getX(), enemyBorns
  412.                         .get(temp).getY(), false, allTanks, walls, irons,
  413.                         golds, missles, home, booms, r.nextInt(3) + 1);
  414.             } catch (Exception e) {
  415.                 e.printStackTrace();
  416.             }
  417.             enemyTank.setDir(Direction.D);
  418.             enemyTank.setDown(true);
  419.             allTanks.add(enemyTank);
  420.             tankMax--;
  421.             if (tankMax <= 0)
  422.                 return;
  423.         }
  424.     }
  425.  
  426.     public static void addMyTankLives() {
  427.         selfMax++;
  428.     }
  429.  
  430.     private void over() {
  431.  
  432.         for (int i = 0; i < AREA_HEIGHT / 50; i++) {
  433.             for (int j = 0; j < AREA_WIDTH / 50; j++) {
  434.                 irons.add(new Iron(j * 50, i * 50));
  435.                 p.repaint();
  436.                 try {
  437.                     Thread.sleep(5);
  438.                 } catch (InterruptedException e) {
  439.                 }
  440.             }
  441.         }
  442.         while (true) {
  443.             flash = !flash;
  444.             p.repaint();
  445.             try {
  446.                 Thread.sleep(1000);
  447.             } catch (InterruptedException e) {
  448.             }
  449.             f.addKeyListener(this);
  450.         }
  451.     }
  452.  
  453.     private void drawOver(Graphics g) {
  454.         p.repaint();
  455.         g.setColor(Color.red);
  456.         g.setFont(new Font("Arial", 1, 100));
  457.         g.drawString("GAME OVER", 100, 200);
  458.         g.setFont(new Font("Arial", 2, 50));
  459.         if (win)
  460.             g.drawString("Congratulation!  You Win!", 100, 400);
  461.         else
  462.             g.drawString("So Sorry,  You Lose!", 100, 400);
  463.         if (flash) {
  464.             g.setFont(new Font("Arial", 2, 30));
  465.             g.setColor(Color.BLACK);
  466.             g.drawString("Press F1 to try again...,", 150, 500);
  467.             g.drawString("Press the other Key to Return the Title...,", 150,
  468.                     600);
  469.         }
  470.     }
  471. }
  472.  

回复 "坦克大战"

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

captcha