ホームページ  >  記事  >  バックエンド開発  >  戦車戦デモ

戦車戦デモ

WBOY
WBOYオリジナル
2016-07-25 09:08:161640ブラウズ
基本的な機能は実現されています。ローカル戦車の進路探索は比較的弱いです。各戦車は同時に最大 5 発の砲弾を発射できます。敵には 3 種類の兵力があり、メニュー バーは反応しません。これは非常に簡単です。必要に応じて自分で追加できます。学習用にすべてのコードをアップロードします。 戦車戦デモ
  1. パッケージ Tank.common;
  2. abstract public クラス Bullet 実装 Runnable、Common {
  3. private int x, y;
  4. private int Speed;
  5. private int Direction;
  6. private boolean active;
  7. protected int power;
  8. public Bullet(int x, int y, int 速度, int 方向) {
  9. this.x = x;
  10. this.y = y;
  11. this.speed = 速度;
  12. this.direction = 方向;
  13. 生きている = true;
  14. }
  15. public void die() {
  16. live = 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. // TODO 自動生成された catch ブロック
  36. e.printStackTrace();
  37. }
  38. switch (方向) {
  39. case UP:
  40. y -= 速度;
  41. ブレーク;
  42. case DOWN:
  43. y += 速度;
  44. ブレーク;
  45. ケース右:
  46. x += 速度;
  47. ブレーク;
  48. ケース左:
  49. x -= 速度;
  50. ブレーク;
  51. }
  52. if (x < 0 || x > WIDTH || y >高さ || y 生きている = false;
  53. return;
  54. }
  55. }
  56. }
  57. }
パッケージ Tank.common;
  1. public インターフェース Common {
  2. public static Final int UP = 0;
  3. public static Final int DOWN = 1;
  4. public static Final int RIGHT = 2;
  5. public static Final int LEFT = 3 ;
  6. パブリック静的final int WIDTH = 600;
  7. パブリック静的final int HEIGHT = 400;
  8. パブリック静的final int WATER = 0;
  9. パブリック静的final int WALLS = 1;
  10. パブリック静的final int STEELS = 2;
  11. パブリック静的final int GRASS = 3;
  12. }
コードをコピー
  1. package Tank.common;
  2. import java.awt.Color;
  3. import java.awt.Point;
  4. public abstract class EnemyTank extends Tank {
  5. private class AutoFireimplements Runnable {
  6. @Override
  7. public void run () {
  8. // TODO 自動生成されたメソッド stub
  9. while (isAlive()) {
  10. try {
  11. Thread.sleep(500);
  12. } catch (InterruptedException e) {
  13. // TODO 自動生成された catch ブロック
  14. e .printStackTrace();
  15. }
  16. if (Math.random() > shotKey) {
  17. Shot();
  18. }
  19. }
  20. }
  21. }
  22. プライベート クラス AutoMove 実装 Runnable {
  23. @Override
  24. public void run () {
  25. // TODO 自動生成メソッド stub
  26. int move[] = new int[4];
  27. 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 move[i] = judgeHero(i);
  35. }
  36. int direct = 0;
  37. int max = Integer.MIN_VALUE;
  38. for (int i = 0; i if (moves[i] >= max) {
  39. max = move [i];
  40. 方向 = i;
  41. }
  42. }
  43. move(direction);
  44. }
  45. }
  46. }
  47. プライベート ダブルショットKey;
  48. プライベート ポイント heroPosition;
  49. プライベート タンク ヒーロー;
  50. パブリック EnemyTank(int x, int y, Tank hero, int life, int ID) {
  51. super(x, y, Color.chan);
  52. this.lifes = life;
  53. this.hero = hero;
  54. super.setImageID(ID);
  55. }
  56. private int judgeHero(int Direction) {
  57. heroPosition = new Point(hero.getX() + 9, hero.getY() + 9);
  58. int result = 0;
  59. int x = this.getX();
  60. int y = this.getY();
  61. int Speed = this.getSpeed();
  62. double distance1 = 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 -= 速度;
  69. ブレーク;
  70. 下の場合:
  71. y += 速度;
  72. ブレーク;
  73. 右の場合:
  74. x += 速度;
  75. ブレーク;
  76. 左の場合:
  77. x -= 速度;
  78. ブレーク;
  79. }
  80. if (getDirection() == 方向) {
  81. result += 5000;
  82. }
  83. if (!canMove(x, y)) {
  84. result -= Integer.MAX_VALUE;
  85. }
  86. double distance2 = Math.abs((x - heroPosition.x) * (x - heroPosition.x)
  87. + (y - heroPosition.y) * (y - heroPosition.y));
  88. if (Math.random() > 0.8) {
  89. result += Math.random() * 20000;
  90. }
  91. result += ( distance1 - distance2) * 10;
  92. return result;
  93. }
  94. public void setPosition(int x, int y) {
  95. super .x = x;
  96. super.y = y;
  97. }
  98. public void setShotSpeed(doubleshotSpeed) {
  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. }
  107. }
复制代
  1. パッケージ Tank.common;
  2. import java.awt.Color;
  3. import java.util.ArrayList;
  4. import Tank.entity.NormalBullet;
  5. public abstract class Tank は Common {
  6. protected int x;
  7. を実装します
  8. protected int y;
  9. private Color color;
  10. private int 速度;
  11. private int 方向;
  12. private ArrayList箇条書き;
  13. プライベート ArrayListタンク;
  14. プライベート Bullet 弾丸;
  15. プライベート int maxBulletNum;
  16. プライベート ブール アライブ;
  17. 保護された int ライフ;
  18. ArrayList壁;
  19. プライベート int タンク画像 ID;
  20. {
  21. 速度 = 2;
  22. 方向 = 上;
  23. 生きている = true;
  24. }
  25. パブリック Tank() {
  26. this.x = 0;
  27. this.y = 0;
  28. color = Color.black;
  29. }
  30. public Tank(int x, int y, Color color) {
  31. this.x = x;
  32. this.y = y;
  33. this.color = color;
  34. maxBulletNum = 5;
  35. Bullets = new ArrayList();
  36. Walls = new ArrayList();
  37. }
  38. protected boolean canMove(int x, int y) {
  39. if (x < 0 || x > WIDTH - 20 || y < 0 || y > HEIGHT - 20) {
  40. return false;
  41. }
  42. if (tanks == null) {
  43. return true;
  44. }
  45. if (tanks.size() 1) {
  46. return true;
  47. }
  48. for (int i = 0; i 壁 tempWall = Walls.get(i);
  49. if (tempWall.isAlive( )) {
  50. if (x >= tempWall.getX() && y >= tempWall.getY()) {
  51. if (x <= tempWall.getX() + 20 && y <= tempWall.getY( ) + 20) {
  52. 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.getX() && y > = tempWall.getY()) {
  58. if ((x + 20) >= tempWall.getX()
  59. && y return tempWall.canBeWalk();
  60. }
  61. } else if (x <= tempWall.getX() && y <= tempWall.getY()) {
  62. if ((x + 20) >= tempWall.getX()
  63. && (y + 20) > = tempWall.getY()) {
  64. return tempWall.canBeWalk();
  65. }
  66. }
  67. }
  68. }
  69. for (int i = 0;私は<タンクのサイズ(); ++i) {
  70. タンク tempTank = Tanks.get(i);
  71. if (tempTank == this)
  72. Break;
  73. if (tempTank.isAlive()) {
  74. if (x >= tempTank.getX() && y >= tempTank.getY()) {
  75. if (x return false;
  76. }
  77. } else if ( x >= tempTank.getX() && y if (x && (y + 20) >= tempTank.getY() ) {
  78. return false;
  79. }
  80. } else if (x = tempTank.getY()) {
  81. if ((x + 20) >= tempTank.getX()
  82. && y <= tempTank.getY() + 20) {
  83. return false;
  84. }
  85. } else if (x <= tempTank.getX() && y <= tempTank.getY()) {
  86. if ( (x + 20) >= tempTank.getX()
  87. && (y + 20) >= tempTank.getY()) {
  88. return false;
  89. }
  90. }
  91. }
  92. }
  93. return true;
  94. }
  95. public voiddamage(int power) {
  96. lifes -= power;
  97. if (lifes live = false;
  98. }
  99. }
  100. public ArrayList getBullet() {
  101. 弾丸を返す;
  102. }
  103. public Color getColor() {
  104. カラーを返す;
  105. }
  106. public int getDirection() {
  107. 方向を返す;
  108. }
  109. public int getImageID() {
  110. TankImageID を返す;
  111. }
  112. public int getSpeed() {
  113. 戻り速度;
  114. }
  115. public ArrayList getWalls() {
  116. return this.walls;
  117. }
  118. public int getX() {
  119. return x;
  120. }
  121. public int getY() {
  122. return y;
  123. }
  124. public boolean isAlive() {
  125. return live;
  126. }
  127. public void move(int direction) {
  128. setDirection(direction);
  129. int x = this.x;
  130. int y = this.y;
  131. switch (direction) {
  132. case UP:
  133. y -=速度;
  134. ブレーク;
  135. ケース DOWN:
  136. y += 速度;
  137. ブレーク;
  138. ケース 右:
  139. x += 速度;
  140. ブレーク;
  141. ケース 左:
  142. x -= 速度;
  143. ブレーク;
  144. }
  145. if (canMove (x, y)) {
  146. this.x = x;
  147. this.y = y;
  148. }
  149. }
  150. public void setAllTanks(ArrayList タンク) {
  151. this.tanks = タンク;
  152. }
  153. public void setDirection(int 方向) {
  154. this.direction = 方向;
  155. }
  156. Final protected void setImageID(int ID) {
  157. this.tankImageID = ID;
  158. }
  159. public void setSpeed(int Speed) {
  160. this.speed = Speed;
  161. }
  162. public void setWalls(ArrayListwallList ) {
  163. this.walls = WallList;
  164. }
  165. public void Shot() {
  166. スイッチ (方向) {
  167. case UP:
  168. Bullet = new NormalBullet(x + 10, y, UP);
  169. Break;
  170. case DOWN :
  171. 箇条書き = 新しい NormalBullet(x + 10, y + 20, DOWN);
  172. ブレーク;
  173. ケース右:
  174. ブレット = 新しい NormalBullet(x + 20, y + 10, RIGHT);
  175. ブレーク;
  176. ケース左:
  177. Bullet = new NormalBullet(x, y + 10, LEFT);
  178. Break;
  179. }
  180. for (int i = 0; i Bullet temp = Bullets.get(i) );
  181. if (!temp.isAlive()) {
  182. Bullets.remove(temp);
  183. }
  184. }
  185. if (bullets.size() >= maxBulletNum) {
  186. } else {
  187. new Thread(bullet) .start();
  188. Bullets.add(bullet);
  189. }
  190. }
  191. }
复制代
  1. パッケージtank.common;
  2. パブリック抽象クラスWall {
  3. private int x, y;
  4. private intwallImageID;
  5. private boolean canWalk;
  6. private boolean canFly;
  7. private booleanalive;
  8. private boolean canHit;
  9. public Wall(int x, int y, int ID, boolean walk, boolean fly, boolean Hit) {
  10. this.x = x;
  11. this.y = y;
  12. this.wallImageID = ID;
  13. this .canWalk = walk;
  14. this.canFly = fly;
  15. this.alive = true;
  16. this.canHit = Hit;
  17. }
  18. public boolean canBeFly() {
  19. return canFly;
  20. }
  21. public boolean canBeHit() {
  22. return this.canHit;
  23. }
  24. public boolean canBeWalk() {
  25. return canWalk;
  26. }
  27. public void die() {
  28. live = false;
  29. }
  30. public int getImageID() {
  31. return WallImageID;
  32. }
  33. public int getX() {
  34. return x;
  35. }
  36. public int getY() {
  37. return y;
  38. }
  39. public boolean isAlive() {
  40. return alive;
  41. }
  42. }
复制代
  1. package Tank.entity;
  2. public class Bomb {
  3. private int x;
  4. private int y;
  5. private int life = 9;
  6. private boolean active;
  7. public Bomb(int x, int y) {
  8. this.x = x;
  9. this.y = y;
  10. 生きている = true;
  11. }
  12. public void decrese() {
  13. if (life > 0) {
  14. life--;
  15. } else {
  16. 生きている= false;
  17. }
  18. }
  19. public int getLife() {
  20. return life;
  21. }
  22. public int getX() {
  23. return x;
  24. }
  25. public int getY() {
  26. return y;
  27. }
  28. public boolean isAlive() {
  29. 生き返る;
  30. }
  31. }
复制代
  1. パッケージtank.entity;
  2. インポートtank.common.EnemyTank;
  3. インポートtank.common.Tank;
  4. public class EnemyTank1 extends EnemyTank {
  5. public EnemyTank1(int x, int y, Tank hero) {
  6. super(x, y, hero, 3, 1);
  7. setSpeed(2);
  8. setShotSpeed(0.8);
  9. }
  10. }
复制代
  1. パッケージtank.entity;
  2. インポートtank.common.EnemyTank;
  3. インポートtank.common.Tank;
  4. public class EnemyTank2 extends EnemyTank {
  5. public EnemyTank2(int x, int y, Tank hero) {
  6. super (x, y, hero, 5, 2);
  7. setSpeed(3);
  8. setShotSpeed(0.5);
  9. }
  10. }
复制代
  1. パッケージtank.entity;
  2. インポートtank.common.EnemyTank;
  3. インポートtank.common.Tank;
  4. public class EnemyTank3 extends EnemyTank {
  5. public EnemyTank3(int x, int y, Tank hero) {
  6. super (x, y, hero, 10, 3);
  7. setSpeed(5);
  8. setShotSpeed(0.2);
  9. }
  10. }
复制代
  1. package Tank.entity;
  2. import 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. package Tank.entity;
  2. import java.awt.Color;
  3. import Tank.common.Tank;
  4. public class MyTank extends Tank {
  5. public MyTank(int x, int y) {
  6. super(x) , y, Color. yellow);
  7. lifes = 5;
  8. super.setImageID(0);
  9. }
  10. public int getLife() {
  11. return lifes;
  12. }
  13. }
复制代
  1. パッケージ Tank.entity;
  2. import Tank.common.Bullet;
  3. public class NormalBullet extends Bullet {
  4. public NormalBullet(int x, int y, int direct) {
  5. super(x, y, 2,方向);
  6. power = 1;
  7. }
  8. }
复制代
  1. package Tank.entity;
  2. import java.util.ArrayList;
  3. import Tank.common.EnemyTank;
  4. public class Stage {
  5. private int totalEnemyNum;
  6. private int LeaveEnemyNum;
  7. private int totalHeroLife;
  8. private int LeaveHeroLife;
  9. private int level;
  10. private WallContainerwallContainer;
  11. private ArrayList enemeyTanks;
  12. private int activeEnemyTankNum;
  13. private EnemyTank activeEnemyTanks[];
  14. private MyTank hero;
  15. public Stage(int totalEnemyNum, int totalHeroLife, int level,
  16. int activeEnemyTankNum) {
  17. this.totalEnemyNum = totalEnemyNum;
  18. this.totalHeroLife = totalHeroLife;
  19. this.level = level;
  20. this.activeEnemyTankNum = activeEnemyTankNum;
  21. this.leaveEnemyNum = this.totalEnemyNum;
  22. this.leaveHeroLife = this.totalHeroLife;
  23. this.enemeyTanks = new ArrayList();
  24. this。 activeEnemyTanks = new EnemyTank[this.activeEnemyTankNum];
  25. this.hero = new MyTank(290, 370);
  26. this.wallContainer = new WallContainer();
  27. }
  28. public void addEnemyTank(EnemyTank タンク) {
  29. enemeyTanks.add(タンク);
  30. }
  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(0, 0, hero));
  36. } else {
  37. this.enemeyTanks.add(new EnemyTank3(0, 0, hero));
  38. }
  39. }
  40. }
  41. public int getActiveEnemyTankNum() {
  42. return this.activeEnemyTankNum;
  43. }
  44. public EnemyTank[] getEnemyTank () {
  45. return this.activeEnemyTanks;
  46. }
  47. public MyTank getHeroTank() {
  48. return this.hero;
  49. }
  50. public int getLeaveEnemyNum() {
  51. return this.leaveEnemyNum;
  52. }
  53. public int getLeaveHeroLife() {
  54. return this.leaveHeroLife;
  55. }
  56. public int getLevel() {
  57. return this.level;
  58. }
  59. public WallContainer getWallContainer() {
  60. return this.wallContainer;
  61. }
  62. public boolean isHeroDead() {
  63. if (this.leaveHeroLife return true;
  64. } else
  65. return false;
  66. }
  67. public EnemyTank PopEnemyTank() {
  68. if (leaveEnemyNum > 0) {
  69. this.leaveEnemyNum--;
  70. EnemyTank temp = enemeyTanks.get(enemeyTanks.size() - 1);
  71. enemeyTanks.remove(temp);
  72. return temp;
  73. } else
  74. return null;
  75. }
  76. public MyTank PopHero() {
  77. LeaveHeroLife--;
  78. MyTank temp = new MyTank(290, 370);
  79. temp.setWalls(wallContainer.getWallList());
  80. return temp;
  81. }
  82. public void setHeroTank(MyTank Tank) {
  83. this.hero = Tank;
  84. }
  85. }
复制代码
  1. package Tank.entity;
  2. import Tank.common.Wall;
  3. public class Steels extends Wall {
  4. public Steels(int x, int y) {
  5. super(x, y, 2, false, false, false);
  6. }
  7. }
复制代
  1. package Tank.entity;
  2. import java.util.ArrayList;
  3. import Tank.common.Common;
  4. import Tank.common.Wall;
  5. public class 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 , WATER);
  10. if (i == 11) {
  11. i += 4;
  12. }
  13. }
  14. for (int i = 0; i this.addWall(10 + i) * 20, 160, WALLS);
  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. public void addWall(int x, int y, int kind) {
  31. switch (kind) {
  32. case WATER:
  33. data.add(new Water(x, y));
  34. Break;
  35. case WALLS:
  36. data.add(new Walls(x, y));
  37. Break ;
  38. ケース STEELS:
  39. data.add(new Steels(x, y));
  40. ブレーク;
  41. ケース GRASS:
  42. data.add(new Grass(x, y));
  43. ブレーク;
  44. }
  45. }
  46. public ArrayList getWallList() {
  47. ArrayList temp = data;
  48. return temp;
  49. }
  50. public boolean isEmpty() {
  51. return data.isEmpty();
  52. }
  53. public void RemoveDead() {
  54. for (int i = 0; i 壁温度 = data.get(i);
  55. if (!temp.isAlive())
  56. data.remove(temp);
  57. }
  58. }
  59. }
复制代
  1. package Tank.entity;
  2. import Tank.common.Wall;
  3. public class Walls extends Wall {
  4. public Walls(int x, int y) {
  5. super(x, y, 1, false, false, true);
  6. }
  7. }
复制發
  1. package Tank.entity;
  2. import Tank.common.Wall;
  3. public class Water extends Wall {
  4. public Water(int x, int y) {
  5. super(x, y, 0, false, true, false);
  6. }
  7. }
复制代
  1. パッケージ Tank.gui;
  2. import javax.swing.JFrame;
  3. import javax.swing.JMenu;
  4. import javax.swing.JMenuBar;
  5. import javax.swing.JMenuItem;
  6. public class TankFrame extends JFrame {
  7. /**
  8. *
  9. */
  10. private static Final longserialVersionUID = 1L;
  11. private TankPanel gamePanel;
  12. public TankFrame() {
  13. super("谷克大战————玄雨制作");
  14. これ。 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15. // 追加游戏主面板
  16. gamePanel = new TankPanel();
  17. this.add(gamePanel);
  18. gamePanel.addMouseListener(gamePanel);
  19. this.addKeyListener(gamePanel);
  20. //追加菜单栏
  21. JMenuBar menuBar = new JMenuBar();
  22. // ///////////////////////////////
  23. JMenu menu1 = new JMenu("菜单");
  24. menuBar.add(menu1);
  25. JMenuItem itemNewGame = new JMenuItem("新游戏");
  26. menu1.add(itemNewGame);
  27. menu1.addSeparator();
  28. JMenuItem itemList = new JMenuItem ("排行トレンド");
  29. menu1.add(itemList);
  30. menu1.addSeparator();
  31. JMenuItem itemExit = new JMenuItem("退出");
  32. menu1.add(itemExit);
  33. // ///// /////////////////////////////
  34. JMenu menu2 = new JMenu("设置");
  35. JMenuItem itemSet = new JMenuItem("设置" ");
  36. menu2.add(itemSet);
  37. menuBar.add(menu2);
  38. // //////////////////////////// /////
  39. JMenu menu3 = new JMenu("帮助");
  40. menuBar.add(menu3);
  41. JMenuItem itemInfo = new JMenuItem("关について");
  42. menu3.add(itemInfo);
  43. JMenuItem itemHelp = new JMenuItem("帮助");
  44. menu3.add(itemHelp);
  45. this.setJMenuBar(menuBar);
  46. this.setResizable(false);
  47. this.pack();
  48. this.setVisible(true);
  49. }
  50. }
复制定
  1. package Tank.start;
  2. import Tank.gui.TankFrame;
  3. public class TankStart {
  4. /**
  5. * @param args
  6. */
  7. public static void main(String[] args) {
  8. // TODO自動生成されたメソッド スタブ
  9. new TankFrame().setLocation(250, 150);
  10. }
  11. }
复制代


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。