1. 程式人生 > >Java開發坦克大戰

Java開發坦克大戰

//1、第一大部分:窗體及面板類//這是主窗體類-------------------------------------------------------------------package TankFrame;import java.applet.Applet;import java.applet.AudioClip;import java.awt.Color;import java.awt.Container;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.io.File;import java.io.InputStream;import java.io.OutputStreamWriter;import java.net.MalformedURLException;import java.net.URL;import javax.sound.sampled.AudioInputStream;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JPanel;import music.PlaySounds;public class MyTankFrame extends JFrame{//選單屬性private JMenuBar jmb;//開始遊戲private JMenu jm1;private JMenuItem jmi1;private JMenuItem jmi2;private StartPanel sp;//開始面板//內容面板屬性private Container contentP;//坦克面板屬性private TankPanel tankPanel;//記錄面板屬性private RecordPanel rp;public MyTankFrame(){Toolkit tk = Toolkit.getDefaultToolkit();this.setBounds((int)(tk.getScreenSize().getWidth()-1400)/2, (int)(tk.getScreenSize().getHeight()-900)/2, 1400, 900);this.setTitle("坦克大戰1.0");this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);this.setResizable(false);this.addContent();this.setVisible(true);} public void addContent(){this.contentP = this.getContentPane();this.contentP.setLayout(null);//載入選單this.jmb = new JMenuBar();this.jm1 = new JMenu("遊戲(G)");//設定快捷方式this.jm1.setMnemonic('G');this.jmi1 = new JMenuItem("開始新遊戲(N)");this.jmi1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent arg0) {// TODO 自動生成的方法存根MyTankFrame.this.tankPanel = new TankPanel();//啟動坦克面板執行緒Thread th = new Thread(MyTankFrame.this.tankPanel);th.start();//先刪除舊的刪除面板,再顯示新坦克執行面板MyTankFrame.this.remove(MyTankFrame.this.sp);MyTankFrame.this.contentP.add(MyTankFrame.this.tankPanel);MyTankFrame.this.addKeyListener(MyTankFrame.this.tankPanel);//註冊鍵盤監聽//顯示資料記錄面板MyTankFrame.this.rp.setVisible(true);}});this.jmi2 = new JMenuItem("退出遊戲(E)");this.jmi2.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO 自動生成的方法存根System.exit(0);}});this.jm1.add(jmi1);this.jm1.add(jmi2);this.jmb.add(jm1);this.setJMenuBar(jmb);//載入開始面板this.sp = new StartPanel();Thread th1 = new Thread(this.sp);th1.start();this.sp.setBounds(0, 0, 1100, 900);this.sp.setVisible(true);this.contentP.add(this.sp);//載入音樂PlaySounds ps = new PlaySounds("music.wav");ps.start();//載入記錄資料的坦克面板this.rp = new RecordPanel();this.rp.setBackground(Color.GREEN);this.rp.setVisible(false);this.contentP.add(MyTankFrame.this.rp);}}//坦克執行介面-----------------------------------------------------------------------------------------package TankFrame;import java.awt.Color;import java.awt.Container;import java.awt.Graphics;import java.awt.Image;import java.awt.Panel;import java.awt.Toolkit;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.util.Vector;import javax.swing.JPanel;import TankBean.Bomb;import TankBean.Bullet;import TankBean.EnemyTank;import TankBean.MyHeroTank;public class TankPanel extends JPanel implements KeyListener,Runnable{private MyHeroTank heroTank;private Vector allEnemyTank = new Vector();private int enemyTankNumber = 10;private Vector allBombs = new Vector();private Image image1;private Image image2;private Image image3;public TankPanel(){this.setLayout(null);this.setBounds(0, 0, 1100, 900);this.setBackground(Color.black);//初始化我的坦克this.heroTank = new MyHeroTank(400,400);//生成我的坦克this.heroTank.setTankDirection(0);//初始化敵人坦克for(int i = 0;i < this.enemyTankNumber;i++){EnemyTank et = new EnemyTank((i+1)*100,0);et.setTankColor(1);et.setTankDirection(2);this.allEnemyTank.add(et);//將面板上的坦克集合傳給新建立的坦克et.getEnemyTank(allEnemyTank);}//初始化炸彈圖片this.image1 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_1.jpg"));this.image2 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_2.jpg"));this.image3 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_3.jpg"));this.setVisible(true);}public Vector getAllEnemyTank() {return allEnemyTank;}public void setAllEnemyTank(Vector allEnemyTank) {this.allEnemyTank = allEnemyTank;}//重寫面板的paint方法@Overridepublic void paint(Graphics g) {// TODO 自動生成的方法存根super.paint(g);//畫我的坦克if(this.heroTank.isJudgeTankIsLive()){this.drawTank(this.heroTank.getX(), this.heroTank.getY(), 0, this.heroTank.getTankDirection(), g);}//繪製我的坦克的炮彈for(int i = 0;i < this.heroTank.getAllBullets().size();i++){Bullet bullet = this.heroTank.getAllBullets().get(i);if(bullet != null && bullet.isBulletLive() == true){g.draw3DRect(bullet.getX(), bullet.getY(), 1, 1,false);}//如果判斷子彈已經死亡,那麼將其從集合中刪掉if(bullet.isBulletLive() == false){this.heroTank.getAllBullets().remove(bullet);}}//畫炸彈for(int i = 0;i < this.allBombs.size();i++){Bomb bomb = this.allBombs.get(i);if(bomb.getLifeValue() > 6){g.drawImage(this.image1, bomb.getX(), bomb.getY(), 30, 30, this);}else if(bomb.getLifeValue() > 3){g.drawImage(this.image2, bomb.getX(), bomb.getY(), 30, 30, this);}else{g.drawImage(this.image3, bomb.getX(), bomb.getY(), 30, 30, this);}//讓炸彈生命減小bomb.lifeDown();//如果生命為零,那麼刪除炸彈if(bomb.getLifeValue() == 0){this.allBombs.remove(bomb);}}//畫敵人坦克for(int i = 0;i < this.allEnemyTank.size();i++){//如果坦克或者就繪製出來if(this.allEnemyTank.get(i).isJudgeTankIsLive() == true){this.drawTank(this.allEnemyTank.get(i).getX(), this.allEnemyTank.get(i).getY(),this.allEnemyTank.get(i).getTankColor(), this.allEnemyTank.get(i).getTankDirection(), g);}}//繪製敵人的坦克的炮彈for(int i = 0;i < this.allEnemyTank.size();i++){for(int j = 0;j < this.allEnemyTank.get(i).getAllBullets().size();j++){Bullet bullet = this.allEnemyTank.get(i).getAllBullets().get(j);if(bullet != null && bullet.isBulletLive() == true){g.draw3DRect(bullet.getX(), bullet.getY(), 1, 1,false);}if(bullet.isBulletLive() == false){this.allEnemyTank.get(i).getAllBullets().remove(bullet);}}}}//畫坦克的行為public static void drawTank(int x,int y,int tankColor,int tankDirection,Graphics g){//根據顏色判斷坦克型別if(tankColor == 0){g.setColor(Color.RED);//我的坦克}else if(tankColor == 1){g.setColor(Color.CYAN);//敵人的坦克}//判斷坦克方向if(tankDirection == 0){//向上g.fill3DRect(x, y, 5, 30, false);//畫第左邊矩形g.fill3DRect(x+5, y+5, 10, 20, false);//畫中間矩形g.fill3DRect(x+15, y, 5, 30, false);//畫右邊矩形g.fillOval(x+4, y+10, 10, 10);//畫炮塔g.drawLine(x+9, y-2, x+9, y+15);//畫炮管}else if(tankDirection == 2){//向下g.fill3DRect(x, y, 5, 30, false);//畫第左邊矩形g.fill3DRect(x+5, y+5, 10, 20, false);//畫中間矩形g.fill3DRect(x+15, y, 5, 30, false);//畫右邊矩形g.fillOval(x+4, y+10, 10, 10);//畫炮塔g.drawLine(x+9, y+32, x+9, y+15);//畫炮管}else if(tankDirection == 1){//向右g.fill3DRect(x, y, 30, 5, false);//畫第上邊矩形g.fill3DRect(x+5, y+5, 20, 10, false);//畫中間矩形g.fill3DRect(x, y+15, 30, 5, false);//畫下邊矩形g.fillOval(x+10, y+4, 10, 10);//畫炮塔g.drawLine(x+15, y+9, x+32, y+9);//畫炮管}else if(tankDirection == 3){//向左g.fill3DRect(x, y, 30, 5, false);//畫第上邊矩形g.fill3DRect(x+5, y+5, 20, 10, false);//畫中間矩形g.fill3DRect(x, y+15, 30, 5, false);//畫下邊矩形g.fillOval(x+10, y+4, 10, 10);//畫炮塔g.drawLine(x+15, y+9, x-2, y+9);//畫炮管}}//判斷我的坦克發射的子彈是否觸碰到敵人的坦克public void judgeIsHit(EnemyTank et,Bullet bullet){switch(et.getTankDirection()){case 0:case 2:if(bullet.getX() > et.getX() && bullet.getX() < et.getX()+20 && bullet.getY() > et.getY() && bullet.getY() < et.getY()+30){//擊中向上向下的坦克bullet.setBulletLive(false);//子彈死亡et.setJudgeTankIsLive(false);//被擊中則坦克死亡//更新敵人坦克數量減1RecordPanel.setEnemyLife(RecordPanel.getEnemyLife() - 1);//更新我的戰績加1RecordPanel.setDestroyEnemy(RecordPanel.getDestroyEnemy() + 1);//建立一個炸彈Bomb bomb = new Bomb(et.getX(),et.getY());this.allBombs.add(bomb);}break;case 1:case 3:if(bullet.getX() > et.getX() && bullet.getX() < et.getX()+30 && bullet.getY() > et.getY() && bullet.getY() < et.getY()+20){//擊中向左向右的坦克bullet.setBulletLive(false);et.setJudgeTankIsLive(false);//被擊中則坦克死亡//更新敵人坦克數量減1RecordPanel.setEnemyLife(RecordPanel.getEnemyLife() - 1);//更新我的戰績加1RecordPanel.setDestroyEnemy(RecordPanel.getDestroyEnemy() + 1);//建立一個炸彈Bomb bomb = new Bomb(et.getX(),et.getY());this.allBombs.add(bomb);}break;}}//判斷敵人坦克發射的子彈是否觸碰到我的坦克public void judgeEnemyHitHero(MyHeroTank mht,Bullet bullet){//先得到每一輛敵人坦克for(int i = 0;i < this.allEnemyTank.size();i++){for(int j = 0;j < this.allEnemyTank.get(i).getAllBullets().size();j++){bullet = this.allEnemyTank.get(i).getAllBullets().get(j);switch(mht.getTankDirection()){case 0:case 2:if(bullet.getX() > mht.getX() && bullet.getX() < mht.getX()+20 && bullet.getY() > mht.getY() && bullet.getY() < mht.getY()+30){bullet.setBulletLive(false);//子彈死亡mht.setJudgeTankIsLive(false);//我的坦克死亡//更新記錄面板上我的坦克數量RecordPanel.setMyLife(RecordPanel.getMyLife() - 1);Bomb bomb = new Bomb(mht.getX(),mht.getY());this.allBombs.add(bomb);}break;case 1:case 3:if(bullet.getX() > mht.getX() && bullet.getX() < mht.getX()+30 && bullet.getY() > mht.getY() && bullet.getY() < mht.getY()+20){bullet.setBulletLive(false);//子彈死亡mht.setJudgeTankIsLive(false);//我的坦克死亡//更新記錄面板上我的坦克數量RecordPanel.setMyLife(RecordPanel.getMyLife() - 1);Bomb bomb = new Bomb(mht.getX(),mht.getY());this.allBombs.add(bomb);}break;}}}}@Overridepublic void keyPressed(KeyEvent e) {// TODO 自動生成的方法存根if(e.getKeyCode() == KeyEvent.VK_UP){this.heroTank.setTankDirection(0);this.heroTank.moveUp();}else if(e.getKeyCode() == KeyEvent.VK_DOWN){this.heroTank.setTankDirection(2);this.heroTank.moveDown();}else if(e.getKeyCode() == KeyEvent.VK_LEFT){this.heroTank.setTankDirection(3);this.heroTank.moveLeft();}else if(e.getKeyCode() == KeyEvent.VK_RIGHT){this.heroTank.setTankDirection(1);this.heroTank.moveRight();}//判斷玩家是否按下開始鍵和暫停鍵if(e.getKeyCode() == KeyEvent.VK_P){this.heroTank.setTankSpeed(0);for(int k = 0;k < this.heroTank.getAllBullets().size();k++){this.heroTank.getAllBullets().get(k).setBulletSpeed(0);}for(int i = 0;i < this.allEnemyTank.size();i++){EnemyTank et = this.allEnemyTank.get(i);et.setTankSpeed(0);for(int j = 0;j < et.getAllBullets().size();j++){et.getAllBullets().get(j).setBulletSpeed(0);}}}if(e.getKeyCode() == KeyEvent.VK_S){this.heroTank.setTankSpeed(5);for(int k = 0;k < this.heroTank.getAllBullets().size();k++){this.heroTank.getAllBullets().get(k).setBulletSpeed(6);}for(int i = 0;i < this.allEnemyTank.size();i++){EnemyTank et = this.allEnemyTank.get(i);et.setTankSpeed(5);for(int j = 0;j < et.getAllBullets().size();j++){et.getAllBullets().get(j).setBulletSpeed(6);}}}//判斷玩家是否按下開火鍵,如果按下就呼叫坦克的開火行為,同時建立炮彈if(e.getKeyCode() == KeyEvent.VK_F){if(this.heroTank.getAllBullets().size() < 5){this.heroTank.shootEnemy();}}this.repaint();}@Overridepublic void keyReleased(KeyEvent e) {// TODO 自動生成的方法存根}@Overridepublic void keyTyped(KeyEvent e) {// TODO 自動生成的方法存根}@Overridepublic void run() {// TODO 自動生成的方法存根//讓面板每隔100毫秒重繪一次while(true){try {Thread.sleep(100);} catch (InterruptedException e) {// TODO 自動生成的 catch 塊e.printStackTrace();}//判斷我的坦克是否擊中敵人坦克for(int i = 0;i < this.heroTank.getAllBullets().size();i++){//取出子彈Bullet bullet = this.heroTank.getAllBullets().get(i);//判斷子彈是否存活if(bullet.isBulletLive()){//取出敵人每一輛坦克與這顆子彈進行判斷for(int j = 0;j < this.allEnemyTank.size();j++){EnemyTank et = this.allEnemyTank.get(j);if(et.isJudgeTankIsLive()){this.judgeIsHit(et, bullet);}}}}//判斷敵人坦克是否擊中我的坦克//先取出每一個敵人的子彈for(int i = 0;i < this.allEnemyTank.size();i++){for(int j = 0;j < this.allEnemyTank.get(i).getAllBullets().size();j++){Bullet bullet = this.allEnemyTank.get(i).getAllBullets().get(j);//判斷敵人的子彈是否存活或者是否擊中我的英雄if(bullet.isBulletLive()){if(this.heroTank.isJudgeTankIsLive()){this.judgeEnemyHitHero(this.heroTank, bullet);}}}}this.repaint();}}}//資料記錄面板---------------------------------------------------------------------------package TankFrame;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import javax.swing.JPanel;public class RecordPanel extends JPanel implements Runnable{//用來記錄遊戲資料private static int myLife = 3;//我的坦克數量private static int enemyLife = 10;//敵人坦克數量private static int destroyEnemy = 0;//我摧毀的坦克數量public RecordPanel(){this.setBounds(1100, 0, 300, 900);this.setVisible(true);Thread th = new Thread(this);th.start();}public static int getMyLife() {return myLife;}public static void setMyLife(int myLife) {RecordPanel.myLife = myLife;}public static int getEnemyLife() {return enemyLife;}public static void setEnemyLife(int enemyLife) {RecordPanel.enemyLife = enemyLife;}public static int getDestroyEnemy() {return destroyEnemy;}public static void setDestroyEnemy(int destroyEnemy) {RecordPanel.destroyEnemy = destroyEnemy;}@Overridepublic void paint(Graphics g) {// TODO 自動生成的方法存根super.paint(g);//呼叫坦克面板上畫坦克的方法TankPanel.drawTank(10, 70, 0, 0, g);TankPanel.drawTank(10, 170, 1, 0, g);TankPanel.drawTank(10, 270, 1, 0, g);g.setFont(new Font("微軟雅黑",Font.BOLD,30));g.setColor(Color.BLACK);//我的坦克數量g.drawString("MyLife:" + this.getMyLife(), 50, 95);//敵人坦克數量g.drawString("EnemyTank:" + this.getEnemyLife(), 50, 195);//戰績g.drawString("Score:" + this.getDestroyEnemy(), 50, 295);}@Overridepublic void run() {// TODO 自動生成的方法存根while(true){try {Thread.sleep(200);} catch (InterruptedException e) {// TODO 自動生成的 catch 塊e.printStackTrace();}this.repaint();}}}//開始遊戲面板----------------------------------------------------------------------package TankFrame;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.io.IOException;import java.nio.CharBuffer;import javax.swing.JPanel;public class StartPanel extends JPanel implements Runnable{private int times = 0;@Overridepublic void paint(Graphics g) {// TODO 自動生成的方法存根super.paint(g);g.fillRect(0, 0, 1100, 900);if(this.times % 2 == 0){g.setColor(Color.BLUE);g.setFont(new Font("微軟雅黑",Font.BOLD,80));//提示資訊g.drawString("Stage:1", 400, 450);}else{g.setColor(Color.YELLOW);g.setFont(new Font("微軟雅黑",Font.BOLD,80));//提示資訊g.drawString("Stage:1", 400, 450);}}@Overridepublic void run() {// TODO 自動生成的方法存根while(true){try {Thread.sleep(300);} catch (InterruptedException e) {// TODO 自動生成的 catch 塊e.printStackTrace();}this.times++;this.repaint();}}}//2、第二部分,物件類--------------------------------------------------------------//炸彈類,製造坦克被擊毀時的爆炸效果--------------------------------------------------------------package TankBean;public class Bomb {private int x;private int y;private boolean isLive = true;private int lifeValue = 9;public Bomb(int x,int y){this.x = x;this.y = y;}public void lifeDown(){if(this.lifeValue > 0){this.lifeValue--;}else{this.isLive = false;}}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public boolean isLive() {return isLive;}public void setLive(boolean isLive) {this.isLive = isLive;}public int getLifeValue() {return lifeValue;}public void setLifeValue(int lifeValue) {this.lifeValue = lifeValue;}}//子彈類-------------------------------------------------------package TankBean;public class Bullet implements Runnable{private int x;private int y;private int bulletSpeed = 6;private int bulletDirection;private boolean isBulletLive = true;public Bullet(int x,int y,int bulletDirection){this.x = x;this.y = y;this.bulletDirection = bulletDirection;}public void run(){while(true){try {Thread.sleep(60);} catch (InterruptedException e) {// TODO 自動生成的 catch 塊e.printStackTrace();}switch(this.bulletDirection){case 0://子彈向上this.setY(this.getY()-this.bulletSpeed);break;case 1://子彈向右this.setX(this.getX()+this.bulletSpeed);break;case 2://子彈向下this.setY(this.getY()+this.bulletSpeed);break;case 3://子彈向左this.setX(this.getX()-this.bulletSpeed);break;}//如果子彈超過邊界就消失,或者碰到敵人也要消失if(this.getX() < 0 || this.getX() > 1100 || this.getY() < -1 || this.getY() > 900){this.setBulletLive(false);break;}}}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public boolean isBulletLive() {return isBulletLive;}public void setBulletLive(boolean isBulletLive) {this.isBulletLive = isBulletLive;}public int getBulletSpeed() {return bulletSpeed;}public void setBulletSpeed(int bulletSpeed) {this.bulletSpeed = bulletSpeed;}public int getBulletDirection() {return bulletDirection;}public void setBulletDirection(int bulletDirection) {this.bulletDirection = bulletDirection;}}//敵人坦克類-------------------------------------------------------package TankBean;import java.util.Vector;public class EnemyTank extends TankFather implements Runnable{private Vector allBullets = new Vector();private Bullet bullet;//定義一個集合得到panel上的所有敵人坦克private Vector allEnemyTank = new Vector();public EnemyTank(int x, int y) {super(x, y);Thread th = new Thread(this);th.start();}public Vector getAllBullets() {return allBullets;}public void setAllBullets(Vector allBullets) {this.allBullets = allBullets;}public Bullet getBullet() {return bullet;}public void setBullet(Bullet bullet) {this.bullet = bullet;}//從tankPanel上得到已有敵人坦克public void getEnemyTank(Vector allEnemyTank){this.allEnemyTank = allEnemyTank;}//判斷自己是否和其他坦克碰撞到public boolean isTouchOtherTank(){boolean b = false;switch(this.getTankDirection()){case 0://我的坦克向上,取出所有的敵人坦克for(int i = 0;i < this.allEnemyTank.size();i++){//取出第一個坦克EnemyTank et = this.allEnemyTank.get(i);//排除自己if(et != this){//敵人的坦克向上或者向下if(et.getTankDirection() == 0 || et.getTankDirection() == 2){//左點if(this.getX() >= et.getX() && this.getX() <= et.getX() + 20 && this.getY() >= et.getY() && this.getY() <= et.getY() + 30){return true;}//右點if(this.getX() + 20 >= et.getX() && this.getX() + 20 <= et.getX() + 20 && this.getY() >= et.getY() && this.getY() <= et.getY() + 30){return true;}}//敵人的坦克向左或者向右if(et.getTankDirection() == 1 || et.getTankDirection() == 3){//左點if(this.getX() >= et.getX() && this.getX() <= et.getX() + 30 && this.getY() >= et.getY() && this.getY() <= et.getY() + 20){return true;}//右點if(this.getX() + 20 >= et.getX() && this.getX() + 20 <= et.getX() + 30 && this.getY() >= et.getY() && this.getY() <= et.getY() + 20){return true;}}}}break;case 1://我的坦克向右,取出所有的敵人坦克for(int i = 0;i < this.allEnemyTank.size();i++){//取出第一個坦克EnemyTank et = this.allEnemyTank.get(i);//排除自己if(et != this){//敵人的坦克向上或者向下if(et.getTankDirection() == 0 || et.getTankDirection() == 2){//上點if(this.getX() + 30 >= et.getX() && this.getX() + 30 <= et.getX() + 20 && this.getY() >= et.getY() && this.getY() <= et.getY() + 30){return true;}//下點if(this.getX() + 30 >= et.getX() && this.getX() + 30 <= et.getX() + 20 && this.getY() + 20 >= et.getY() && this.getY() + 20 <= et.getY() + 30){return true;}}//敵人的坦克向左或者向右if(et.getTankDirection() == 1 || et.getTankDirection() == 3){//上點if(this.getX() + 30 >= et.getX() && this.getX() + 30 <= et.getX() + 30 && this.getY() >= et.getY() && this.getY() <= et.getY() + 20){return true;}//下點if(this.getX() + 30 >= et.getX() && this.getX() + 30 <= et.getX() + 30 && this.getY() + 20 >= et.getY() && this.getY() + 20 <= et.getY() + 20){return true;}}}}break;case 2://我的坦克向下,取出所有的敵人坦克for(int i = 0;i < this.allEnemyTank.size();i++){//取出第一個坦克EnemyTank et = this.allEnemyTank.get(i);//排除自己if(et != this){//敵人的坦克向上或者向下if(et.getTankDirection() == 0 || et.getTankDirection() == 2){//左點if(this.getX() >= et.getX() && this.getX() <= et.getX() + 20 && this.getY() + 30 >= et.getY() && this.getY() + 30 <= et.getY() + 30){return true;}//右點if(this.getX() + 20 >= et.getX() && this.getX() + 20 <= et.getX() + 20 && this.getY() + 30 >= et.getY() && this.getY() + 30 <= et.getY() + 30){return true;}}//敵人的坦克向左或者向右if(et.getTankDirection() == 1 || et.getTankDirection() == 3){//左點if(this.getX() >= et.getX() && this.getX() <= et.getX() + 30 && this.getY() + 30 >= et.getY() && this.getY() + 30 <= et.getY() + 20){return true;}//右點if(this.getX() + 20 >= et.getX() && this.getX() + 20 <= et.getX() + 30 && this.getY() + 30 >= et.getY() && this.getY() + 30 <= et.getY() + 20){return true;}}}}break;case 3://我的坦克向左,取出所有的敵人坦克for(int i = 0;i < this.allEnemyTank.size();i++){//取出第一個坦克EnemyTank et = this.allEnemyTank.get(i);//排除自己if(et != this){//敵人的坦克向上或者向下if(et.getTankDirection() == 0 || et.getTankDirection() == 2){//上點if(this.getX() >= et.getX() && this.getX() <= et.getX() + 20 && this.getY() >= et.getY() && this.getY() <= et.getY() + 30){return true;}//下點if(this.getX() >= et.getX() && this.getX() <= et.getX() + 20 && this.getY() + 20 >= et.getY() && this.getY() + 20 <= et.getY() + 30){return true;}}//敵人的坦克向左或者向右if(et.getTankDirection() == 1 || et.getTankDirection() == 3){//上點if(this.getX() >= et.getX() && this.getX() <= et.getX() + 30 && this.getY() >= et.getY() && this.getY() <= et.getY() + 20){return true;}//下點if(this.getX() >= et.getX() && this.getX() <= et.getX() + 30 && this.getY() + 20 >= et.getY() && this.getY() + 20 <= et.getY() + 20){return true;}}}}break;}return false;}@Overridepublic void run() {// TODO 自動生成的方法存根while(true){switch(this.getTankDirection()){case 0:for(int i = 0;i < 30;i++){if(this.getY() > 0 && !this.isTouchOtherTank()){this.setY(this.getY()-this.getTankSpeed());if(this.allBullets.size() < 1){this.shootHero();}try {Thread.sleep(200);} catch (InterruptedException e) {// TODO 自動生成的 catch 塊e.printStackTrace();}}}break;case 1:for(int i = 0;i < 30;i++){if(this.getX() < 1070 && !this.isTouchOtherTank()){this.setX(this.getX()+this.getTankSpeed());if(this.allBullets.size() < 1){this.shootHero();}try {Thread.sleep(200);} catch (InterruptedException e) {// TODO 自動生成的 catch 塊e.printStackTrace();}}}break;case 2:for(int i = 0;i < 30;i++){if(this.getY() < 830 && !this.isTouchOtherTank()){this.setY(this.getY()+this.getTankSpeed());if(this.allBullets.size() < 1){this.shootHero();}try {Thread.sleep(200);} catch (InterruptedException e) {// TODO 自動生成的 catch 塊e.printStackTrace();}}}break;case 3:for(int i = 0;i < 30;i++){if(this.getX() > 0 && !this.isTouchOtherTank()){this.setX(this.getX()-this.getTankSpeed());if(this.allBullets.size() < 1){this.shootHero();}try {Thread.sleep(200);} catch (InterruptedException e) {// TODO 自動生成的 catch 塊e.printStackTrace();}}}break;}//隨機產生方向int tankDirection = (int)(Math.random()*4);this.setTankDirection(tankDirection);//如果死亡退出執行緒if(this.isJudgeTankIsLive() == false){break;}}}//敵人坦克有攻擊我的坦克的行為public void shootHero(){switch(this.getTankDirection()){case 0:this.bullet = new Bullet(this.getX()+8,this.getY()-2,0);this.allBullets.add(this.bullet);break;case 1:this.bullet = new Bullet(this.getX()+32,this.getY()+9,1);this.allBullets.add(this.bullet);break;case 2:this.bullet = new Bullet(this.getX()+8,this.getY()+32,2);this.allBullets.add(this.bullet);break;case 3:this.bullet = new Bullet(this.getX()-2,this.getY()+8,3);this.allBullets.add(this.bullet);break;}//啟動子彈執行緒Thread th = new Thread(this.bullet);th.start();}}//我的坦克類-------------------------------------------------------package TankBean;import java.util.Vector;public class MyHeroTank extends TankFather{private Vector allBullets = new Vector();;private Bullet bullet;public MyHeroTank(int x,int y){super(x,y);}public Vector getAllBullets() {return allBullets;}public void setAllBullets(Vector allBullets) {this.allBullets = allBullets;}public Bullet getBullet() {return bullet;}public void setBullet(Bullet bullet) {this.bullet = bullet;}//坦克有移動的行為public void moveUp(){if(this.getY() > 0){this.setY(this.getY() - this.getTankSpeed());}}public void moveRight(){if(this.getX() < 1070){this.setX(this.getX() + this.getTankSpeed());}}public void moveDown(){if(this.getY() < 830){this.setY(this.getY() + this.getTankSpeed());}}public void moveLeft(){if(this.getX() > 0){this.setX(this.getX() - this.getTankSpeed());}}//我的坦克有攻擊敵人的行為public void shootEnemy(){switch(this.getTankDirection()){case 0:this.bullet = new Bullet(this.getX()+8,this.getY()-2,0);this.allBullets.add(this.bullet);break;case 1:this.bullet = new Bullet(this.getX()+32,this.getY()+9,1);this.allBullets.add(this.bullet);break;case 2:this.bullet = new Bullet(this.getX()+8,this.getY()+32,2);this.allBullets.add(this.bullet);break;case 3:this.bullet = new Bullet(this.getX()-2,this.getY()+8,3);this.allBullets.add(this.bullet);break;}//啟動子彈執行緒Thread th = new Thread(this.bullet);th.start();}}//坦克父類-------------------------------------------------------package TankBean;public class TankFather {private int x;//坦克位置的橫座標private int y;//坦克位置的縱座標// private int tankType;//0代表我的,1代表敵人的坦克private int tankDirection = 0;//0代表上,1代表右,2代表下,3代表左private int tankColor;//坦克顏色private int tankSpeed = 5;private boolean judgeTankIsLive = true;//帶參構造public TankFather(int x, int y) {super();this.x = x;this.y = y;}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public int getTankDirection() {return tankDirection;}public void setTankDirection(int tankDirection) {this.tankDirection = tankDirection;}public int getTankSpeed() {return tankSpeed;}public void setTankSpeed(int tankSpeed) {this.tankSpeed = tankSpeed;}public int getTankColor() {return tankColor;}public void setTankColor(int tankColor) {this.tankColor = tankColor;}public boolean isJudgeTankIsLive() {return judgeTankIsLive;}public void setJudgeTankIsLive(boolean judgeTankIsLive) {this.judgeTankIsLive = judgeTankIsLive;}}//第三部分//測試類-------------------------------------------------------package TestTank;import TankFrame.MyTankFrame;public class TestMyTankGame {public static void main(String[] args) {// TODO 自動生成的方法存根new MyTankFrame();}}//第四部分//音樂匯入類-------------------------------------------------------package music;import java.io.File;import java.io.IOException;import javax.sound.sampled.AudioFormat;import javax.sound.sampled.AudioInputStream;import javax.sound.sampled.AudioSystem;import javax.sound.sampled.DataLine;import javax.sound.sampled.SourceDataLine;//播放聲音的類public class PlaySounds extends Thread { private String filename; public PlaySounds(String wavfile) {filename = wavfile;}public void run() {File soundFile = new File(filename);AudioInputStream audioInputStream = null;try {audioInputStream = AudioSystem.getAudioInputStream(soundFile);} catch (Exception e1) {e1.printStackTrace();return;}AudioFormat format = audioInputStream.getFormat();SourceDataLine auline = null;DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);try {auline = (SourceDataLine) AudioSystem.getLine(info);auline.open(format);} catch (Exception e) {e.printStackTrace();return;}auline.start();int nBytesRead = 0;//這是緩衝byte[] abData = new byte[512];try {while (nBytesRead != -1) {nBytesRead = audioInputStream.read(abData, 0, abData.length);if (nBytesRead >= 0)auline.write(abData, 0, nBytesRead);}} catch (IOException e) {e.printStackTrace();return;} finally {auline.drain();auline.close();} } }