坦克大战[源码] ---你懂得

import java.awt.Color; import java.awt.Frame; import java.awt.Graphics; import java.awt.Image; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.ArrayList; import java.util.List; public class TankClient extends Frame{ public static final int GAMEWEIGHT = 800; public static final int GAMEHEIGHT = 600; Image offScreenImage = null; Tank myTank = new Tank(700, 50, true, this); List<Missile> missiles = new ArrayList<Missile>(); List<Explode> explodes = new ArrayList<Explode>(); List<Tank> tanks = new ArrayList<Tank>(); Wall w1 = new Wall(300, 200, 20, 150, this); Wall w2 = new Wall(50, 500, 300, 20, this); Blood b = new Blood(); private static final long serialVersionUID = 1L; //主线程入口 public static void main(String[] args) { TankClient tc = new TankClient(); tc.launchFrame(); } public void launchFrame() { setBounds(50, 50, GAMEWEIGHT, GAMEHEIGHT); setResizable(false); //用匿名类处理窗口关闭事件 addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { setVisible(false); System.exit(0); } }); addKeyListener(new KeyMonitor()); setTitle("TanKing ......."); setBackground(Color.GRAY); setVisible(true); for(int i = 0; i < 10; i++) { tanks.add(new Tank(40 * (i + 1), 400, false, this, Tank.Direction.U)); } new Thread(new PaintThread()).start(); } public void paint(Graphics g) { g.drawString("missiles conut:" + missiles.size(), 10, 50); g.drawString("explodes count:" + explodes.size(), 10, 70); g.drawString("tanks count" + tanks.size(), 10, 90); g.drawString("MyTank life:" + myTank.getLife(), 10, 110); if(tanks.size() <= 0) { for(int i = 0; i < 5; i++) { tanks.add(new Tank(40 * (i + 1), 400, false, this, Tank.Direction.U)); } } myTank.draw(g); myTank.eat(b); for(int i = 0; i < tanks.size(); i++) { Tank t = tanks.get(i); t.collidesWall(w1); t.collidesWall(w2); t.CollidesWithTanks(tanks); t.draw(g); } for(int i = 0; i < missiles.size(); i++) { Missile m = missiles.get(i); m.hitTanks(tanks); m.hitTank(myTank); m.hitWall(w1); m.hitWall(w2); m.draw(g); } for(int i = 0; i < explodes.size(); i++) { Explode e = explodes.get(i); e.draw(g); } w1.draw(g); w2.draw(g); b.draw(g); } public void update(Graphics g) { if(offScreenImage == null) { offScreenImage = this.createImage(GAMEWEIGHT, GAMEHEIGHT); } Graphics gOffScreenImage = offScreenImage.getGraphics(); Color c = gOffScreenImage.getColor(); gOffScreenImage.setColor(Color.GRAY); gOffScreenImage.fillRect(0, 0, GAMEWEIGHT, GAMEHEIGHT); gOffScreenImage.setColor(c); paint(gOffScreenImage); g.drawImage(offScreenImage, 0, 0, null); } private class PaintThread implements Runnable { public void run() { while(true) { repaint(); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } } private class KeyMonitor extends KeyAdapter { public void keyPressed(KeyEvent e) { myTank.keyPressed(e); } public void keyReleased(KeyEvent e) { myTank.keyReleased(e); } } } import java.awt.Color; import java.util.*; import java.awt.Graphics; import java.awt.Rectangle; import java.awt.event.KeyEvent; public class Tank { public static final int XSPEED = 5; public static final int YSPEED = 5; public static final int WIDTH = 30; public static final int HEIGHT = 30; private static Random r = new Random(); private BloodBar bb = new BloodBar(); private int x, oldX; private int y, oldY; int step = r.nextInt(12) + 3; TankClient tc; boolean good; private int life = 100; private boolean live = true; public void setLife(int life) { this.life = life; } public int getLife() { return life; } public boolean isLive() { return live; } public void setLive(boolean live) { this.live = live; } public Tank(int x, int y, boolean good, TankClient tc, Direction dir) { this(x, y, good, tc); this.dir = dir; } public Tank(int x, int y, boolean good, TankClient tc) { this(x, y, good); this.tc = tc; } public Tank(int x, int y, boolean good) { this.x = x; this.y = y; this.oldX = x; this.oldY = y; this.good = good; } private boolean bL = false; private boolean bR = false; private boolean bU = false; private boolean bD = false; enum Direction {L, LU, U, RU, R, RD, D, LD, STOP}; private Direction dir = Direction.STOP; private Direction ptDir = Direction.D; public void draw(Graphics g) { if(!live) { if(!good) { tc.tanks.remove(this); } return; } Color c = g.getColor(); if(good) { g.setColor(Color.RED); } else { g.setColor(Color.YELLOW); } g.fillOval(x, y, WIDTH, HEIGHT); g.setColor(c); drawPt(g); if(good) { bb.draw(g); } move(); } public void drawPt(Graphics g) { switch(ptDir) { case L: g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT/ 2, x, y + Tank.HEIGHT / 2); break; case LU: g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT/ 2, x, y); break; case U: g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT/ 2, x + Tank.WIDTH / 2, y); break; case RU: g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT/ 2, x + Tank.WIDTH, y); break; case R: g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT/ 2, x + Tank.WIDTH , y + Tank.HEIGHT / 2); break; case RD: g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT/ 2, x + Tank.WIDTH, y + Tank.HEIGHT); break; case D: g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT/ 2, x + Tank.WIDTH / 2, y + Tank.HEIGHT); break; case LD: g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT/ 2, x, y + Tank.HEIGHT); break; } } public void move() { this.oldX = x; this.oldY = y; switch(dir) { case L: x -= XSPEED; break; case LU: x -= XSPEED; y -= YSPEED; break; case U: y -= YSPEED; break; case RU: y -= YSPEED; x += XSPEED; break; case R: x += XSPEED; break; case RD: x += XSPEED; y += YSPEED; break; case D: y += YSPEED; break; case LD: y += YSPEED; x -= XSPEED; break; case STOP: break; } if(dir != Direction.STOP) { ptDir = dir; } if(x < 0) x = 0; if(y < 24) y = 24; if(x + Tank.WIDTH > TankClient.GAMEWEIGHT) { x = TankClient.GAMEWEIGHT- Tank.WIDTH; } if(y + Tank.HEIGHT > TankClient.GAMEHEIGHT) { y = TankClient.GAMEHEIGHT - Tank.HEIGHT; } if(!good) { Direction[] dirs = Direction.values(); if(step == 0) { step = r.nextInt(40) + 9; int rn = r.nextInt(dirs.length); dir = dirs[rn]; } if(r.nextInt(40) > 36) { this.fire(); } step --; } } public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); switch(key) { case KeyEvent.VK_UP: bU = true; break; case KeyEvent.VK_DOWN: bD = true; break; case KeyEvent.VK_LEFT: bL = true; break; case KeyEvent.VK_RIGHT: bR = true; break; } setLocateDirection(); } public void keyReleased(KeyEvent e) { int key = e.getKeyCode(); switch(key) { case KeyEvent.VK_UP: bU = false; break; case KeyEvent.VK_DOWN: bD = false; break; case KeyEvent.VK_LEFT: bL = false; break; case KeyEvent.VK_RIGHT: bR = false; break; case KeyEvent.VK_CONTROL: fire(); break; case KeyEvent.VK_A: superFire(); break; case KeyEvent.VK_F2: if(!live) { this.live = true; this.life = 100; } break; } setLocateDirection(); } public void setLocateDirection() { if(!bL && !bU && !bR && !bD) dir = Direction.STOP; else if(bL && !bU && !bR && !bD) dir = Direction.L; else if(!bL && bU && !bR && !bD) dir = Direction.U; else if(!bL && !bU && bR && !bD) dir = Direction.R; else if(!bL && !bU && !bR && bD) dir = Direction.D; else if(bL && bU && !bR && !bD) dir = Direction.LU; else if(bL && !bU && !bR && bD) dir = Direction.LD; else if(!bL && bU && bR && !bD) dir = Direction.RU; else if(!bL && !bU && bR && bD) dir = Direction.RD; } public Missile fire() { if(!live) { return null; } int x = this.x + Tank.WIDTH / 2 - Missile.WIDTH /2; int y = this.y + Tank.HEIGHT /2 - Missile.HEIGHT /2; Missile m = new Missile(x, y, ptDir,tc, good); tc.missiles.add(m); return m; } public Rectangle getRect() { return new Rectangle(x, y, WIDTH, HEIGHT); } private void stay() { x = oldX; y = oldY; } public boolean collidesWall(Wall w) { if(this.live && this.getRect().intersects(w.getRect())) { this.stay(); return true; } return false; } public boolean CollidesWithTanks(List<Tank> tanks) { for(int i = 0; i < tanks.size(); i++) { Tank t = tanks.get(i); if(this != t) { if(this.live && t.isLive() && this.getRect().intersects(t.getRect())) { this.stay(); t.stay(); return true; } } } return false; } public Missile fire(Direction dir) { if(!live) { return null; } int x = this.x + Tank.WIDTH / 2 - Missile.WIDTH /2; int y = this.y + Tank.HEIGHT /2 - Missile.HEIGHT /2; Missile m = new Missile(x, y, dir,tc, good); tc.missiles.add(m); return m; } private void superFire() { Direction[] dirs = Direction.values(); for(int i = 0; i < 8; i++) { tc.missiles.add(fire(dirs[i])); } } private class BloodBar { public void draw(Graphics g) { Color c = g.getColor(); g.setColor(Color.GREEN); g.drawRect(x, y-10, WIDTH, 10); int w = WIDTH * life / 100; g.fillRect(x, y-10, w, 10); g.setColor(c); } } public boolean eat(Blood b) { if(this.live && b.isLive()&& this.getRect().intersects(b.getRect())) { this.life = 100; b.setLive(false); return true; } return false; } }
import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import java.util.List; public class Missile { public static final int XSPEED = 20; public static final int YSPEED = 20; public static final int WIDTH = 10; public static final int HEIGHT = 10; private boolean live = true; int x; int y; Tank.Direction dir; private TankClient tc; boolean good; public void setLive(boolean live) { this.live = live; } public boolean isLive() { return live; } public Missile(int x, int y, Tank.Direction dir) { this.x = x; this.y = y; this.dir = dir; } public Missile(int x, int y, Tank.Direction dir,TankClient tc) { this(x, y, dir); this.tc = tc; } public Missile(int x, int y, Tank.Direction dir,TankClient tc, boolean good) { this(x, y, dir, tc); this.good = good; } public void draw(Graphics g) { if(!live) { tc.missiles.remove(this); return; } Color c = g.getColor(); g.setColor(Color.BLACK); g.fillOval(x, y, WIDTH, HEIGHT); g.setColor(c); move(); } private void move() { switch(dir) { case L: x -= XSPEED; break; case LU: x -= XSPEED; y -= YSPEED; break; case U: y -= YSPEED; break; case RU: y -= YSPEED; x += XSPEED; break; case R: x += XSPEED; break; case RD: x += XSPEED; y += YSPEED; break; case D: y += YSPEED; break; case LD: y += YSPEED; x -= XSPEED; break; } if(x < 0 || y < 0 || x > TankClient.GAMEWEIGHT || y > TankClient.GAMEHEIGHT) { live = false; } } public Rectangle getRect() { return new Rectangle(x, y, WIDTH, HEIGHT); } public boolean hitTank(Tank t) { if(this.live && this.getRect().intersects(t.getRect()) && t.isLive() && this.good != t.good) { if(t.good) { t.setLife(t.getLife() - 20); if(t.getLife() <= 0) { t.setLive(false); } } else { t.setLive(false); } this.live = false; Explode e = new Explode(x, y, tc); tc.explodes.add(e); return true; } return false; } public boolean hitTanks(List<Tank> tanks) { for(int i = 0; i < tanks.size(); i++) { if(hitTank(tanks.get(i))) { return true; } } return false; } public boolean hitWall(Wall w) { if(this.live && this.getRect().intersects(w.getRect())) { this.live = false; return true; } return false; } }
import java.awt.*; public class Explode { int x; int y; private TankClient tc; public Explode(int x, int y, TankClient tc) { this.x = x; this.y = y; this.tc = tc; } private boolean live = true; int [] diameter = { 4, 7 , 12, 23, 34, 12, 6 }; int step = 0; public void draw(Graphics g) { if(!live) { tc.explodes.remove(this); return; } if(step == diameter.length) { live = false; step = 0; return; } Color c = g.getColor(); g.setColor(Color.ORANGE); g.fillOval(x , y, diameter[step], diameter[step]); g.setColor(c); step ++; } }
import java.awt.*; public class Wall { int x, y, w, h; TankClient tc; public Wall(int x, int y, int w, int h, TankClient tc) { this.x = x; this.y = y; this.w = w; this.h = h; this.tc = tc; } public void draw(Graphics g) { Color c = g.getColor(); g.setColor(Color.BLACK); g.fillRect(x, y, w, h); g.setColor(c); } public Rectangle getRect() { return new Rectangle(x, y, w, h); } }
import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; public class Blood { int x, y, w, h; TankClient tc; int step = 0; private boolean live = true; public void setLive(boolean live) { this.live = live; } public boolean isLive() { return live; } private int [][] pos = { {70,70}, {120,70}, {170,70}, {220,70}, {270,70}, {320,70}, {370,7} }; public Blood() { x = pos[0][0]; y = pos[0][1]; w = h = 15; } public void draw(Graphics g) { if(!live) { return; } Color c = g.getColor(); g.setColor(Color.MAGENTA); g.fillRect(x, y, w, h); g.setColor(c); move(); } private void move() { step ++; if(step == pos.length) { step = 0; } x = pos[step][0]; y = pos[step][1]; } public Rectangle getRect() { return new Rectangle(x, y, w, h); } }

----------------------------------------------------------------------华丽的分割线----------------------------------------------------------------------------------------------------------------------------------------------

---------------------------- //美化版-------

import java.awt.Color; import java.util.*; import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.event.KeyEvent; public class Tank { public static final int XSPEED = 5; public static final int YSPEED = 5; public static final int WIDTH = 30; public static final int HEIGHT = 30; private static Random r = new Random(); private BloodBar bb = new BloodBar(); private int x, oldX; private int y, oldY; int step = r.nextInt(12) + 3; TankClient tc; boolean good; private int life = 100; private static Toolkit tk = Toolkit.getDefaultToolkit(); private static Image [] imgs = null; private static Map<String, Image> img = new HashMap<String, Image>(); static { imgs = new Image[] { tk.getImage(Tank.class.getClassLoader().getResource("images/tankL.gif")), tk.getImage(Tank.class.getClassLoader().getResource("images/tankLU.gif")), tk.getImage(Tank.class.getClassLoader().getResource("images/tankU.gif")), tk.getImage(Tank.class.getClassLoader().getResource("images/tankRU.gif")), tk.getImage(Tank.class.getClassLoader().getResource("images/tankR.gif")), tk.getImage(Tank.class.getClassLoader().getResource("images/tankRD.gif")), tk.getImage(Tank.class.getClassLoader().getResource("images/tankD.gif")), tk.getImage(Tank.class.getClassLoader().getResource("images/tankLD.gif")) }; img.put("L", imgs[0]); img.put("LU", imgs[1]); img.put("U", imgs[2]); img.put("RU", imgs[3]); img.put("R", imgs[4]); img.put("RD", imgs[5]); img.put("D", imgs[6]); img.put("LD", imgs[7]); } private boolean live = true; public void setLife(int life) { this.life = life; } public int getLife() { return life; } public boolean isLive() { return live; } public void setLive(boolean live) { this.live = live; } public Tank(int x, int y, boolean good, TankClient tc, Direction dir) { this(x, y, good, tc); this.dir = dir; } public Tank(int x, int y, boolean good, TankClient tc) { this(x, y, good); this.tc = tc; } public Tank(int x, int y, boolean good) { this.x = x; this.y = y; this.oldX = x; this.oldY = y; this.good = good; } private boolean bL = false; private boolean bR = false; private boolean bU = false; private boolean bD = false; enum Direction {L, LU, U, RU, R, RD, D, LD, STOP}; private Direction dir = Direction.STOP; private Direction ptDir = Direction.D; public void draw(Graphics g) { if(!live) { if(!good) { tc.tanks.remove(this); } return; } drawPt(g); if(good) { bb.draw(g); } move(); } public void drawPt(Graphics g) { switch(ptDir) { case L: g.drawImage(img.get("L"), x, y, null); break; case LU: g.drawImage(img.get("LU"), x, y, null); break; case U: g.drawImage(img.get("U"), x, y, null); break; case RU: g.drawImage(img.get("RU"), x, y, null); break; case R: g.drawImage(img.get("R"), x, y, null); break; case RD: g.drawImage(img.get("RD"), x, y, null); break; case D: g.drawImage(img.get("D"), x, y, null); break; case LD: g.drawImage(img.get("LD"), x, y, null); break; } } public void move() { this.oldX = x; this.oldY = y; switch(dir) { case L: x -= XSPEED; break; case LU: x -= XSPEED; y -= YSPEED; break; case U: y -= YSPEED; break; case RU: y -= YSPEED; x += XSPEED; break; case R: x += XSPEED; break; case RD: x += XSPEED; y += YSPEED; break; case D: y += YSPEED; break; case LD: y += YSPEED; x -= XSPEED; break; case STOP: break; } if(dir != Direction.STOP) { ptDir = dir; } if(x < 0) x = 0; if(y < 24) y = 24; if(x + Tank.WIDTH > TankClient.GAMEWEIGHT) { x = TankClient.GAMEWEIGHT- Tank.WIDTH; } if(y + Tank.HEIGHT > TankClient.GAMEHEIGHT) { y = TankClient.GAMEHEIGHT - Tank.HEIGHT; } if(!good) { Direction[] dirs = Direction.values(); if(step == 0) { step = r.nextInt(40) + 9; int rn = r.nextInt(dirs.length); dir = dirs[rn]; } if(r.nextInt(40) > 36) { this.fire(); } step --; } } public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); switch(key) { case KeyEvent.VK_UP: bU = true; break; case KeyEvent.VK_DOWN: bD = true; break; case KeyEvent.VK_LEFT: bL = true; break; case KeyEvent.VK_RIGHT: bR = true; break; } setLocateDirection(); } public void keyReleased(KeyEvent e) { int key = e.getKeyCode(); switch(key) { case KeyEvent.VK_UP: bU = false; break; case KeyEvent.VK_DOWN: bD = false; break; case KeyEvent.VK_LEFT: bL = false; break; case KeyEvent.VK_RIGHT: bR = false; break; case KeyEvent.VK_CONTROL: fire(); break; case KeyEvent.VK_A: superFire(); break; case KeyEvent.VK_F2: if(!live) { this.live = true; this.life = 100; } break; } setLocateDirection(); } public void setLocateDirection() { if(!bL && !bU && !bR && !bD) dir = Direction.STOP; else if(bL && !bU && !bR && !bD) dir = Direction.L; else if(!bL && bU && !bR && !bD) dir = Direction.U; else if(!bL && !bU && bR && !bD) dir = Direction.R; else if(!bL && !bU && !bR && bD) dir = Direction.D; else if(bL && bU && !bR && !bD) dir = Direction.LU; else if(bL && !bU && !bR && bD) dir = Direction.LD; else if(!bL && bU && bR && !bD) dir = Direction.RU; else if(!bL && !bU && bR && bD) dir = Direction.RD; } public Missile fire() { if(!live) { return null; } int x = this.x + Tank.WIDTH / 2 - Missile.WIDTH /2; int y = this.y + Tank.HEIGHT /2 - Missile.HEIGHT /2; Missile m = new Missile(x, y, ptDir,tc, good); tc.missiles.add(m); return m; } public Rectangle getRect() { return new Rectangle(x, y, WIDTH, HEIGHT); } private void stay() { x = oldX; y = oldY; } public boolean collidesWall(Wall w) { if(this.live && this.getRect().intersects(w.getRect())) { this.stay(); return true; } return false; } public boolean CollidesWithTanks(List<Tank> tanks) { for(int i = 0; i < tanks.size(); i++) { Tank t = tanks.get(i); if(this != t) { if(this.live && t.isLive() && this.getRect().intersects(t.getRect())) { this.stay(); t.stay(); return true; } } } return false; } public Missile fire(Direction dir) { if(!live) { return null; } int x = this.x + Tank.WIDTH / 2 - Missile.WIDTH /2; int y = this.y + Tank.HEIGHT /2 - Missile.HEIGHT /2; Missile m = new Missile(x, y, dir,tc, good); tc.missiles.add(m); return m; } private void superFire() { Direction[] dirs = Direction.values(); for(int i = 0; i < 8; i++) { tc.missiles.add(fire(dirs[i])); } } private class BloodBar { public void draw(Graphics g) { Color c = g.getColor(); g.setColor(Color.GREEN); g.drawRect(x, y-10, WIDTH, 10); int w = WIDTH * life / 100; g.fillRect(x, y-10, w, 10); g.setColor(c); } } public boolean eat(Blood b) { if(this.live && b.isLive()&& this.getRect().intersects(b.getRect())) { this.life = 100; b.setLive(false); return true; } return false; } }
import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; public class Explode { int x; int y; private TankClient tc; public Explode(int x, int y, TankClient tc) { this.x = x; this.y = y; this.tc = tc; } private boolean live = true; private static boolean init = false; private static Toolkit tk = Toolkit.getDefaultToolkit(); private static Image [] imgs = { tk.getImage(Explode.class.getClassLoader().getResource("images/0.gif")), tk.getImage(Explode.class.getClassLoader().getResource("images/1.gif")), tk.getImage(Explode.class.getClassLoader().getResource("images/2.gif")), tk.getImage(Explode.class.getClassLoader().getResource("images/3.gif")), tk.getImage(Explode.class.getClassLoader().getResource("images/4.gif")), tk.getImage(Explode.class.getClassLoader().getResource("images/5.gif")), tk.getImage(Explode.class.getClassLoader().getResource("images/6.gif")), tk.getImage(Explode.class.getClassLoader().getResource("images/7.gif")), tk.getImage(Explode.class.getClassLoader().getResource("images/8.gif")), tk.getImage(Explode.class.getClassLoader().getResource("images/9.gif")), tk.getImage(Explode.class.getClassLoader().getResource("images/10.gif")) }; int step = 0; public void draw(Graphics g) { if(!init) { for (int j = 0; j < imgs.length; j++) { g.drawImage(imgs[j], -100, -100, null); } init = true; } if(!live) { tc.explodes.remove(this); return; } if(step == imgs.length) { live = false; step = 0; return; } g.drawImage(imgs[step], x, y, null); step ++; } }
import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle; import java.awt.Toolkit; import java.util.HashMap; import java.util.List; import java.util.Map; public class Missile { public static final int XSPEED = 20; public static final int YSPEED = 20; public static final int WIDTH = 10; public static final int HEIGHT = 10; private boolean live = true; int x; int y; Tank.Direction dir; private TankClient tc; boolean good; private static Toolkit tk = Toolkit.getDefaultToolkit(); private static Image [] imgs = null; private static Map<String, Image> img = new HashMap<String, Image>(); static { imgs = new Image[] { tk.getImage(Missile.class.getClassLoader().getResource("images/missileL.gif")), tk.getImage(Missile.class.getClassLoader().getResource("images/missileLU.gif")), tk.getImage(Missile.class.getClassLoader().getResource("images/missileU.gif")), tk.getImage(Missile.class.getClassLoader().getResource("images/missileRU.gif")), tk.getImage(Missile.class.getClassLoader().getResource("images/missileR.gif")), tk.getImage(Missile.class.getClassLoader().getResource("images/missileRD.gif")), tk.getImage(Missile.class.getClassLoader().getResource("images/missileD.gif")), tk.getImage(Missile.class.getClassLoader().getResource("images/missileLD.gif")) }; img.put("L", imgs[0]); img.put("LU", imgs[1]); img.put("U", imgs[2]); img.put("RU", imgs[3]); img.put("R", imgs[4]); img.put("RD", imgs[5]); img.put("D", imgs[6]); img.put("LD", imgs[7]); } public void setLive(boolean live) { this.live = live; } public boolean isLive() { return live; } public Missile(int x, int y, Tank.Direction dir) { this.x = x; this.y = y; this.dir = dir; } public Missile(int x, int y, Tank.Direction dir,TankClient tc) { this(x, y, dir); this.tc = tc; } public Missile(int x, int y, Tank.Direction dir,TankClient tc, boolean good) { this(x, y, dir, tc); this.good = good; } public void draw(Graphics g) { if(!live) { tc.missiles.remove(this); return; } switch(dir) { case L: g.drawImage(img.get("L"), x, y, null); break; case LU: g.drawImage(img.get("LU"), x, y, null); break; case U: g.drawImage(img.get("U"), x, y, null); break; case RU: g.drawImage(img.get("RU"), x, y, null); break; case R: g.drawImage(img.get("R"), x, y, null); break; case RD: g.drawImage(img.get("RD"), x, y, null); break; case D: g.drawImage(img.get("D"), x, y, null); break; case LD: g.drawImage(img.get("LD"), x, y, null); break; } move(); } private void move() { switch(dir) { case L: x -= XSPEED; break; case LU: x -= XSPEED; y -= YSPEED; break; case U: y -= YSPEED; break; case RU: y -= YSPEED; x += XSPEED; break; case R: x += XSPEED; break; case RD: x += XSPEED; y += YSPEED; break; case D: y += YSPEED; break; case LD: y += YSPEED; x -= XSPEED; break; } if(x < 0 || y < 0 || x > TankClient.GAMEWEIGHT || y > TankClient.GAMEHEIGHT) { live = false; } } public Rectangle getRect() { return new Rectangle(x, y, WIDTH, HEIGHT); } public boolean hitTank(Tank t) { if(this.live && this.getRect().intersects(t.getRect()) && t.isLive() && this.good != t.good) { if(t.good) { t.setLife(t.getLife() - 20); if(t.getLife() <= 0) { t.setLive(false); } } else { t.setLive(false); } this.live = false; Explode e = new Explode(x, y, tc); tc.explodes.add(e); return true; } return false; } public boolean hitTanks(List<Tank> tanks) { for(int i = 0; i < tanks.size(); i++) { if(hitTank(tanks.get(i))) { return true; } } return false; } public boolean hitWall(Wall w) { if(this.live && this.getRect().intersects(w.getRect())) { this.live = false; return true; } return false; } }
---------------------------------------------------华丽的分割线----------------------------------------------------------------------------------------------------------------------

----------------------------//加配置文件的方法

配置文件名:tank.properties

initTankCount=10
reProduceTankCount=15

--------------------------------使用局部配置

//加配置文件 ; Properties props = new Properties(); try { props.load(this.getClass().getClassLoader().getResourceAsStream("config/tank.properties")); } catch (IOException e1) { e1.printStackTrace(); } int initTankCount = Integer.parseInt(props.getProperty("initTankCount")); for(int i = 0; i < initTankCount; i++) { tanks.add(new Tank(40 * (i + 1), 400, false, this, Tank.Direction.U)); }
------------------使用全局配置

import java.io.IOException; import java.util.Properties; public class PropertyMgr { static Properties props = new Properties(); static { try { props.load(PropertyMgr.class.getClass().getClassLoader().getResourceAsStream("config/tank.properties")); } catch (IOException e1) { e1.printStackTrace(); } } private PropertyMgr(){}; public static String getProperty(String key) { return props.getProperty(key); } }
for(int i = 0; i < Integer.parseInt(PropertyMgr.getProperty("initTankCount")); i++) { tanks.add(new Tank(40 * (i + 1), 400, false, this, Tank.Direction.U)); }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值