天天看點

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 = ;
    public static final int AREA_HEIGHT = ;
    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 = ;
    private boolean win;
    private boolean flash = false;
    private TankWar tw = this;
    static int SCORE = ;

    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, , , AREA_HEIGHT);
        gamePanel.add(messgePanel);
        gamePanel.add(p);
        f.add(gamePanel);
        f.setBounds(, , AREA_WIDTH + , AREA_HEIGHT);
        f.setDefaultCloseOperation();
        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 < ) {
                            f.removeKeyListener(tw);
                            over = true;
                            win = false;
                            break;
                        } else {
                            myTank.setLevel();
                            myTank.setX(selfBorn.getX());
                            myTank.setY(selfBorn.getY());
                            myTank.setDir(Direction.U);
                            myTank.setHp();
                            myTank.setLive(true);
                        }
                    }
                    if (tankMax <=  && allTanks.size() == ) {
                        f.removeKeyListener(tw);
                        over = true;
                        win = true;
                    }
                    if (!home.isLive()) {
                        f.removeKeyListener(tw);
                        over = true;
                        win = false;
                    }
                    p.repaint();
                    myTank.move();
                    for (int i = ; i < allTanks.size(); i++) {
                        allTanks.get(i).move();
                        allTanks.get(i).setNoFire(myTank.getNoFire() + );
                        // if(allTanks.get(i).getX()%5==0&&allTanks.get(i).getY()%5==0)
                        aI(allTanks.get(i));
                    }
                    if (allTanks.size() <= enemyBorns.size() + )
                        addTank();
                    myTank.setNoFire(myTank.getNoFire() + );
                    messgePanel.setEnemyCount(tankMax);
                    messgePanel.setSelfCount(selfMax);
                    messgePanel.setScore(SCORE);
                    if (SCORE %  == ) {
                        SCORE += ;
                        Item item = new Item(allTanks, booms, irons, home);
                        items.add(item);
                        item.start();
                    }
                    try {
                        Thread.sleep();
                    } catch (InterruptedException e) {
                    }
                }

                over();
            }

        }).start();

    }

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

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(backGround.getImage(), , , null);
            for (int j = ; j < walls.size(); j++) {
                walls.get(j).draw(g);
            }
            for (int j = ; j < irons.size(); j++) {
                irons.get(j).draw(g);
            }
            for (int j = ; j < golds.size(); j++) {
                golds.get(j).draw(g);
            }
            for (int j = ; j < enemyBorns.size(); j++) {
                enemyBorns.get(j).draw(g);
            }
            home.draw(g);
            selfBorn.draw(g);

            for (int j = ; j < allTanks.size(); j++) {
                allTanks.get(j).drawTank(g);
            }
            for (int j = ; j < irons.size(); j++) {
                irons.get(j).draw(g);
            }

            for (int i = ; i < missles.size(); i++) {
                missles.get(i).drawMissle(g);
                if (!missles.get(i).isLive())
                    missles.remove(i);
            }
            for (int i = ; i < booms.size(); i++) {
                if (booms.get(i).isLive())
                    booms.get(i).drawBoom(g);
                else
                    booms.remove(i);
            }
            for (int j = ; j < items.size(); j++) {
                if (!items.get(j).isLive()) {
                    items.remove(j);
                    continue;
                }
                items.get(j).draw(g);
            }
            if (over)
                drawOver(g);
            messgePanel.repaint();
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (over) {
            if (e.getKeyCode() == KeyEvent.VK_F1) {
                over = false;
                missles.clear();
                allTanks.clear();
                booms.clear();
                walls.clear();
                irons.clear();
                golds.clear();
                enemyBorns.clear();
                try {
                    init();
                } catch (Exception e1) {
                }
            } else {
                f.setVisible(false);
                mainF.setSize(, );
                mainF.setVisible(true);
            }
        } else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
            myTank.fire();
        } else {
            myTank.keyPress(e);
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

        myTank.keyReleased(e);
    }

    public void aI(Tank tank) {
        if (TIMEOUT) {
            tank.setUp(false);
            tank.setLeft(false);
            tank.setDown(false);
            tank.setRight(false);
            return;
        }

        if (r.nextInt() == )
            tank.fire();
        if (r.nextInt() == ) {
            if (tank.getX() >= myTank.getX()
                    && tank.getX() <= myTank.getX() + Tank.SIZE
                    && tank.getY() > myTank.getY()) {
                tank.setUp(true);
                tank.setLeft(false);
                tank.setDown(false);
                tank.setRight(false);
                tank.setDir(Direction.U);
                return;
            } else if (tank.getX() >= myTank.getX()
                    && tank.getX() <= myTank.getX() + Tank.SIZE
                    && tank.getY() < myTank.getY()) {
                tank.setUp(false);
                tank.setLeft(false);
                tank.setDown(true);
                tank.setRight(false);
                tank.setDir(Direction.D);
                return;
            }

            else if (tank.getX() > myTank.getX()
                    && tank.getY() >= myTank.getY()
                    && tank.getY() <= myTank.getY() + Tank.SIZE) {
                tank.setUp(false);
                tank.setLeft(true);
                tank.setDown(false);
                tank.setRight(false);
                tank.setDir(Direction.L);
                return;
            } else if (tank.getX() < myTank.getX()
                    && tank.getY() >= myTank.getY()
                    && tank.getY() <= myTank.getY() + Tank.SIZE) {
                tank.setUp(false);
                tank.setLeft(false);
                tank.setDown(false);
                tank.setRight(true);
                tank.setDir(Direction.R);
                return;
            }
        }
        if (tank.getX() <= ) {
            tank.setUp(false);
            tank.setLeft(false);
            tank.setDown(false);
            tank.setRight(true);
            tank.setDir(Direction.R);
        }
        if (tank.getY() <= ) {
            tank.setUp(false);
            tank.setLeft(false);
            tank.setDown(true);
            tank.setRight(false);
            tank.setDir(Direction.D);
        }
        if (tank.getX() >= AREA_WIDTH - Tank.SIZE) {
            tank.setUp(false);
            tank.setLeft(true);
            tank.setDown(false);
            tank.setRight(false);
            tank.setDir(Direction.L);
        }
        if (tank.getY() >= AREA_HEIGHT - Tank.SIZE) {
            tank.setUp(true);
            tank.setLeft(false);
            tank.setDown(false);
            tank.setRight(false);
            tank.setDir(Direction.U);
        } else if (r.nextInt() == ) {
            tank.setUp(true);
            tank.setLeft(false);
            tank.setDown(false);
            tank.setRight(false);
            tank.setDir(Direction.U);
        }

        else if (r.nextInt() == ) {
            tank.setUp(false);
            tank.setLeft(true);
            tank.setDown(false);
            tank.setRight(false);
            tank.setDir(Direction.L);
        } else if (r.nextInt() == ) {
            tank.setUp(false);
            tank.setLeft(false);
            tank.setDown(true);
            tank.setRight(false);
            tank.setDir(Direction.D);
        } else if (r.nextInt() == ) {
            tank.setUp(false);
            tank.setLeft(false);
            tank.setDown(false);
            tank.setRight(true);
            tank.setDir(Direction.R);
        }
    }

    public void initMap(File file) {
        try {
            FileInputStream read = new FileInputStream(file);
            for (int i = ; i < AREA_HEIGHT / ; i++) {
                for (int j = ; j < AREA_WIDTH / ; j++) {
                    switch (read.read()) {
                    case :
                        walls.add(new Wall(j * , i * ));
                        break;
                    case :
                        irons.add(new Iron(j * , i * ));
                        break;
                    case :
                        golds.add(new Gold(j * , i * ));
                        break;
                    case :
                        selfBorn = new SelfBorn(j * , i * );
                        break;
                    case :
                        enemyBorns.add(new EnemyBorn(j * , i * ));
                        break;
                    case :
                        home = new Home(j * , i * );
                        break;
                    }
                }
            }
            read.close();
        } catch (Exception e) {
        }
        ;
    }

    public void addTank() {
        if (tankMax <= )
            return;
        for (int i = allTanks.size(); i < enemyBorns.size() + ; 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() + );
            } catch (Exception e) {
                e.printStackTrace();
            }
            enemyTank.setDir(Direction.D);
            enemyTank.setDown(true);
            allTanks.add(enemyTank);
            tankMax--;
            if (tankMax <= )
                return;
        }
    }

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

    private void over() {

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

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

代碼運作效果截圖

Java版坦克大戰遊戲源碼代碼運作效果截圖