坦克大戰DEMO

WBOY
發布: 2016-07-25 09:08:16
原創
1660 人瀏覽過
實現了基本功能....地方坦克尋路做得比較弱智,每個坦克最多能同時發射5發砲彈....敵方有三種兵種,菜單欄沒做響應,這個很簡單,需要的可以自己加......上傳全部程式碼,僅供學習參考...... 坦克大戰DEMO
  1. package tank.common;
  2. abstract public class Bullet implements Runnable, Common {
  3. private int x, y;
  4. private int; > private int direction;
  5. private boolean alive;
  6. protected int power;
  7. public Bullet(int x, int y, int speed, int direction) {
  8. this.x = x;
  9. this.y = y;
  10. this.speed = speed;
  11. this.direction = direction;
  12. alive = true;
  13. }
  14. public void true;
  15. }
  16. alive = false;
  17. }
  18. public int getPower() {
  19. return power;
  20. }
  21. public int getX() {
  22. return x ;
  23. }
  24. public int getY() {
  25. return y;
  26. }
  27. public boolean isAlive() {
  28. return alive;
  29. }
  30. public void run() {
  31. while (true) {
  32. try {
  33. Thread.sleep(15);
  34. } catch (InterruptedException e) {
  35. // TO Auto-generated catch block
  36. e.printStackTrace();
  37. }
  38. switch (direction) {
  39. case UP:
  40. y -= speed;
  41. break;
  42. case DO🎜> case WN :
  43. y += speed;
  44. break;
  45. case RIGHT:
  46. x += speed;
  47. break;
  48. case LEFT:
  49. x -= speed;
  50. break;
  51. }
  52. if (x WIDTH || y > HEIGHT || y alive = false;
  53. return;
  54. }
  55. }
  56. }
  57. }
複製程式碼
  1. package tank.common;
  2. public interface Common {
  3. public static final int UP = 0;
  4. public static final int public static final int RIGHT = 2;
  5. public static final int LEFT = 3;
  6. public static final int WIDTH = 600;
  7. public static final int intIGHT = 400 int HEIGHT WATER = 0;
  8. public static final int WALLS = 1;
  9. public static final int STEELS = 2;
  10. public static final int GRASS = 3;
  11. public static final int GRASS = 3;
  12. }
複製程式碼
  1. 套件tank.common;
  2. 導入java.awt.Color;
  3. 導入java.awt.Point;
  4. 公有抽象類別EnemyTank extends Tank {
  5. private class AutoFire Implements Runnable {
  6. @Override
  7. public void run() {
  8. // TODO 自動產生的方法存根
  9. while try {
  10. Thread.sleep(500);
  11. } catch (InterruptedException e) {
  12. // TODO 自動產生的catch 區塊
  13. e.printStackTrace();
  14. }
  15. if (Math.random() > shotKey) {
  16. shot();
  17. }
  18. }
  19. }
  20. }
  21. }
  22. }
  23. 私有類別{
  24. @Override
  25. public void run() {
  26. // TODO 自動產生的方法存根
  27. int move[] = new int[4]; while (isAlive()) {
  28. try {
  29. Thread.sleep(110);
  30. } catch (InterruptedException e) {
  31. // TODO 自動產生的catch 區塊
  32. e.printStackTrace( ) ;
  33. }
  34. for (int i = 0; i Moves[i] = JudgeHero(i);
  35. }
  36. int Direction = 0 ;
  37. int max = Integer.MIN_VALUE;
  38. for (int i = 0;我 if (moves[i] >= max) {
  39. max = Moves[i ];
  40. 方向= i;
  41. }
  42. }
  43. move(方向);
  44. }
  45. }
  46. }
  47. 私人射擊鍵;
  48. 私人點英雄位置;
  49. 私人坦克英雄;
  50. public EnemyTank(int x, int y, 坦克英雄, int life, int ID) {
  51. super(x, y, Color.cyan);
  52. this.lifes = life;
  53. this .hero = 英雄;
  54. super.setImageID(ID);
  55. }
  56. private int JudgeHero(int Direction) {
  57. HeroPosition = new Point(hero.getX() + 9, 英雄.getY() + 9);
  58. int result = 0;
  59. int x = this.getX( );
  60. int y = this.getY();
  61. int speed = this.getSpeed( );
  62. 雙距離1 = Math.abs((this.getX() - HeroPosition.x)
  63. * (this.getX() - HeroPosition.x)
  64. + (this.getY() - HeroPosition .y)
  65. * (this.getY() - HeroPosition.y));
  66. 開關(方向){
  67. case UP:
  68. y -= speed;
  69. 中斷;
  70. 案例向下:
  71. y += 速度;
  72. 中斷;
  73. 案例右側:
  74. x += 速度;
  75. 中斷;
  76. 案例向左:
  77. x -= 速度;
  78. 中斷;
  79. }
  80. if (getDirection() == 方向) {
  81. 結果+= 5000;
  82. }
  83. if (!canMove(x, y)) {
  84. result -= Integer.MAX_VALUE;
  85. }
  86. 雙倍距離2 = Math .abs((x - HeroPosition.x) * (x - HeroPosition.x)
  87. + (y - HeroPosition.y) * (y - HeroPosition.y));
  88. if (Math.random() > 0.8) {
  89. 結果+= Math.random() * 20000;
  90. }
  91. 結果+= (距離1 - 距離2) * 10;
  92. 回傳結果;
  93. }
  94. public void setPosition(int x, int y) {
  95. super.x = x;
  96. super.y = y;
  97. }
  98. public void setShotSpeed(double Speed(doublele Speed) {
  99. this.shotKey = shotSpeed;
  100. }
  101. public void startFire() {
  102. new Thread(new AutoFire()).start();
  103. }
  104. public void startMove() {
  105. new Thread(new AutoMove()).start();
  106. }
}
複製程式碼複製程式碼複製程式碼複製程式碼複製程式碼
  1. 包tank.common;
  2. 導入java.awt.Color;
  3. 導入java.util.ArrayList;
  4. 導入tank. .NormalBullet;
  5. 公共抽象類別Tank 實作Common {
  6. protected int x;
  7. protected int y;
  8. private Color color;
  9. private int 方向;
  10. private ArrayList;子彈;
  11. private ArrayList; ;
  12. private int maxBulletNum;
  13. private boolean alive;
  14. protected int life;
  15. ArList
  16. {
  17. 速度= 2;
  18. 方向=上;
  19. 活= true;
  20. }
  21. public Tank() {
  22. this
  23. public Tank() {
  24. this
  25. public Tank() {
  26. this
  27. this .x = 0;
  28. this.y = 0;
  29. color = Color.black;
  30. }
  31. public Tank(int x, int y , 顏色顏色) {
  32. this .x = x;
  33. this.y = y;
  34. this.color = color;
  35. maxBulletNum = 5;
  36. 項目符號= new ArrayList( );
  37. wall = new ArrayList();
  38. }
  39. protected boolean canMove(int x, int y) {
  40. if (x )寬度- 20 || y 高度- 20) {
  41. 回傳false;
  42. }
  43. if (坦克== null) {
  44. 回傳true;
  45. }
  46. if (tanks.size () == 1) {
  47. return true;
  48. }
  49. for (int i = 0; i 牆壁tempWall = wall .get(i);
  50. if (tempWall.isAlive()) {
  51. if (x >= tempWall.getX() && y > ;= tempWall.getY()) {
  52. if (x return tempWall.canBeWalk();
  53. }
  54. } else if (x >= tempWall.getX() && y if (x && (y + 20) >= tempWall.getY()) {
  55. return tempWall. canBeWalk();
  56. }
  57. } else if (x ; = tempWall.getY()) {
  58. if ((x + 20) >= tempWall.getX ()
  59. && y 回傳tempWall.canBeWalk ();
  60. }
  61. } else if (x if ((x + 20) >= tempWall. getX()
  62. && (y + 20) >= tempWall.getY()) {
  63. return tempWall.canBeWalk ();
  64. }
  65. }
  66. }
  67. }
  68. for (int i = 0;我 Tank tempTank =坦克.get(i);
  69. if (tempTank == this)
  70. break;
  71. if (tempTank.isAlive()) {
  72. if ( x >= tempTank.getX() && y >= tempTank.getY()) {
  73. if (x return false;
  74. }
  75. } else if (x >= tempTank.getX() && y if (x && (y + 20) >= tempTank .getY()) {
  76. return false;
  77. }
  78. } else if (x = tempTank.getY()) {
  79. if ((x + 20) >= tempTank.getX()
  80. && y return false;
  81. }
  82. } else if (x if ((x + 20) >= tempTank. getX()
  83. && (y + 20) >= tempTank.getY()) {
  84. 回傳false;
  85. }
  86. }
  87. }
  88. }
  89. 回傳true;
  90. }
  91. public void Damage(int power) {
  92. lifes -= power;
  93. if (lifes live = false;
  94. }
  95. }
  96. public ArrayList; getBullet() {
  97. 回傳項目符號;
  98. }
  99. public Color getColor() {
  100. 回傳顏色;
  101. }
  102. public int getDirection() {
  103. 返回方向🎜> }
  104. public int getImageID() {
  105. return TankImageID;
  106. }
  107. public int getSpeed() {
  108. 回傳速度;
  109. }
  110. Wall>; getWalls() {
  111. return this.walls;
  112. }
  113. public int getX() {
  114. return x;
  115. }
  116. public int getY () {
  117. return y;
  118. }
  119. public boolean isAlive() {
  120. 回傳活;
  121. }
  122. public void move(int Direction) {
  123. setDirection (方向);
  124. int x = this.x;
  125. int y = this.y;
  126. switch (方向) {
  127. case UP:
  128. y -= speed;
  129. 中斷;
  130. 案例向下:
  131. y += 速度;
  132. 中斷;
  133. 案例右:
  134. x += 速度;
  135. 中斷;
  136. 案例向左:
  137. x - = 速度;
  138. 中斷;
  139. }
  140. if (canMove(x, y)) {
  141. this.x = x;
  142. this.y = y;
  143. }
  144. }
  145. public void setAllTanks(ArrayList坦克) {
  146. this.tanks =坦克;
  147. }
  148. public void setDirection(int Direction) {
  149. this.方向= 方向;
  150. }
  151. Final protected void setImageID(int ID) {
  152. this.tankImageID = ID;
  153. }
  154. public void setSpeed(int speed) {
  155. public void setSpeed(int speed) {
  156. this.speed ;
  157. }
  158. public void setWalls(ArrayList wallList) {
  159. this.walls = wallList;
  160. }
  161. public voidshot() {
  162. switch (方向) {
  163. case UP:
  164. bullet = new NormalBullet(x + 10, y, UP);
  165. break;
  166. case DOWN:
  167. bullet = new NormalBullet(x + 1000 ) ) , y + 20, 迭代);
  168. 中斷;
  169. 案例右:
  170. 項目符號 = new NormalBullet(x + 20, y + 10, 右);
  171. 中斷;
  172. 案例左:
  173. Bullet = new NormalBullet(x, y + 10, LEFT);
  174. break;
  175. }
  176. for (int i = 0; i Bullet temp =Bullets.get(i);
  177. if (!temp.isAlive()) {
  178. Bullets.remove(temp);
  179. }
  180. }
  181. if (temp);
  182. }
  183. }
  184. if (temp);
  185. }
  186. }
  187. if (temp);
  188. }
}
if (temp); } }
if (temp);
}
}
    if (temp);
  1. }
  2. }
  3. if (temp);
  4. }
  5. }
  6. if(子彈. size() >= maxBulletNum) {
  7. } else {
  8. new Thread(bullet).start();
  9. bullets.add(bullet);
  10. }
  11. }
  12. }
  13. }
  14. 複製程式碼
  15. package Tank.common;
  16. 公共抽象類別 Wall {
  17. private int x, y;
  18. private int wallImageID;
  19. 火災布林值canWalk;
  20. 火災布林值canFly;
  21. 火災布林值alive;
  22. 火災布林值canHit;
  23. 火災布林值(Hit;
  24. this.x = x;
  25. this.y = y;
  26. this.wallImageID = ID;
  27. this .canWalk = walk ;
  28. this.canFly = 飛;
  29. this.alive = true;
  30. this.canHit = Hit;
  31. }
  32. public boolean canBeFly() {
  33. public boolean canBeFly() {
  34. 返回canFly;
  35. }
  36. public boolean canBeHit() {
  37. return this.canHit;
}
public boolean canBeWalk() { return can🎜> public boolean canBeWalk() {
return can🎜>
public void die() {
    alive = false;
  1. }
  2. public int getImageID() {
  3. return wallImageID
  4. }
  5. > public int getX() {
  6. return x;
  7. }
  8. public int getY() {
  9. return y;
  10. }
  11. public boolean isAlive() {
  12. 活回;
  13. }
  14. }
  15. 複製程式碼
  16. package Tank.entity;
  17. public class Bomb {
  18. private int x;
  19. private int y;
  20. private boolean alive;
  21. public Bomb(int x, int y) {
  22. this.x = x;
  23. this.y = y;
  24. alive = true;
  25. }
public void decrese() {
if (life > 0) { life--; } else {
alive = false ;
}
}
  1. public int getLife() {
  2. return life;
  3. }
  4. public int getX() {
  5. return x; > }
  6. public int getY() {
  7. return y;
  8. }
  9. public boolean isAlive() {
  10. 回傳活;
  11. }
複製程式碼
  1. 包tank.entity;
  2. 導入tank.common.EnemyTank;
  3. 導入tank.common.Tank;
  4. 公共擴充類EnemyTank {
  5. public EnemyTank1(int x, int y, 坦克英雄) {
  6. super(x, y, Hero, 3, 1);
  7. setSpeed(2);
  8. setShotSpeed( 0.8);
  9. }
}
複製程式碼
  1. 包tank.entity;
  2. 導入tank.common.EnemyTank;
  3. 導入tank.common.Tank;
  4. 公共擴充類EnemyTank {
  5. public EnemyTank2(int x, int y, 坦克英雄) {
  6. super(x, y, Hero, 5, 2);
  7. setSpeed(3);
  8. setShotSpeed(0.5);
  9. }
}
複製程式碼 包tank.entity;導入tank.common.EnemyTank;導入tank.common.Tank;公共擴充類EnemyTank { public EnemyTank3(int x, int y, 坦克英雄) { super(x, y, Hero, 10, 3); setSpeed(5); setShotSpeed(0.2); }}複製程式碼
  1. 包tank.entity;
  2. 導入tank.common.Wall;
  3. public class Grass extends Wall {
  4. public Grass(int x , int y) {
  5. super(x, y, 3, true, true, false);
  6. }
  7. }
複製程式碼
複製程式碼
  1. 複製程式碼
  2. 複製程式碼
  3. >
  4. 包tank.entity;
  5. 導入java.awt.Color;
  6. 導入tank.common.Tank;
導入tank.common.Tank;
public
public> class MyTank extends Tank {
public MyTank(int x, int y) {
    super(x, y, Color.yellow);
  1. lifes = 5;
  2. super.setImageID(0);
  3. }
  4. public int getLife() {
  5. return life;
  6. }
  7. }
  8. }
複製程式碼🎜>
    包tank.entity;
  1. 導入tank.common.Bullet;
  2. 公用類別NormalBulletpideBullet {
  3. 公用類別NormalBullet kallet {
  4. super(x, y, 2, 方向);
  5. power = 1;
  6. }
  7. }
  8. 複製程式碼
  9. 包tank.entity;
  10. 導入java.util.ArrayList;
  11. 導入tank.common.EnemyTank
  12. public
  13. 導入tank.common.EnemyTank
  14. public private int totalEnemyNum;
  15. private int leftEnemyNum;
  16. private int totalHeroLife;
  17. private int leftHeroLife; private ArrayList< ;敵方戰車> enemeyTanks;
  18. private int activeEnemyTankNum;
  19. private EnemyTank activeEnemyTanks[];
  20. private MyTank Hero; this.totalEnemyNum =totalEnemyNum;
  21. this.totalHeroLife =totalHeroLife;
  22. this.level = level;
  23. this.activeEnemypank. EnemyNum;
  24. this.leaveHeroLife = this.totalHeroLife;
  25. this.enemeyTanks = new ArrayList();
  26. this.activeEnemyTanks = new EnemyTank[this.activeEnemyTank> (290, 370);
  27. this.wallContainer = new WallContainer();
  28. }
  29. public void addEnemyTank(EnemyTank Tank) {
  30. enem *TeyT.addok) };
  31. public void autoCreateEnemyTank() {
  32. for (int i = 0; i double key = Math.random();
  33. if (key this.enemeyTanks.add(new EnemyTank1(0, 0, Hero));
  34. } else if (key >= 0.66) {
  35. this.enemeyTanks.add>(new EnemyTank2(new EnemyT)2(new) ) , 0, 英雄));
  36. } else {
  37. this.enemeyTanks.add(new EnemyTank3(0, 0, 英雄));
  38. }
  39. }
  40. }
  41. }
  42. public int getActiveEnemyTankNum() {
  43. return this.activeEnemyTankNum;
  44. }
  45. public EnemyTank[] getEnemyTank() { 🎜 > public MyTank getHeroTank() {
  46. return this.hero;
  47. }
  48. public int getLeaveEnemyNum() {
  49. return this.leaveEnemyNum }
  50. public int getLeaveHeroLife() {
  51. return this.leaveHeroLife;
  52. }
  53. public int getLevel() {
  54. return this.level;
  55. }
  56. } return this.wallContainer;
  57. }
  58. public boolean isHeroDead() {
  59. if (this.leaveHeroLife return true; } else
  60. return false;
  61. }
  62. public EnemyTank popEnemyTank() {
  63. if (leaveEnemyNum >; 0) {
  64. . enemeyTanks.get(enemeyTanks.size() - 1);
  65. enemeyTanks.remove(temp);
  66. return temp;
  67. } else
  68. return null public MyTank popHero() {
  69. leftHeroLife--;
  70. MyTank temp = new MyTank(290, 370);
  71. temp. setWalls(wallContainer.getWallList());
  72. temp. setWalls(wallContainer.getWallList());
  73. return temp; }
  74. public void setHeroTank(MyTank Tank) {
  75. this.hero = Tank;
  76. }
}
}
  1. 複製程式碼
  2. 🎜>
  3. 包tank.entity;
  4. 導入tank.common.Wall;
  5. public class Steels extends Wall {
public Steels(int x 。 >
  1. 包tank.entity;
  2. 導入java.util.ArrayList;
  3. 導入tank.common.Common;
  4. >tank.common .Wall;
  5. 公共類別WallContainer 實作Common {
  6. private ArrayList; data;
  7. public WallContainer() {
  8. data = new ArrayList();
  9. for (int i = 0; i this . addWall(10 + i * 20, 100, 水);
  10. if (i == 11) {
  11. i += 4;
  12. }
  13. }
  14. for (int i = 0; i this.addWall(10 + i * 20, 160, 牆);
  15. if (i == 12) {
  16. i += 4 ;
  17. }
  18. }
  19. for (int i = 0; i this.addWall(10 + i * 20, 220, STEELS);
  20. if (i == 11) {
  21. i += 4;
  22. }
  23. }
  24. for (int i = 0; i this.addWall(10 + i * 20, 280, GRASS);
  25. if (i == 12) {
  26. i += 4;
  27. }
  28. }
  29. }
  30. }
  31. }
  32. }
  33. }
  34. }
  35. }
  36. }
  37. }
  38. }
  39. public void addWall(int x, int y, int kind) {
  40. switch (kind) {
  41. case WATER:
  42. data.add(new Water(x, y));
  43. 打破;
  44. 案例牆:
  45. data.add(new Walls(x, y));
  46. 打破;
  47. 案例鋼鐵:
  48. data.add(new Steels(x, y) ) ));
  49. 中斷;
  50. case GRASS:
  51. data.add(new Grass(x, y));
  52. 中斷;
  53. }
  54. }
  55. public ArrayList getWallList() {
  56. ArrayList; temp = data;
  57. return temp;
  58. }
  59. public boolean isEmpty() {
  60. return data.isEmpty();
  61. }
public void🎜 ) {
for (int i = 0; i 牆壁溫度= data.get(i); if (!temp.isAlive() )
data.remove(temp);
}
}
    }
  1. 複製程式碼
  2. package Tank.entity;
導入 Tank.common.Wall;public class Walls extends Wall {
public Walls(int x 。 >
  1. 包tank.entity;
  2. 導入tank.common.Wall;
  3. public class Water extends Wall {
  4. public Water(inte x 。 >
  5. 套件tank.gui;
導入javax.swing.JFrame;導入javax.swing.JMenu;導入javax.swing.JMenu;Barswing.JMenu;
import javax.swing.JMenuItem;
public class TankFrame extends JFrame {
  1. /**
  2. *
  3. */
  4. private static final final long serialfored; private TankPanel gamePanel;
  5. public TankFrame() {
  6. super("坦克大戰————玄雨製作");
  7. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);添加遊戲主面板
  8. gamePanel = new TankPanel();
  9. this.add(gamePanel);
  10. gamePanel.addMouseListener(gamePanel);
  11. this.addKeyListener(gamePanel);
  12. // 新增選單列
  13. JMenuBar menuBar = new JMenuBar();
  14. // ///////////////////////////// // // //
  15. JMenu menu1 = new JMenu("選單");
  16. menuBar.add(menu1);
  17. JMenuItem itemNewGame = new JMenuItem("新遊戲");
  18. menu1. add( itemNewGame) ;
  19. menu1.addSeparator();
  20. JMenuItem itemList = new JMenuItem("排行榜");
  21. menu1.add(itemList);
  22. menu1. JMenuItem itemExit = new JMenuItem("退出");
  23. menu1.add(itemExit);
  24. // /////////////////////// //// //// ///////
  25. JMenu menu2 = new JMenu("設定");
  26. JMenuItem itemSet = new JMenuItem("設定");
  27. menu2.add( itemSet);
  28. 選單列。 add(menu2);
  29. // /////////////////////////////////
  30. JMenu menu3 = new JMenu("幫助");
  31. menuBar.add(menu3);
  32. JMenuItem itemInfo = new JMenuItem("關於");
  33. menu3.add(itemInfo);
  34. JMenuItem itemHelp = new JMenux "幫助");
  35. menu3.add(itemHelp);
  36. this.setJMenuBar(menuBar);
  37. this.setResizable(false);
  38. this.pack();
  39. this.setVisible(true);
  40. }
  41. }
  42. 複製程式碼
    1. 包tank.start;
    2. 導入tank.gui.TankFrame;
    3. 公共類TankStart {
    4. /**
    5. * @param args
    6. */
    7. public static void main(String[] args) {
    8. // TODO 自動生成的方法存根
    9. new TankFrame().setLocation(250, 150);
    10. }
    11. }
    複製程式碼


相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!