请选择 进入手机版 | 继续访问电脑版
查看: 569|回复: 0

[Java语言] 坦克大战DEMO

3万

主题

3万

帖子

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
100197
发表于 2016-8-13 15:30:00
实现了基本功能....地方坦克寻路做得比较弱智,每个坦克最多能同时发射5发炮弹....敌方有三种兵种,菜单栏没做响应,这个很简单,需要的可以自己加......上传全部代码,仅供学习参考......
  1. package tank.common;
  2. abstract public class Bullet implements Runnable, Common {
  3. private int x, y;
  4. private int speed;
  5. private int direction;
  6. private boolean alive;
  7. protected int power;
  8. public Bullet(int x, int y, int speed, int direction) {
  9. this.x = x;
  10. this.y = y;
  11. this.speed = speed;
  12. this.direction = direction;
  13. alive = true;
  14. }
  15. public void die() {
  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. // TODO Auto-generated catch block
  36. e.printStackTrace();
  37. }
  38. switch (direction) {
  39. case UP:
  40. y -= speed;
  41. break;
  42. case DOWN:
  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 < 0 || x > WIDTH || y > HEIGHT || y < 0) {
  53. alive = false;
  54. return;
  55. }
  56. }
  57. }
  58. }
复制代码
  1. package tank.common;
  2. public interface Common {
  3. public static final int UP = 0;
  4. public static final int DOWN = 1;
  5. public static final int RIGHT = 2;
  6. public static final int LEFT = 3;
  7. public static final int WIDTH = 600;
  8. public static final int HEIGHT = 400;
  9. public static final int WATER = 0;
  10. public static final int WALLS = 1;
  11. public static final int STEELS = 2;
  12. public static final int GRASS = 3;
  13. }
复制代码
  1. package tank.common;
  2. import java.awt.Color;
  3. import java.awt.Point;
  4. public abstract class EnemyTank extends Tank {
  5. private class AutoFire implements Runnable {
  6. @Override
  7. public void run() {
  8. // TODO Auto-generated method stub
  9. while (isAlive()) {
  10. try {
  11. Thread.sleep(500);
  12. } catch (InterruptedException e) {
  13. // TODO Auto-generated catch block
  14. e.printStackTrace();
  15. }
  16. if (Math.random() > shotKey) {
  17. shot();
  18. }
  19. }
  20. }
  21. }
  22. private class AutoMove implements Runnable {
  23. @Override
  24. public void run() {
  25. // TODO Auto-generated method stub
  26. int moves[] = new int[4];
  27. while (isAlive()) {
  28. try {
  29. Thread.sleep(110);
  30. } catch (InterruptedException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. }
  34. for (int i = 0; i < 4; ++i) {
  35. moves[i] = judgeHero(i);
  36. }
  37. int direction = 0;
  38. int max = Integer.MIN_VALUE;
  39. for (int i = 0; i < 4; ++i) {
  40. if (moves[i] >= max) {
  41. max = moves[i];
  42. direction = i;
  43. }
  44. }
  45. move(direction);
  46. }
  47. }
  48. }
  49. private double shotKey;
  50. private Point heroPosition;
  51. private Tank hero;
  52. public EnemyTank(int x, int y, Tank hero, int life, int ID) {
  53. super(x, y, Color.cyan);
  54. this.lifes = life;
  55. this.hero = hero;
  56. super.setImageID(ID);
  57. }
  58. private int judgeHero(int direction) {
  59. heroPosition = new Point(hero.getX() + 9, hero.getY() + 9);
  60. int result = 0;
  61. int x = this.getX();
  62. int y = this.getY();
  63. int speed = this.getSpeed();
  64. double distance1 = Math.abs((this.getX() - heroPosition.x)
  65. * (this.getX() - heroPosition.x)
  66. + (this.getY() - heroPosition.y)
  67. * (this.getY() - heroPosition.y));
  68. switch (direction) {
  69. case UP:
  70. y -= speed;
  71. break;
  72. case DOWN:
  73. y += speed;
  74. break;
  75. case RIGHT:
  76. x += speed;
  77. break;
  78. case LEFT:
  79. x -= speed;
  80. break;
  81. }
  82. if (getDirection() == direction) {
  83. result += 5000;
  84. }
  85. if (!canMove(x, y)) {
  86. result -= Integer.MAX_VALUE;
  87. }
  88. double distance2 = Math.abs((x - heroPosition.x) * (x - heroPosition.x)
  89. + (y - heroPosition.y) * (y - heroPosition.y));
  90. if (Math.random() > 0.8) {
  91. result += Math.random() * 20000;
  92. }
  93. result += (distance1 - distance2) * 10;
  94. return result;
  95. }
  96. public void setPosition(int x, int y) {
  97. super.x = x;
  98. super.y = y;
  99. }
  100. public void setShotSpeed(double shotSpeed) {
  101. this.shotKey = shotSpeed;
  102. }
  103. public void startFire() {
  104. new Thread(new AutoFire()).start();
  105. }
  106. public void startMove() {
  107. new Thread(new AutoMove()).start();
  108. }
  109. }
复制代码
  1. package tank.common;
  2. import java.awt.Color;
  3. import java.util.ArrayList;
  4. import tank.entity.NormalBullet;
  5. public abstract class Tank implements Common {
  6. protected int x;
  7. protected int y;
  8. private Color color;
  9. private int speed;
  10. private int direction;
  11. private ArrayList<Bullet> bullets;
  12. private ArrayList<Tank> tanks;
  13. private Bullet bullet;
  14. private int maxBulletNum;
  15. private boolean alive;
  16. protected int lifes;
  17. ArrayList<Wall> walls;
  18. private int tankImageID;
  19. {
  20. speed = 2;
  21. direction = UP;
  22. alive = true;
  23. }
  24. public Tank() {
  25. this.x = 0;
  26. this.y = 0;
  27. color = Color.black;
  28. }
  29. public Tank(int x, int y, Color color) {
  30. this.x = x;
  31. this.y = y;
  32. this.color = color;
  33. maxBulletNum = 5;
  34. bullets = new ArrayList<Bullet>();
  35. walls = new ArrayList<Wall>();
  36. }
  37. protected boolean canMove(int x, int y) {
  38. if (x < 0 || x > WIDTH - 20 || y < 0 || y > HEIGHT - 20) {
  39. return false;
  40. }
  41. if (tanks == null) {
  42. return true;
  43. }
  44. if (tanks.size() == 1) {
  45. return true;
  46. }
  47. for (int i = 0; i < walls.size(); ++i) {
  48. Wall 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 <= tempWall.getY()) {
  55. if (x <= tempWall.getX() + 20
  56. && (y + 20) >= tempWall.getY()) {
  57. return tempWall.canBeWalk();
  58. }
  59. } else if (x <= tempWall.getX() && y >= tempWall.getY()) {
  60. if ((x + 20) >= tempWall.getX()
  61. && y <= tempWall.getY() + 20) {
  62. return tempWall.canBeWalk();
  63. }
  64. } else if (x <= tempWall.getX() && y <= tempWall.getY()) {
  65. if ((x + 20) >= tempWall.getX()
  66. && (y + 20) >= tempWall.getY()) {
  67. return tempWall.canBeWalk();
  68. }
  69. }
  70. }
  71. }
  72. for (int i = 0; i < tanks.size(); ++i) {
  73. Tank tempTank = tanks.get(i);
  74. if (tempTank == this)
  75. break;
  76. if (tempTank.isAlive()) {
  77. if (x >= tempTank.getX() && y >= tempTank.getY()) {
  78. if (x <= tempTank.getX() + 20 && y <= tempTank.getY() + 20) {
  79. return false;
  80. }
  81. } else if (x >= tempTank.getX() && y <= tempTank.getY()) {
  82. if (x <= tempTank.getX() + 20
  83. && (y + 20) >= tempTank.getY()) {
  84. return false;
  85. }
  86. } else if (x <= tempTank.getX() && y >= tempTank.getY()) {
  87. if ((x + 20) >= tempTank.getX()
  88. && y <= tempTank.getY() + 20) {
  89. return false;
  90. }
  91. } else if (x <= tempTank.getX() && y <= tempTank.getY()) {
  92. if ((x + 20) >= tempTank.getX()
  93. && (y + 20) >= tempTank.getY()) {
  94. return false;
  95. }
  96. }
  97. }
  98. }
  99. return true;
  100. }
  101. public void damage(int power) {
  102. lifes -= power;
  103. if (lifes <= 0) {
  104. alive = false;
  105. }
  106. }
  107. public ArrayList<Bullet> getBullet() {
  108. return bullets;
  109. }
  110. public Color getColor() {
  111. return color;
  112. }
  113. public int getDirection() {
  114. return direction;
  115. }
  116. public int getImageID() {
  117. return tankImageID;
  118. }
  119. public int getSpeed() {
  120. return speed;
  121. }
  122. public ArrayList<Wall> getWalls() {
  123. return this.walls;
  124. }
  125. public int getX() {
  126. return x;
  127. }
  128. public int getY() {
  129. return y;
  130. }
  131. public boolean isAlive() {
  132. return alive;
  133. }
  134. public void move(int direction) {
  135. setDirection(direction);
  136. int x = this.x;
  137. int y = this.y;
  138. switch (direction) {
  139. case UP:
  140. y -= speed;
  141. break;
  142. case DOWN:
  143. y += speed;
  144. break;
  145. case RIGHT:
  146. x += speed;
  147. break;
  148. case LEFT:
  149. x -= speed;
  150. break;
  151. }
  152. if (canMove(x, y)) {
  153. this.x = x;
  154. this.y = y;
  155. }
  156. }
  157. public void setAllTanks(ArrayList<Tank> tanks) {
  158. this.tanks = tanks;
  159. }
  160. public void setDirection(int direction) {
  161. this.direction = direction;
  162. }
  163. final protected void setImageID(int ID) {
  164. this.tankImageID = ID;
  165. }
  166. public void setSpeed(int speed) {
  167. this.speed = speed;
  168. }
  169. public void setWalls(ArrayList<Wall> wallList) {
  170. this.walls = wallList;
  171. }
  172. public void shot() {
  173. switch (direction) {
  174. case UP:
  175. bullet = new NormalBullet(x + 10, y, UP);
  176. break;
  177. case DOWN:
  178. bullet = new NormalBullet(x + 10, y + 20, DOWN);
  179. break;
  180. case RIGHT:
  181. bullet = new NormalBullet(x + 20, y + 10, RIGHT);
  182. break;
  183. case LEFT:
  184. bullet = new NormalBullet(x, y + 10, LEFT);
  185. break;
  186. }
  187. for (int i = 0; i < bullets.size(); ++i) {
  188. Bullet temp = bullets.get(i);
  189. if (!temp.isAlive()) {
  190. bullets.remove(temp);
  191. }
  192. }
  193. if (bullets.size() >= maxBulletNum) {
  194. } else {
  195. new Thread(bullet).start();
  196. bullets.add(bullet);
  197. }
  198. }
  199. }
复制代码
  1. package tank.common;
  2. public abstract class Wall {
  3. private int x, y;
  4. private int wallImageID;
  5. private boolean canWalk;
  6. private boolean canFly;
  7. private boolean alive;
  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. alive = 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 alive;
  7. public Bomb(int x, int y) {
  8. this.x = x;
  9. this.y = y;
  10. alive = true;
  11. }
  12. public void decrese() {
  13. if (life > 0) {
  14. life--;
  15. } else {
  16. alive = 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. return alive;
  30. }
  31. }
复制代码
  1. package tank.entity;
  2. import tank.common.EnemyTank;
  3. import 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. package tank.entity;
  2. import tank.common.EnemyTank;
  3. import 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. package tank.entity;
  2. import tank.common.EnemyTank;
  3. import 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. package tank.entity;
  2. import tank.common.Bullet;
  3. public class NormalBullet extends Bullet {
  4. public NormalBullet(int x, int y, int direction) {
  5. super(x, y, 2, direction);
  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 WallContainer wallContainer;
  11. private ArrayList<EnemyTank> 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<EnemyTank>();
  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 tank) {
  29. enemeyTanks.add(tank);
  30. }
  31. public void autoCreateEnemyTank() {
  32. for (int i = 0; i < totalEnemyNum; ++i) {
  33. double key = Math.random();
  34. if (key <= 0.33) {
  35. this.enemeyTanks.add(new EnemyTank1(0, 0, hero));
  36. } else if (key >= 0.66) {
  37. this.enemeyTanks.add(new EnemyTank2(0, 0, hero));
  38. } else {
  39. this.enemeyTanks.add(new EnemyTank3(0, 0, hero));
  40. }
  41. }
  42. }
  43. public int getActiveEnemyTankNum() {
  44. return this.activeEnemyTankNum;
  45. }
  46. public EnemyTank[] getEnemyTank() {
  47. return this.activeEnemyTanks;
  48. }
  49. public MyTank getHeroTank() {
  50. return this.hero;
  51. }
  52. public int getLeaveEnemyNum() {
  53. return this.leaveEnemyNum;
  54. }
  55. public int getLeaveHeroLife() {
  56. return this.leaveHeroLife;
  57. }
  58. public int getLevel() {
  59. return this.level;
  60. }
  61. public WallContainer getWallContainer() {
  62. return this.wallContainer;
  63. }
  64. public boolean isHeroDead() {
  65. if (this.leaveHeroLife <= 0) {
  66. return true;
  67. } else
  68. return false;
  69. }
  70. public EnemyTank popEnemyTank() {
  71. if (leaveEnemyNum > 0) {
  72. this.leaveEnemyNum--;
  73. EnemyTank temp = enemeyTanks.get(enemeyTanks.size() - 1);
  74. enemeyTanks.remove(temp);
  75. return temp;
  76. } else
  77. return null;
  78. }
  79. public MyTank popHero() {
  80. leaveHeroLife--;
  81. MyTank temp = new MyTank(290, 370);
  82. temp.setWalls(wallContainer.getWallList());
  83. return temp;
  84. }
  85. public void setHeroTank(MyTank tank) {
  86. this.hero = tank;
  87. }
  88. }
复制代码
  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 implements Common {
  6. private ArrayList<Wall> data;
  7. public WallContainer() {
  8. data = new ArrayList<Wall>();
  9. for (int i = 0; i < 28; ++i) {
  10. this.addWall(10 + i * 20, 100, WATER);
  11. if (i == 11) {
  12. i += 4;
  13. }
  14. }
  15. for (int i = 0; i < 28; ++i) {
  16. this.addWall(10 + i * 20, 160, WALLS);
  17. if (i == 12) {
  18. i += 4;
  19. }
  20. }
  21. for (int i = 0; i < 28; ++i) {
  22. this.addWall(10 + i * 20, 220, STEELS);
  23. if (i == 11) {
  24. i += 4;
  25. }
  26. }
  27. for (int i = 0; i < 28; ++i) {
  28. this.addWall(10 + i * 20, 280, GRASS);
  29. if (i == 12) {
  30. i += 4;
  31. }
  32. }
  33. }
  34. public void addWall(int x, int y, int kind) {
  35. switch (kind) {
  36. case WATER:
  37. data.add(new Water(x, y));
  38. break;
  39. case WALLS:
  40. data.add(new Walls(x, y));
  41. break;
  42. case STEELS:
  43. data.add(new Steels(x, y));
  44. break;
  45. case GRASS:
  46. data.add(new Grass(x, y));
  47. break;
  48. }
  49. }
  50. public ArrayList<Wall> getWallList() {
  51. ArrayList<Wall> temp = data;
  52. return temp;
  53. }
  54. public boolean isEmpty() {
  55. return data.isEmpty();
  56. }
  57. public void removeDead() {
  58. for (int i = 0; i < data.size(); ++i) {
  59. Wall temp = data.get(i);
  60. if (!temp.isAlive())
  61. data.remove(temp);
  62. }
  63. }
  64. }
复制代码
  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. package 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 long serialVersionUID = 1L;
  11. private TankPanel gamePanel;
  12. public TankFrame() {
  13. super("坦克大战————玄雨制作");
  14. this.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 Auto-generated method stub
  9. new TankFrame().setLocation(250, 150);
  10. }
  11. }
复制代码



上一篇:添加android广告
下一篇:clone 深拷贝
回复

使用道具 举报