📄 tank.java
字号:
package leo.tank;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
/**
* 用于构建坦克
* @author 黄思志 030402129
*
*/
public class Tank {
public static final int XSPEED = 5,YSPEED = 5;
public static final int WIDTH = 50;
public static final int HEIGHT = 50;
private boolean live = true;
private BloodBar bb = new BloodBar();
private int life = 100;
TankWar tw;
private boolean good = true;
int x,y,oldx,oldy;
private static Random r = new Random();
private int step = r.nextInt(12)+3;
private int liveCount = 3;
private int superMCount = 5;
private boolean bL =false,bU=false, bR=false,bD=false;
private Direction dir = Direction.STOP;
private Direction ptDir = Direction.U;
private static Toolkit tk = Toolkit.getDefaultToolkit();
private static Image[] tankImages = null;
/**
* 建立坦克图片的映射字符
* 用字符索引不同的坦克图片
*/
private static Map<String, Image> imgs = new HashMap<String, Image>();
/**
* 静态函数
* 载入内存后首先执行的函数
* 可以用来初始化变量和函数
* 或执行一些必须在其他函数外执行的方法函数
* 适合做初始化工作
*/
static {
tankImages = 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"))
};
imgs.put("L", tankImages[0]);
imgs.put("LU", tankImages[1]);
imgs.put("U", tankImages[2]);
imgs.put("RU", tankImages[3]);
imgs.put("R", tankImages[4]);
imgs.put("RD", tankImages[5]);
imgs.put("D", tankImages[6]);
imgs.put("LD", tankImages[7]);
}
public Tank(int x, int y,boolean good) {
this.x = x;
this.y = y;
this.oldx = x;
this.oldy = y;
this.good = good;
}
public Tank(int x, int y,boolean good,Direction dir,TankWar tw) {
this(x,y,good);
this.dir = dir;
this.tw = tw;
}
/**
*自绘方法
* @param g
*/
public void draw(Graphics g) {
if(!live){
if(!good)tw.emTk.remove(this);
return;
}
if(good) {
bb.draw(g);
}
switch(ptDir) {
case L:
//g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2,x, y+Tank.HEIGHT/2);
g.drawImage(imgs.get("L"), x, y, null);
break;
case LU:
//g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2,x, y);
g.drawImage(imgs.get("LU"), x, y, null);
break;
case U:
//g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2,x+WIDTH/2, y);
g.drawImage(imgs.get("U"), x, y, null);
break;
case RU:
//g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2,x+WIDTH, y);
g.drawImage(imgs.get("RU"), x, y, null);
break;
case R:
//g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2,x+WIDTH, y+Tank.HEIGHT/2);
g.drawImage(imgs.get("R"), x, y, null);
break;
case RD:
//g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2,x+WIDTH, y+Tank.HEIGHT);
g.drawImage(imgs.get("RD"), x, y, null);
break;
case D:
//g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2,x+WIDTH/2, y+Tank.HEIGHT);
g.drawImage(imgs.get("D"), x, y, null);
break;
case LD:
//g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2,x, y+Tank.HEIGHT);
g.drawImage(imgs.get("LD"), x, y, null);
break;
case STOP:
break;
}
move();
}
/**
* 移动方法
*
*/
public void move() {
oldx = x;
oldy = y;
switch(dir) {
case L:
x -= XSPEED;
break;
case LU:
x -= XSPEED;
y -= YSPEED;
break;
case U:
y -= YSPEED;
break;
case RU:
x += XSPEED;
y -= YSPEED;
break;
case R:
x += XSPEED;
break;
case RD:
x += XSPEED;
y += YSPEED;
break;
case D:
y += YSPEED;
break;
case LD:
x -= XSPEED;
y += YSPEED;
break;
case STOP:
break;
}
if(this.dir != Direction.STOP) {
this.ptDir = this.dir;
}
if(x < 0 || y < 30 || x+Tank.WIDTH > TankWar.vx || y+Tank.HEIGHT > TankWar.vy) {
this.stay();
}
if(!good) {
Direction[] dirs = Direction.values();
if(step == 0){
step = r.nextInt(12)+5;
int rn = r.nextInt(dirs.length);
dir = dirs[rn];
}
step--;
int fireR;
if((tw.gameLevel+1)*(tw.gameThrough+1) > 977)fireR = 0;
else fireR = 977 - (tw.gameLevel+1)*(tw.gameThrough+1)*5;
if(r.nextInt(1000) > fireR) this.fire();
}
//this.hitTank(tc.myTank);
tw.myTank.eatBlood(tw.b);
tw.myTank.eatSuperMissile(tw.sm);
this.hitTanks(tw.emTk);
this.hitWall(tw.w1);
this.hitWall(tw.w2);
}
/**
* 被停止(退到微小的前一步)方法
* 调用于碰撞后
*/
public void stay() {
x = oldx;
y = oldy;
step = 0;
}
/**
* 处理键盘事件的方法
* 键盘按钮按下事件调用
* @param e
*/
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch(key) {
case KeyEvent.VK_A:
case KeyEvent.VK_LEFT:
bL = true;
break;
case KeyEvent.VK_D:
case KeyEvent.VK_RIGHT:
bR = true;
break;
case KeyEvent.VK_W:
case KeyEvent.VK_UP:
bU = true;
break;
case KeyEvent.VK_S:
case KeyEvent.VK_DOWN:
bD = true;
break;
case KeyEvent.VK_Q:
bL = true;
bU = true;
break;
case KeyEvent.VK_E:
bR = true;
bU = true;
break;
default:
break;
}
locateDirection();
}
/**
* 设置生存与否
* @param live
*/
public void setLive(boolean live) {
this.live = live;
}
/**
* 处理键盘弹起事件的方法
* 键盘弹起事件被调用
* @param e
*/
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
switch(key) {
case KeyEvent.VK_A:
case KeyEvent.VK_LEFT:
bL = false;
break;
case KeyEvent.VK_D:
case KeyEvent.VK_RIGHT:
bR = false;
break;
case KeyEvent.VK_W:
case KeyEvent.VK_UP:
bU = false;
break;
case KeyEvent.VK_S:
case KeyEvent.VK_DOWN:
bD = false;
break;
case KeyEvent.VK_Q:
bL = false;
bU = false;
break;
case KeyEvent.VK_E:
bR = false;
bU = false;
break;
case KeyEvent.VK_CONTROL:
fire();
break;
case KeyEvent.VK_SHIFT:
superFire();
break;
case KeyEvent.VK_F2:
if(!tw.myTank.live) {
tw.myTank.reborn();
}
break;
default:
break;
}
locateDirection();
}
/**
* 复活方法
* @return
*/
boolean reborn (){
if(this.liveCount <= 0)return false;
this.liveCount--;
this.live = true;
this.life = 100;
this.superMCount = 5;
this.x = TankWar.vx-100;
this.y = TankWar.vy-100;
this.ptDir = Direction.U;
return true;
}
/**
* 根据键盘值设置移动方向
*
*/
void locateDirection() {
if(bL && !bU && !bR && !bD) dir =Direction.L;
else if(bL && bU && !bR && !bD) dir =Direction.LU;
else if(!bL && bU && !bR && !bD) dir =Direction.U;
else if(!bL && bU && bR && !bD) dir =Direction.RU;
else if(!bL && !bU && bR && !bD) dir =Direction.R;
else if(!bL && !bU && bR && bD) dir =Direction.RD;
else if(!bL && !bU && !bR && bD) dir =Direction.D;
else if(bL && !bU && !bR && bD) dir =Direction.LD;
else if(!bL && !bU && !bR && !bD) dir =Direction.STOP;
}
/**
* 普通射击方法
* 己方坦克调用
* @return
*/
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 mi = new Missile(x,y, good, ptDir,tw);
tw.m.add(mi);
return mi;
}
/**
* 普通射击方法
* 敌方坦克调用
* @param dir
* @return
*/
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 mi = new Missile(x,y, good, dir,tw);
tw.m.add(mi);
return mi;
}
/**
* 超级子弹射击方法
*
*/
public void superFire() {
if(this.superMCount <= 0)
return;
this.superMCount--;
Direction[] dir = Direction.values();
for(int j=0;j<20;j++)
for(int i=0;i<8;i++) {
fire(dir[i]);
}
}
/**
* 获取外接矩形方法
* @return
*/
public Rectangle getRect() {
return new Rectangle(x, y, WIDTH,HEIGHT);
}
/**
* 获取生存与否方法
* @return
*/
public boolean isLive() {
return live;
}
/**
* 判断是否己方坦克方法
* @return
*/
public boolean isGood() {
return good;
}
/**
* 设置坦克是否己方
*/
public void setGood(boolean good) {
this.good = good;
}
/**
* 与其他坦克撞击方法
* @param t 目标坦克
* @return 相撞则返回true
*/
public boolean hitTank(Tank t) {
if(this != t && this.getRect().intersects(t.getRect()) && this.live && t.isLive()) {
if(this.good != t.isGood()) {
this.setLife(this.getLife()-34);
if(this.getLife() <= 0){
this.setLive(false);
if(this.liveCount == 0)
tw.gameOver = true;
}
t.setLive(false);
Explode e = new Explode(x,y,tw);
tw.ep.add(e);
} else {
this.stay();
}
return true;
}
return false;
}
/**
* 与坦克数组里所有坦克撞击的方法
* @param tanks
* @return
*/
public boolean hitTanks(java.util.List<Tank> tanks) {
for(int i=0;i<tanks.size();i++) {
if(hitTank(tanks.get(i))) {
return true;
}
}
return false;
}
/**
* 撞墙方法
* @param w
* @return
*/
public boolean hitWall(Wall w) {
if(this.live && this.getRect().intersects(w.getRect())) {
this.stay();
return true;
}
return false;
}
/**
* 获取坦克生命值
* @return
*/
public int getLife() {
return life;
}
/**
* 设置坦克生命值方法
* @param life
*/
public void setLife(int life) {
if(life < 0) {
this.life = 0;
return;
}
this.life = life;
}
/**
* 坦克血槽
* @author 黄思志 030402129
*
*/
private class BloodBar {
public void draw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.gray);
g.drawRect(x-5, y-10, WIDTH + 10, 4);
int w = (WIDTH + 10)* life/100;
g.setColor(Color.red);
g.fillRect(x-5, y-10, w, 4);
g.setColor(c);
}
}
/**
* 获取坦克剩余复活次数
* @return
*/
public int getLiveCount() {
return liveCount;
}
/**
* 设置坦克剩余复活次数
* @param liveCount
*/
public void setLiveCount(int liveCount) {
this.liveCount = liveCount;
}
/**
* 获取超级子弹剩余数
* @return
*/
public int getSuperMCount() {
return superMCount;
}
/**
* 设置超级子弹剩余数
* @param superMCount
*/
public void setSuperMCount(int superMCount) {
this.superMCount = superMCount;
}
/**
* 吃血块的方法
* @param b
* @return
*/
public boolean eatBlood(Blood b) {
if(this.getRect().intersects(b.getRect()) && this.live && b.isLive()) {
if(this.good) {
b.setLive(false);
life = 100;
tw.score += 200;
if(tw.score/10000*(tw.gameLevel+1)*(tw.gameThrough+1) > (tw.score-20)/10000*(tw.gameLevel+1)*(tw.gameThrough+1))this.liveCount++;
}
return true;
}
return false;
}
/**
* 吃超级子弹的方法
* @param sm
* @return
*/
public boolean eatSuperMissile(SuperMissile sm) {
if(this.getRect().intersects(sm.getRect()) && this.live && sm.isLive()) {
if(this.good) {
sm.setLive(false);
this.superMCount ++;
tw.score += 200;
if(tw.score/10000*(tw.gameLevel+1)*(tw.gameThrough+1) > (tw.score-20)/10000*(tw.gameLevel+1)*(tw.gameThrough+1))this.liveCount++;
}
return true;
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -