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));
}