本文来自:小易博客专栏。转载请注明出处:http://blog.csdn.net/oldinaction

在此小易将坦克大战这个项目分为几个版本,以此对J2SE的知识进行回顾和总结,希望这样也能给刚学完J2SE的小伙伴们一点启示!

如果嫌代码太长,可以在这里下载打包好的源代码哦!

坦克大战效果图:

坦克大战V0.7图片版实现功能:

1、将方向定义为一个Enum类写在一个文件里,修正坦克子弹的颜色
2、加入坦克、子弹、爆炸的图片
3、添加配置文件,并导出可运行的jar包

注意事项:

1、ProperMrg类中props.load(ProperMgr.class.getClassLoader().getResourceAsStream("config/tank.properties"));的解释:

(1)得到ProperMgr这个类编译后的class文件(ProperMgr.class)
(2)得到类装载器(getClassLoader())
(3)通过装载器得到源文件("config/tank.properties")的Stream(getResourceAsStream())
(4)加载(load)源文件

2、Tank类中获取图片tk.getImage(Tank.class.getClassLoader().getResource("images/tankL.gif"))的解释:

(1)Tank.class.getClassLoader()得到某个类(Tank)的装载器

(2)再获取文件getResource("文件路径")

(3)这样既不是绝对路径也不是相对路径,文件路径是根据装载器来获取相应的路径的。不用担心文件被移动后的路径问题(所有的文件最终会被编译到bin目录下)

坦克大战V0.7图片版源代码:

包com.exjava.tankWar中含有的类

TankClient类:

  1. package com.exjava.tankWar;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.IOException;
  5. import java.util.List; //java.awt包中也有个List,所以此处要导包明确
  6. import java.util.ArrayList;
  7. import java.util.Properties;
  8.  
  9. /**
  10. * 主要的类
  11. * @ClassName: TankClient
  12. * @Description: 坦克,子弹,爆炸,血块都在这里实例化
  13. * @author oldinaction
  14. * @date 2014年9月2日 下午4:15:44
  15. */
  16. public class TankClient extends Frame {
  17. public static final int GAME_WIDTH = 800;
  18. public static final int GAME_HEIGHT = 600;
  19.  
  20. Tank myTank = new Tank(350, 520, true, Direction.STOP, this); //我方坦克
  21. Wall w1 = new Wall(100, 300, 20, 100, this), w2 = new Wall(300, 250, 150, 20, this);
  22. Blood b = new Blood(this);
  23.  
  24. List<Missile> missiles = new ArrayList<Missile>(); //定义一个集合来装子弹
  25. List<Explode> explodes = new ArrayList<Explode>();
  26. List<Tank> tanks = new ArrayList<Tank>();
  27.  
  28. Image offScreenImage = null; //定义一个屏幕后的虚拟图片
  29.  
  30. @Override
  31. public void paint(Graphics g) {
  32. if (tanks.size() <= 0) {
  33. for(int i = 0; i < Integer.parseInt(ProperMgr.getProperty("reProduceTank")); i++) {
  34. tanks.add(new Tank(50 + 40*(i+1), 50, false, Direction.D, this));
  35. }
  36. }
  37.  
  38. g.drawString("missiles Count: " + missiles.size(), 10, 50); //用来记录missiles中子弹的个数
  39. g.drawString("explodes Count: " + explodes.size(), 10, 70);
  40. g.drawString("tanks Count: " + tanks.size(), 10, 90);
  41. g.drawString("myTank life: " + myTank.getLife(), 10, 110);
  42.  
  43. for (int i = 0; i < missiles.size(); i++) { //遍历集合,把其中的子弹画出来
  44. Missile m = missiles.get(i);
  45. m.hitTanks(tanks);
  46. m.hitTank(myTank);
  47. m.hitWall(w1); //检测子弹是否撞墙
  48. m.hitWall(w2);
  49. m.drawMissile(g);
  50. }
  51.  
  52. for (int i = 0; i < explodes.size(); i++) {
  53. Explode e = explodes.get(i);
  54. e.draw(g);
  55. }
  56.  
  57. for (int i = 0; i < tanks.size(); i++) {
  58. Tank t = tanks.get(i);
  59. t.collidesWithWall(w1); //检测敌方坦克是否撞墙
  60. t.collidesWithWall(w2);
  61. t.collidesWithTanks(tanks);
  62. t.drawTank(g);
  63. }
  64.  
  65. myTank.drawTank(g);
  66. myTank.eat(b);
  67. w1.draw(g);
  68. w2.draw(g);
  69. b.draw(g);
  70. }
  71.  
  72. //利用双缓冲消除圆圈移动时屏幕的闪动
  73. @Override
  74. public void update(Graphics g) {
  75. if (offScreenImage == null) {
  76. offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT); //判断是为了避免每次重画时都给offScreenImage赋值
  77. }
  78. Graphics gOffScreen = offScreenImage.getGraphics(); //定义虚拟图片上的画笔gOffScreen
  79. Color c = gOffScreen.getColor();
  80. gOffScreen.setColor(Color.BLACK);
  81. gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT); //重画背景,如果没有这句则在屏幕上会保留圆圈的移动路径
  82. gOffScreen.setColor(c);
  83. paint(gOffScreen); //把圆圈画到虚拟图片上
  84. g.drawImage(offScreenImage, 0, 0, null); //再一次性把虚拟图片画到真实屏幕上,在真实屏幕上画则要用真实屏幕的画笔g
  85. }
  86.  
  87. public void luanchFrame() {
  88.  
  89. int initTankCount = Integer.parseInt(ProperMgr.getProperty("initTankCount"));
  90.  
  91. for(int i = 0; i < initTankCount; i++) {
  92. tanks.add(new Tank(50 + 40*(i+1), 50, false, Direction.D, this));
  93. }
  94.  
  95. this.setLocation(300, 50);
  96. this.setSize(GAME_WIDTH, GAME_HEIGHT);
  97. this.setTitle("坦克大战 - 游戏还存在Bug,欢迎大家试玩! - 帮助(复活:F2;放弹:1键;超级炮弹:空格) - By:小易 - QQ:381740148");
  98. this.setResizable(false); //不允许改变窗口大小
  99. this.addWindowListener(new WindowAdapter() {
  100. @Override
  101. public void windowClosing(WindowEvent e) {
  102. System.exit(0);
  103. }
  104. }); //添加关闭功能,此处使用匿名类比较合适
  105. this.setBackground(Color.BLACK);
  106.  
  107. this.addKeyListener(new KeyMonitor());
  108.  
  109. setVisible(true);
  110.  
  111. new Thread(new PaintThread()).start(); //启动线程,实例化线程对象时不要忘了new Thread(Runnable对象);
  112. }
  113.  
  114. public static void main(String[] args) {
  115. TankClient tc = new TankClient();
  116. tc.luanchFrame();
  117. }
  118.  
  119. //PaintThread只为TankClient服务,所以写成内部类好些
  120. public class PaintThread implements Runnable {
  121.  
  122. public void run() {
  123. while (true) {
  124. repaint(); //repaint()是TankClient或者他的父类的方法,内部类可以访问外部包装类的成员,这也是内部类的好处
  125. try {
  126. Thread.sleep(50); //每隔50毫秒重画一次
  127. } catch (InterruptedException e) {
  128. e.printStackTrace();
  129. }
  130. }
  131. }
  132. }
  133.  
  134. public class KeyMonitor extends KeyAdapter {
  135.  
  136. @Override
  137. public void keyReleased(KeyEvent e) {
  138. myTank.keyReleased(e);
  139. }
  140.  
  141. @Override
  142. public void keyPressed(KeyEvent e) {
  143. myTank.keyPressed(e);
  144. }
  145. }
  146.  
  147. }

Tank类:

  1. package com.exjava.tankWar;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.Random;
  7.  
  8. public class Tank {
  9. public static final int XSPEED = 5; //定义常量X轴速度
  10. public static final int YSPEED = 5;
  11.  
  12. public static final int WIDTH = 30;
  13. public static final int HEIGHT = 30;
  14.  
  15. public static final int LIFE = 100;
  16.  
  17. //定义一个随机数产生器,此时的Random类是java.util.Random,不同于Math中的
  18. private static Random r = new Random(); //随机数产生器只需要一个,所以定义成静态,防止每次new一个坦克是都会产生一个随机数产生器
  19. private BloodBar bb = new BloodBar();
  20.  
  21. private boolean good; //定义变量说明是我方还是敌方坦克,为true表示我方坦克
  22.  
  23. private boolean live = true; //定义变量说明是坦克是否存活
  24.  
  25. private int life = LIFE; //设置坦克的生命值为100
  26.  
  27. TankClient tc;
  28.  
  29. private int x , y; //定义变量画圆圈(坦克)时四边形左上点的x、y左边
  30. private int oldX , oldY; //定义坦克上个位置的坐标
  31.  
  32. private boolean bL = false, bU = false, bR = false, bD = false; //定义变量左上右下的按键是否被按下
  33.  
  34. private Direction dir = Direction.STOP; //定义变量坦克的方向
  35. private Direction ptDir = Direction.U; //定义变量坦克炮筒的方向,起初向上
  36.  
  37. private int step = r.nextInt(12) + 3; //定义坦克朝着一个方向移动几步
  38.  
  39. private static Toolkit tk = Toolkit.getDefaultToolkit();
  40.  
  41. private static Image[] TankImages = null;
  42. private static Map<String, Image> imgs = new HashMap<String, Image>();
  43.  
  44. //静态代码区,这样这个类的class文件被加载,首先执行这里的代码;一条语句也可以写在里面,最适合给一些变量做初始化
  45. static{
  46. TankImages = new Image[] {
  47. tk.getImage(Tank.class.getClassLoader().getResource("images/tankL.gif")),
  48. tk.getImage(Tank.class.getClassLoader().getResource("images/tankLU.gif")),
  49. tk.getImage(Tank.class.getClassLoader().getResource("images/tankU.gif")),
  50. tk.getImage(Tank.class.getClassLoader().getResource("images/tankRU.gif")),
  51. tk.getImage(Tank.class.getClassLoader().getResource("images/tankR.gif")),
  52. tk.getImage(Tank.class.getClassLoader().getResource("images/tankRD.gif")),
  53. tk.getImage(Tank.class.getClassLoader().getResource("images/tankD.gif")),
  54. tk.getImage(Tank.class.getClassLoader().getResource("images/tankLD.gif"))
  55. };
  56.  
  57. imgs.put("L", TankImages[0]);
  58. imgs.put("LU", TankImages[1]);
  59. imgs.put("U", TankImages[2]);
  60. imgs.put("RU", TankImages[3]);
  61. imgs.put("R", TankImages[4]);
  62. imgs.put("RD", TankImages[5]);
  63. imgs.put("D", TankImages[6]);
  64. imgs.put("LD", TankImages[7]);
  65. }
  66.  
  67. public Tank(int x, int y, boolean good) {
  68. this.x = x;
  69. this.y = y;
  70. this.good = good;
  71. this.oldX = x;
  72. this.oldY =y;
  73. }
  74.  
  75. public Tank(int x, int y, boolean good, Direction dir, TankClient tc) {
  76. this(x, y, good); //相当于调用上面的构造方法
  77. this.dir = dir;
  78. this.tc = tc;
  79. }
  80.  
  81. public void drawTank(Graphics g) {
  82. if(!live) {
  83. if(!good) {
  84. tc.tanks.remove(this);
  85. }
  86. return; //如果坦克没有存活就直接返回,不用画坦克了
  87. }
  88.  
  89. if (good) bb.draw(g);
  90.  
  91. //根据炮筒的方向,画直线代表炮筒
  92. switch (ptDir) {
  93. case L:
  94. g.drawImage(imgs.get("L"), x, y, null);
  95. break;
  96. case LU:
  97. g.drawImage(imgs.get("LU"), x, y, null);
  98. break;
  99. case U:
  100. g.drawImage(imgs.get("U"), x, y, null);
  101. break;
  102. case RU:
  103. g.drawImage(imgs.get("RU"), x, y, null);
  104. break;
  105. case R:
  106. g.drawImage(imgs.get("R"), x, y, null);
  107. break;
  108. case RD:
  109. g.drawImage(imgs.get("RD"), x, y, null);
  110. break;
  111. case D:
  112. g.drawImage(imgs.get("D"), x, y, null);
  113. break;
  114. case LD:
  115. g.drawImage(imgs.get("LD"), x, y, null);
  116. break;
  117. }
  118.  
  119. move(); //每次按键都会重画,就会调用drawTank,在这里重画坦克的此时位置
  120. }
  121.  
  122. public void keyPressed(KeyEvent e) {
  123. int key = e.getKeyCode(); //得到按键的虚拟码,再和下面的KeyEvent.VK_LEFT等虚拟码比较看是否是某按键
  124. switch (key) {
  125. case KeyEvent.VK_F2:
  126. if (!this.live) {
  127. this.live = true;
  128. this.life = LIFE;
  129. }
  130. break;
  131. case KeyEvent.VK_LEFT:
  132. bL = true;
  133. break;
  134. case KeyEvent.VK_UP:
  135. bU = true;
  136. break;
  137. case KeyEvent.VK_RIGHT:
  138. bR = true;
  139. break;
  140. case KeyEvent.VK_DOWN:
  141. bD = true;
  142. break;
  143. }
  144. locateDraction();
  145.  
  146. }
  147.  
  148. public void keyReleased(KeyEvent e) {
  149. int key = e.getKeyCode();
  150. switch (key) {
  151. case KeyEvent.VK_1: //按1就发射子弹调用fire方法
  152. fire(); //只有松开1才能发出子弹
  153. break;
  154. case KeyEvent.VK_LEFT:
  155. bL = false;
  156. break;
  157. case KeyEvent.VK_UP:
  158. bU = false;
  159. break;
  160. case KeyEvent.VK_RIGHT:
  161. bR = false;
  162. break;
  163. case KeyEvent.VK_DOWN:
  164. bD = false;
  165. break;
  166. case KeyEvent.VK_SPACE: //按SPACE就发射子弹调用fire方法
  167. superFire();
  168. break;
  169. }
  170. locateDraction();
  171. }
  172.  
  173. //通过上右下的按键是否被按下判断坦克要运动的方向
  174. void locateDraction() {
  175. if(bL && !bU && !bR && !bD) dir =Direction.L;
  176. else if(bL && bU && !bR && !bD) dir =Direction.LU;
  177. else if(!bL && bU && !bR && !bD) dir =Direction.U;
  178. else if(!bL && bU && bR && !bD) dir =Direction.RU;
  179. else if(!bL && !bU && bR && !bD) dir =Direction.R;
  180. else if(!bL && !bU && bR && bD) dir =Direction.RD;
  181. else if(!bL && !bU && !bR && bD) dir =Direction.D;
  182. else if(bL && !bU && !bR && bD) dir =Direction.LD;
  183. else if(!bL && !bU && !bR && !bD) dir =Direction.STOP;
  184. }
  185.  
  186. public void move() {
  187. oldX = x;
  188. oldY = y;
  189.  
  190. switch (dir) {
  191. case L:
  192. x -= XSPEED;
  193. break;
  194. case LU:
  195. x -= XSPEED;
  196. y -= YSPEED;
  197. break;
  198. case U:
  199. y -= YSPEED;
  200. break;
  201. case RU:
  202. x += XSPEED;
  203. y -= YSPEED;
  204. break;
  205. case R:
  206. x += XSPEED;
  207. break;
  208. case RD:
  209. x += XSPEED;
  210. y += YSPEED;
  211. break;
  212. case D:
  213. y += YSPEED;
  214. break;
  215. case LD:
  216. x -= XSPEED;
  217. y += YSPEED;
  218. break;
  219. case STOP:
  220. break;
  221. }
  222.  
  223. if(this.dir != Direction.STOP) {
  224. this.ptDir = this.dir;
  225. }
  226.  
  227. //防止坦克出界
  228. if (x < 0) x = 0;
  229. if (y < 25) y = 25; //考虑了标题栏的高度
  230. if (x + Tank.WIDTH > TankClient.GAME_WIDTH) x = TankClient.GAME_WIDTH - Tank.WIDTH;
  231. if (y + Tank.HEIGHT > TankClient.GAME_HEIGHT) y = TankClient.GAME_HEIGHT - Tank.HEIGHT;
  232.  
  233. if (!good) {
  234. Direction[] dirs = Direction.values(); //把枚举转换成数组
  235. if (step == 0) {
  236. int rn = r.nextInt(dirs.length);
  237. dir = dirs[rn]; //如果移动步数为0就改变方向
  238. step = r.nextInt(12) + 3;
  239. }
  240. step --;
  241.  
  242. if(r.nextInt(40) > 37) this.fire();
  243. }
  244. }
  245.  
  246. public void stay() {
  247. x = oldX;
  248. y = oldY;
  249. }
  250.  
  251. //坦克开火,就new一个子弹出来
  252. private Missile fire() {
  253. if(!live) return null;
  254.  
  255. int x = this.x + Tank.WIDTH/2 - Missile.WIDTH/2; //让子弹从坦克中心打出
  256. int y = this.y + Tank.HEIGHT/2 - Missile.HEIGHT/2;
  257. Missile m = new Missile(x, y, good, ptDir , this.tc);
  258. tc.missiles.add(m); //每new一个Missile对象就把他装到集合中
  259. return m; //返回的m,其他地方可调用可不调用
  260. }
  261.  
  262. private Missile fire(Direction dir) {
  263. if(!live) return null;
  264.  
  265. int x = this.x + Tank.WIDTH/2 - Missile.WIDTH/2; //让子弹从坦克中心打出
  266. int y = this.y + Tank.HEIGHT/2 - Missile.HEIGHT/2;
  267. Missile m = new Missile(x, y, good, dir , this.tc);
  268. tc.missiles.add(m); //每new一个Missile对象就把他装到集合中
  269. return m; //返回的m,其他地方可调用可不调用
  270. }
  271.  
  272. public Rectangle getRect() {
  273. return new Rectangle(x, y, WIDTH, HEIGHT); //得到坦克的探测方块,Rectangle是java.awt包中专门用于游戏碰撞的类
  274. }
  275.  
  276. public boolean isLive() {
  277. return live;
  278. }
  279.  
  280. //Tank类的成员方法可生成对应的get和set方法,那么在其他类中就可以访问了
  281. public void setLive(boolean live) {
  282. this.live = live;
  283. }
  284.  
  285. public boolean isGood() {
  286. return good;
  287. }
  288.  
  289. //检测坦克是否撞墙
  290. public boolean collidesWithWall(Wall w) {
  291. if(this.live && this.getRect().intersects(w.getRect())) {
  292. this.stay(); //如果坦克撞到墙就让他回到上一个位置
  293. return true;
  294. }
  295. return false;
  296. }
  297.  
  298. //检测坦克是否相撞,java.util.List<E>接口或者类的另一种写法,java.awt中也有List,所以要写明确
  299. public boolean collidesWithTanks(java.util.List<Tank> tanks) {
  300. for (int i = 0; i < tanks.size(); i++) {
  301. Tank t = tanks.get(i);
  302. if (this != t) {
  303. if(this.live && t.isLive() && this.getRect().intersects(t.getRect())) {
  304. this.stay();
  305. t.stay();
  306. return true;
  307. }
  308. }
  309. }
  310. return false;
  311. }
  312.  
  313. //超级炮弹:朝8个方向各发一发炮弹
  314. private void superFire() {
  315. Direction[] dirs = Direction.values();
  316. for (int i = 0; i < 8; i++) { //dirs[8]是STOP
  317. fire(dirs[i]);
  318. }
  319. }
  320.  
  321. public int getLife() {
  322. return life;
  323. }
  324.  
  325. public void setLife(int life) {
  326. this.life = life;
  327. }
  328.  
  329. public class BloodBar {
  330. public void draw(Graphics g) {
  331. Color c = g.getColor();
  332. g.setColor(Color.RED);
  333. g.drawRect(x, y - 10, WIDTH, 10);
  334. int w = WIDTH * life/LIFE ;
  335. g.fillRect(x, y - 10, w, 10);
  336. g.setColor(c);
  337. }
  338. }
  339.  
  340. //吃血块
  341. public boolean eat(Blood b) {
  342. if(this.live && b.isLive() && this.getRect().intersects(b.getRect())) {
  343. b.setLive(false);
  344. life = LIFE;
  345. return true;
  346. }
  347. return false;
  348. }
  349. }

Missile类:

  1. package com.exjava.tankWar;
  2. import java.awt.*;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6.  
  7. public class Missile {
  8. public static final int XSPEED = 10;
  9. public static final int YSPEED = 10;
  10.  
  11. public static final int WIDTH = 10;
  12. public static final int HEIGHT = 10;
  13.  
  14. int x, y;
  15. Direction dir;
  16.  
  17. private boolean good; //定义变量表示是否是我方子弹
  18.  
  19. private boolean live = true; //定义一个判断子弹是否出界的变量
  20.  
  21. private TankClient tc;
  22.  
  23. private static Toolkit tk = Toolkit.getDefaultToolkit();
  24.  
  25. private static Image[] MissileImages = null;
  26. private static Map<String, Image> imgs = new HashMap<String, Image>();
  27.  
  28. static{
  29. MissileImages = new Image[] {
  30. tk.getImage(Tank.class.getClassLoader().getResource("images/missileL.gif")),
  31. tk.getImage(Tank.class.getClassLoader().getResource("images/missileLU.gif")),
  32. tk.getImage(Tank.class.getClassLoader().getResource("images/missileU.gif")),
  33. tk.getImage(Tank.class.getClassLoader().getResource("images/missileRU.gif")),
  34. tk.getImage(Tank.class.getClassLoader().getResource("images/missileR.gif")),
  35. tk.getImage(Tank.class.getClassLoader().getResource("images/missileRD.gif")),
  36. tk.getImage(Tank.class.getClassLoader().getResource("images/missileD.gif")),
  37. tk.getImage(Tank.class.getClassLoader().getResource("images/missileLD.gif"))
  38. };
  39.  
  40. imgs.put("L", MissileImages[0]);
  41. imgs.put("LU", MissileImages[1]);
  42. imgs.put("U", MissileImages[2]);
  43. imgs.put("RU", MissileImages[3]);
  44. imgs.put("R", MissileImages[4]);
  45. imgs.put("RD", MissileImages[5]);
  46. imgs.put("D", MissileImages[6]);
  47. imgs.put("LD", MissileImages[7]);
  48. }
  49.  
  50. public Missile(int x, int y, Direction dir) {
  51. this.x = x;
  52. this.y = y;
  53. this.dir = dir;
  54. }
  55.  
  56. public Missile(int x, int y,boolean good, Direction dir, TankClient tc) {
  57. this(x, y, dir);
  58. this.good = good;
  59. this.tc = tc;
  60. }
  61.  
  62. public void drawMissile(Graphics g) {
  63. if(!live) {
  64. tc.missiles.remove(this);
  65. return;
  66. }
  67.  
  68. switch (dir) {
  69. case L:
  70. g.drawImage(imgs.get("L"), x, y, null);
  71. break;
  72. case LU:
  73. g.drawImage(imgs.get("LU"), x, y, null);
  74. break;
  75. case U:
  76. g.drawImage(imgs.get("U"), x, y, null);
  77. break;
  78. case RU:
  79. g.drawImage(imgs.get("RU"), x, y, null);
  80. break;
  81. case R:
  82. g.drawImage(imgs.get("R"), x, y, null);
  83. break;
  84. case RD:
  85. g.drawImage(imgs.get("RD"), x, y, null);
  86. break;
  87. case D:
  88. g.drawImage(imgs.get("D"), x, y, null);
  89. break;
  90. case LD:
  91. g.drawImage(imgs.get("LD"), x, y, null);
  92. break;
  93. }
  94.  
  95. move();
  96. }
  97.  
  98. private void move() {
  99. switch (dir) {
  100. case L:
  101. x -= XSPEED;
  102. break;
  103. case LU:
  104. x -= XSPEED;
  105. y -= YSPEED;
  106. break;
  107. case U:
  108. y -= YSPEED;
  109. break;
  110. case RU:
  111. x += XSPEED;
  112. y -= YSPEED;
  113. break;
  114. case R:
  115. x += XSPEED;
  116. break;
  117. case RD:
  118. x += XSPEED;
  119. y += YSPEED;
  120. break;
  121. case D:
  122. y += YSPEED;
  123. break;
  124. case LD:
  125. x -= XSPEED;
  126. y += YSPEED;
  127. break;
  128. }
  129.  
  130. if (x < 0 || y < 0 || x > tc.GAME_WIDTH || y > tc.GAME_HEIGHT) {
  131. live = false;
  132. }
  133. }
  134.  
  135. public boolean isLive() {
  136. return live;
  137. }
  138.  
  139. public Rectangle getRect() {
  140. return new Rectangle(x, y, WIDTH, HEIGHT); //得到子弹的探测方块,Rectangle是java.awt包中专门用于游戏碰撞的类
  141. }
  142.  
  143. //判断子弹是否打到了坦克
  144. public boolean hitTank(Tank t) {
  145. //intersects是Rectangle的一个方法;t.isLive()是为了判断坦克是否存活,如果没有则打掉这个坦克后,之后的子弹到达原来这个坦克的位置就会消失
  146. if (this.live && this.getRect().intersects(t.getRect()) && t.isLive() && this.good != t.isGood()) {
  147. if (t.isGood()) {
  148. t.setLife(t.getLife() - 20);
  149. if (t.getLife() <= 0) {
  150. t.setLive(false);
  151. }
  152. } else {
  153. t.setLive(false);
  154. }
  155. this.live = false;
  156. Explode e = new Explode(x, y, tc);
  157. tc.explodes.add(e);
  158. return true;
  159. }
  160. return false;
  161. }
  162.  
  163. public boolean hitTanks(List<Tank> tanks) {
  164. for (int i = 0; i < tanks.size(); i++) {
  165. if (hitTank(tanks.get(i))) {
  166. return true;
  167. }
  168. }
  169. return false;
  170. }
  171.  
  172. public boolean hitWall(Wall w) {
  173. if(this.live && this.getRect().intersects(w.getRect())) {
  174. this.live = false;
  175. return true;
  176. }
  177. return false;
  178. }
  179.  
  180. }

Explode类:

  1. package com.exjava.tankWar;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Image;
  6. import java.awt.Toolkit;
  7.  
  8. //爆炸类(爆炸的英文:explode)
  9. public class Explode {
  10.  
  11. int x, y;
  12. private TankClient tc;
  13.  
  14. private boolean live = true;
  15.  
  16. private boolean init = false; //定义变量表示图片是否已经被加载到内存了
  17.  
  18. private static Toolkit tk = Toolkit.getDefaultToolkit(); //Toolkit是java.awt中的工具包类,这样就可以方便的拿到硬盘的信息
  19.  
  20. private static Image[] imgs = { //每次只需加载一次,所以写出static
  21. //下面用到了反射的概念,Explode.class.getClassLoader()是得到最终生成class文件的装载器,getResource()得到装载器的目录
  22. //此时加载的图片路径绝对和相对都不合适,用这个方法最合适,这个方法比较常用
  23. tk.getImage(Explode.class.getClassLoader().getResource("images/1.gif")),
  24. tk.getImage(Explode.class.getClassLoader().getResource("images/2.gif")),
  25. tk.getImage(Explode.class.getClassLoader().getResource("images/3.gif")),
  26. tk.getImage(Explode.class.getClassLoader().getResource("images/4.gif")),
  27. tk.getImage(Explode.class.getClassLoader().getResource("images/5.gif")),
  28. tk.getImage(Explode.class.getClassLoader().getResource("images/6.gif")),
  29. tk.getImage(Explode.class.getClassLoader().getResource("images/7.gif")),
  30. tk.getImage(Explode.class.getClassLoader().getResource("images/8.gif")),
  31. tk.getImage(Explode.class.getClassLoader().getResource("images/9.gif")),
  32. tk.getImage(Explode.class.getClassLoader().getResource("images/10.gif"))
  33. };
  34.  
  35. int step = 0; //定义变量代表画爆炸的圆画到第几个了
  36.  
  37. public Explode(int x, int y, TankClient tc) {
  38. this.x = x;
  39. this.y = y;
  40. this.tc = tc;
  41. }
  42.  
  43. public void draw(Graphics g) {
  44.  
  45. if(!init) {
  46. for (int i = 0; i < imgs.length; i++) {
  47. g.drawImage(imgs[i], -100, -100, null); //把图片画在看不到的地方
  48. }
  49. init = true;
  50. }
  51.  
  52. if(!live) {
  53. tc.explodes.remove(this);
  54. return;
  55. }
  56.  
  57. if (step == imgs.length) {
  58. live = false;
  59. step = 0;
  60. return;
  61. }
  62.  
  63. g.drawImage(imgs[step], x, y, null);
  64.  
  65. step ++ ;
  66.  
  67. }
  68.  
  69. }

Wall类:

  1. package com.exjava.tankWar;
  2. import java.awt.*;
  3.  
  4. public class Wall {
  5. private int x, y, w, h;
  6. private TankClient tc;
  7.  
  8. public Wall(int x, int y, int w, int h, TankClient tc) {
  9. this.x = x;
  10. this.y = y;
  11. this.w = w;
  12. this.h = h;
  13. this.tc = tc;
  14. }
  15.  
  16. public void draw(Graphics g) {
  17. Color c = g.getColor();
  18. g.setColor(Color.WHITE);
  19. g.fillRect(x, y, w, h);
  20. g.setColor(c);
  21. }
  22.  
  23. public Rectangle getRect() {
  24. return new Rectangle(x, y, w, h);
  25. }
  26. }

Blood类:

  1. package com.exjava.tankWar;
  2. import java.awt.*;
  3.  
  4. public class Blood {
  5.  
  6. private int x, y, w, h;
  7. private TankClient tc;
  8.  
  9. private boolean live = true;
  10.  
  11. private int step = 0;
  12.  
  13. int[][] pos = {
  14. {400,400}, {420,400}, {440,400}, {440,420}, {440,440}, {420,440}, {400,440}, {400,420}
  15. };
  16.  
  17. public Blood(TankClient tc) {
  18. this.x = pos[0][0];
  19. this.y = pos[0][1];
  20. this.w = h = 10;
  21. this.tc = tc;
  22. }
  23.  
  24. public void draw(Graphics g) {
  25. if (!live) return;
  26.  
  27. Color c = g.getColor();
  28. g.setColor(Color.MAGENTA);
  29. g.fillRect(x, y, w, h);
  30. g.setColor(c);
  31.  
  32. move();
  33. }
  34.  
  35. private void move() {
  36. step ++;
  37.  
  38. if(step == pos.length) {
  39. step = 0;
  40. }
  41.  
  42. x = pos[step][0];
  43. y = pos[step][1];
  44. }
  45.  
  46. public Rectangle getRect() {
  47. return new Rectangle(x, y, w, h);
  48. }
  49.  
  50. public boolean isLive() {
  51. return live;
  52. }
  53.  
  54. public void setLive(boolean live) {
  55. this.live = live;
  56. }
  57.  
  58. }

Direction类:

  1. package com.exjava.tankWar;
  2.  
  3. public enum Direction {
  4. L,LU,U,RU,R,RD,D,LD,STOP; //定义枚举类型,值为左、左上、上、右上、右、右下、下、左下、停止
  5. }

ProperMgr类:

  1. package com.exjava.tankWar;
  2.  
  3. import java.io.IOException;
  4. import java.util.Properties;
  5.  
  6. public class ProperMgr {
  7. //Singleton模式,只实例化一个对象
  8. //读配置文件用的对象,这个对象只需要把配置文件加载到内存就行,之后就和他没关系,所以只需实例化一次
  9. static Properties props = new Properties();
  10.  
  11. static {
  12. try {
  13. props.load(ProperMgr.class.getClassLoader().getResourceAsStream("config/tank.properties"));
  14. } catch (IOException e1) {
  15. e1.printStackTrace();
  16. }
  17. }
  18.  
  19. private ProperMgr() {} //构造方法定义成静态的,就是不准别的类new这种对象
  20.  
  21. public static String getProperty(String key) {
  22. return props.getProperty(key);
  23. }
  24.  
  25. }

包config中含有的文件

tank.properties源码:

  1. initTankCount=20
  2. reProduceTank=10

包images中含有的文件上述图片

坦克大战图片下载地址:点击下载

Java坦克大战 (七) 之图片版的更多相关文章

  1. Java坦克大战(四)

    这是我的坦克游戏大战的最后一版,里面添加很多新的功能.这个坦克大战的有很多不足之处,但是对于初学者来说依然是一个很好的练习项目,从中我们可以学习Java基础知识,将知识与项目结合,学习面向对象编程思想 ...

  2. Java坦克大战(三)

    关于这个坦克大战的项目是在学习Java基础的时候,拿来练习的最近看到这些代码,感觉很亲切,就把他们都复制下来,编辑成博客.回首看去,Java基础的学习确实应该建立在找项目练习上,这样才能将学到的基础知 ...

  3. Java坦克大战(二)

    本文紧接上一篇讲解坦克大战这个项目,因为当初在学习的时候,是以这个案例逐步学习Java基础的,过程是先讲知识点,再将知识点逐步的融入到项目中,即给坦克添加新的功能.之前的Demo练习,想都记录下来.这 ...

  4. Java坦克大战 (六) 之增加可玩性

    本文来自:小易博客专栏.转载请注明出处:http://blog.csdn.net/oldinaction 在此小易将坦克大战这个项目分为几个版本,以此对J2SE的知识进行回顾和总结,希望这样也能给刚学 ...

  5. Java坦克大战 (五) 之产生敌方坦克和爆炸效果

    本文来自:小易博客专栏.转载请注明出处:http://blog.csdn.net/oldinaction 在此小易将坦克大战这个项目分为几个版本,以此对J2SE的知识进行回顾和总结,希望这样也能给刚学 ...

  6. Java坦克大战 (四) 之子弹的产生

    本文来自:小易博客专栏.转载请注明出处:http://blog.csdn.net/oldinaction 在此小易将坦克大战这个项目分为几个版本,以此对J2SE的知识进行回顾和总结,希望这样也能给刚学 ...

  7. Java坦克大战 (三) 之可完全控制坦克朝八个方向运动

    本文来自:小易博客专栏.转载请注明出处:http://blog.csdn.net/oldinaction 在此小易将坦克大战这个项目分为几个版本,以此对J2SE的知识进行回顾和总结,希望这样也能给刚学 ...

  8. Java坦克大战 (二) 之画一个能动的圆圈代表坦克

    本文来自:小易博客专栏.转载请注明出处:http://blog.csdn.net/oldinaction 在此小易将坦克大战这个项目分为几个版本,以此对J2SE的知识进行回顾和总结,希望这样也能给刚学 ...

  9. Java坦克大战(一)

    接下来的几篇博客,想记录一下通过学习坦克大战项目来循序渐进的学习Java基础.主要是为了巩固基础知识,当然学习编程重要的还是多敲,问题通常是在敲代码的过程中发现的,积累也是在敲代码中寻求的经验.这个坦 ...

随机推荐

  1. 3.爬虫 urlib库讲解 总结

    urllib库的总结: 用ProcessOn(安利这个软件,够用了)根据前面的几节内容做了个思维导图. urllib库一共有四个模块: request:它是最基本的模块,可以用来模拟发送请求 erro ...

  2. 九度OJ--1165(C++)

    #include <iostream>#include <string>#include <vector> using namespace std; int mai ...

  3. oracle补充

    索引 索引是若干数据行的关键字的列表,查询数据时,通过索引中的关键字可以快速定位到要访问的记录所在的数据块,从而大大减少读取数据的I/O次数,因此可以显著的提高性能 创建索引的SQL 把下面表中的na ...

  4. 【bzoj1455】罗马游戏 可并堆+并查集

    题目描述 罗马皇帝很喜欢玩杀人游戏. 他的军队里面有n个人,每个人都是一个独立的团.最近举行了一次平面几何测试,每个人都得到了一个分数. 皇帝很喜欢平面几何,他对那些得分很低的人嗤之以鼻.他决定玩这样 ...

  5. [SCOI2010]序列操作 线段树

    ---题面--- 题解: 在考场上打的这道题,出人意料的很快就打完了?! 直接用线段树,维护几个东西: 1,lazy标记 : 表示区间赋值 2,mark标记:表示区间翻转 3,l1:前缀最长连续的1的 ...

  6. nfs 和samba

    NFS,是Network File System的简写,即网络文件系统.网络文件系统是FreeBSD支持的文件系统中的一种,也被称为NFS. NFS允许一个系统在网络上与他人共享目录和文件.通过使用N ...

  7. 第116讲 boost::algorithm::string之替换和删除

    http://www.360doc.com/content/16/0523/18/29304643_561672752.shtml

  8. 洛谷P2568 GCD (欧拉函数/莫比乌斯反演)

    P2568 GCD 题目描述 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 输入输出格式 输入格式: 一个整数N 输出格式: 答案 输入输出样例 输入 ...

  9. POJ1459:Power Network(多源点多汇点的最大流)

    Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 31086   Accepted: 15986 题 ...

  10. POJ2912:Rochambeau(带权并查集)

    Rochambeau Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5208   Accepted: 1778 题目链接:h ...