Java版坦克大战游戏源码示例

论坛 期权论坛 脚本     
niminba   2021-5-23 02:55   761   0

整理文档,搜刮出一个Java版坦克大战游戏的代码,稍微整理精简一下做下分享。

package tankwar;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

import tankwar.Tank.Direction;
import barrier.EnemyBorn;
import barrier.Gold;
import barrier.Home;
import barrier.Iron;
import barrier.SelfBorn;
import barrier.Wall;
/**
 * Java学习交流QQ群:589809992 我们一起学Java!
 */
public class TankWar implements KeyListener {
  static boolean TIMEOUT = false;
  private JFrame f;
  private JPanel gamePanel;
  private PanelShow messgePanel;
  private myPanel p;
  private Tank myTank;
  public static final int AREA_WIDTH = 830;
  public static final int AREA_HEIGHT = 800;
  private ArrayList<Missle> missles = new ArrayList<Missle>();
  private ArrayList<Tank> allTanks = new ArrayList<Tank>();
  private ArrayList<Boom> booms = new ArrayList<Boom>();
  private ArrayList<Wall> walls = new ArrayList<Wall>();
  private ArrayList<Iron> irons = new ArrayList<Iron>();
  private ArrayList<Gold> golds = new ArrayList<Gold>();
  private ArrayList<Item> items = new ArrayList<Item>();
  private ArrayList<EnemyBorn> enemyBorns = new ArrayList<EnemyBorn>();
  private SelfBorn selfBorn;
  private Home home;
  private Tank enemyTank;
  private Random r;
  private ImageIcon backGround;
  private final String map;
  private int tankMax;
  private boolean over = false;
  private static int selfMax = 3;
  private boolean win;
  private boolean flash = false;
  private TankWar tw = this;
  static int SCORE = 0;

  private final JFrame mainF;
  private int style;

  public TankWar(String map, int tankMax, JFrame mainF, int style)
      throws Exception {
    this.map = map;
    this.tankMax = tankMax;
    this.mainF = mainF;
    this.style = style;
    init();
  }

  private void init() {
    f = new JFrame("坦克大战 V3.0");
    gamePanel = new JPanel(null);
    p = new myPanel();
    p.setBackground(Color.WHITE);
    r = new Random();
    messgePanel = new PanelShow();
    initMap(new File("map/" + map));

    try {
      myTank = new Tank(selfBorn.getX(), selfBorn.getY(), true, allTanks,
          walls, irons, golds, missles, home, booms, style);
    } catch (Exception e1) {
    }
    myTank.setDir(Direction.U);

    allTanks.add(myTank);
    addTank();
    try {
      backGround = new ImageIcon(
          TankWar.class.getResource("/pic/whiteback.jpg"));
    } catch (Exception e) {
    }

    p.setBorder(BorderFactory.createEtchedBorder(Color.BLACK, Color.WHITE));
    p.setSize(AREA_WIDTH, AREA_HEIGHT);
    messgePanel.setBounds(AREA_WIDTH, 0, 200, AREA_HEIGHT);
    gamePanel.add(messgePanel);
    gamePanel.add(p);
    f.add(gamePanel);
    f.setBounds(0, 0, AREA_WIDTH + 200, AREA_HEIGHT);
    f.setDefaultCloseOperation(3);
    f.setResizable(true);
    f.setFocusable(true);
    f.addKeyListener(this);
    f.setVisible(true);

    new Thread(new Runnable() {
      public void run() {
        while (!over) {
          if (!myTank.isLive()) {
            selfMax--;
            if (selfMax < 0) {
              f.removeKeyListener(tw);
              over = true;
              win = false;
              break;
            } else {
              myTank.setLevel(1);
              myTank.setX(selfBorn.getX());
              myTank.setY(selfBorn.getY());
              myTank.setDir(Direction.U);
              myTank.setHp(50);
              myTank.setLive(true);
            }
          }
          if (tankMax <= 0 && allTanks.size() == 1) {
            f.removeKeyListener(tw);
            over = true;
            win = true;
          }
          if (!home.isLive()) {
            f.removeKeyListener(tw);
            over = true;
            win = false;
          }
          p.repaint();
          myTank.move();
          for (int i = 1; i < allTanks.size(); i++) {
            allTanks.get(i).move();
            allTanks.get(i).setNoFire(myTank.getNoFire() + 1);
            // if(allTanks.get(i).getX()%5==0&&allTanks.get(i).getY()%5==0)
            aI(allTanks.get(i));
          }
          if (allTanks.size() <= enemyBorns.size() + 1)
            addTank();
          myTank.setNoFire(myTank.getNoFire() + 1);
          messgePanel.setEnemyCount(tankMax);
          messgePanel.setSelfCount(selfMax);
          messgePanel.setScore(SCORE);
          if (SCORE % 500 == 0) {
            SCORE += 100;
            Item item = new Item(allTanks, booms, irons, home);
            items.add(item);
            item.start();
          }
          try {
            Thread.sleep(30);
          } catch (InterruptedException e) {
          }
        }

        over();
      }

    }).start();

  }

  private class myPanel extends JPanel {
    /**
     * 
     */
    private static final long serialVersionUID = 4408440723797225328L;

    @Override
    public void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.drawImage(backGround.myBorns.add(new EnemyBorn(j * 50, i * 50));
            break;
          case 6:
            home = new Home(j * 50, i * 50);
            break;
          }
        }
      }
      read.close();
    } catch (Exception e) {
    }
    ;
  }

  public void addTank() {
    if (tankMax <= 0)
      return;
    for (int i = allTanks.size(); i < enemyBorns.size() + 1; i++) {
      try {
        int temp = r.nextInt(enemyBorns.size());
        enemyTank = new Tank(enemyBorns.get(temp).getX(), enemyBorns
            .get(temp).getY(), false, allTanks, walls, irons,
            golds, missles, home, booms, r.nextInt(3) + 1);
      } catch (Exception e) {
        e.printStackTrace();
      }
      enemyTank.setDir(Direction.D);
      enemyTank.setDown(true);
      allTanks.add(enemyTank);
      tankMax--;
      if (tankMax <= 0)
        return;
    }
  }

  public static void addMyTankLives() {
    selfMax++;
  }

  private void over() {

    for (int i = 0; i < AREA_HEIGHT / 50; i++) {
      for (int j = 0; j < AREA_WIDTH / 50; j++) {
        irons.add(new Iron(j * 50, i * 50));
        p.repaint();
        try {
          Thread.sleep(5);
        } catch (InterruptedException e) {
        }
      }
    }
    while (true) {
      flash = !flash;
      p.repaint();
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
      }
      f.addKeyListener(this);
    }
  }

  private void drawOver(Graphics g) {
    p.repaint();
    g.setColor(Color.red);
    g.setFont(new Font("Arial", 1, 100));
    g.drawString("GAME OVER", 100, 200);
    g.setFont(new Font("Arial", 2, 50));
    if (win)
      g.drawString("Congratulation! You Win!", 100, 400);
    else
      g.drawString("So Sorry, You Lose!", 100, 400);
    if (flash) {
      g.setFont(new Font("Arial", 2, 30));
      g.setColor(Color.BLACK);
      g.drawString("Press F1 to try again...,", 150, 500);
      g.drawString("Press the other Key to Return the Title...,", 150,
          600);
    }
  }
}

代码运行效果截图

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持社区。

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1060120
帖子:212021
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP