📄 tank.java
字号:
package com.bjsxt.tank;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.*;
import javax.naming.directory.DirContext;
/**
*
* @坦克类
*
*/
public class Tank {
private static Random r = new Random();
private int step = r.nextInt(15) + 5;
int x,y;//坐标
int oldX,oldY;//记录上一步坐标
public static final int WI = 30;//坦克长
public static final int HE = 30;//坦克宽
public static final int XSPEED = 5;//坦克X坐标移动速度
public static final int YSPEED = 5;//坦克Y坐标移动速度
private boolean bU = false,bR = false,bD = false,bL = false;
private boolean good;//区分敌我
private boolean live = true;
private int life = 100;
private lifebar lb = new lifebar();
enum Direction {U,RU,R,RD,D,LD,L,LU,STOP};
Direction[] dirs;
private Direction ptDir = Direction.D;
TankClient tc; //用于持有对方引用
private Direction dir =Direction.STOP ;
/**
* 坦克构造函数1
* @param x X坐标
* @param y Y坐标
* @param good 敌或我
*/
public Tank(int x, int y,boolean good) {
this.x = x;
this.y = y;
this.oldX = x;
this.oldY = y;
this.good = good;
}
/**
*
* @param x
* @param y
* @param good
* @param tc
* @param dir
*/
public Tank(int x, int y, boolean good,TankClient tc,Direction dir) { //方便Tank访问TankClient中的变量
this(x,y,good);
this.tc = tc;
this.dir = dir;
}
public void draw(Graphics g){ //画出tank
if(!live)return;
Color c = g.getColor();
if(good)g.setColor(Color.RED);
else g.setColor(Color.PINK);
if(good)lb.draw(g);
g.fillOval(x, y, WI, HE);
g.setColor(c);
switch(ptDir){ //用一直线模拟炮筒,画出其方向
case U:
g.drawLine(x + Tank.WI/2,y + Tank.HE/2,x + Tank.WI/2,y);
break;
case RU:
g.drawLine(x + Tank.WI/2,y + Tank.HE/2,x + Tank.WI,y);
break;
case R:
g.drawLine(x + Tank.WI/2,y + Tank.HE/2,x + Tank.WI,y + Tank.HE/2);
break;
case RD:
g.drawLine(x + Tank.WI/2,y + Tank.HE/2,x + Tank.WI,y + Tank.HE);
break;
case D:
g.drawLine(x + Tank.WI/2,y + Tank.HE/2,x + Tank.WI/2,y + Tank.HE);
break;
case LD:
g.drawLine(x + Tank.WI/2,y + Tank.HE/2,x,y + Tank.HE);
break;
case L:
g.drawLine(x + Tank.WI/2,y + Tank.HE/2,x,y + Tank.HE/2);
break;
case LU:
g.drawLine(x + Tank.WI/2,y + Tank.HE/2,x,y);
break;
case STOP:
break;
}
if(dir != Direction.STOP){
ptDir = this.dir;
}
move();
}
public Missile fire(){ //利用跑同方向 开炮方法
if(!this.isLive())return null;
int x = this.x + Tank.WI/2 - Missile.WI/2;
int y = this.y + Tank.HE/2 - Missile.HE/2;
Missile m = new Missile(x,y,good,ptDir,tc);
tc.missiles.add(m);
return m;
}
public Missile dfire(Direction dir){ //往某个方向打出炮弹
if(!this.isLive())return null;
int x = this.x + Tank.WI/2 - Missile.WI/2;
int y = this.y + Tank.HE/2 - Missile.HE/2;
Missile m = new Missile(x,y,good,dir,tc);
tc.missiles.add(m);
return m;
}
public void superFire(){
Direction[] dirs = Direction.values();
for(int i=0;i < 8;i++){
dfire(dirs[i]);
}
}
public void keyreleased(KeyEvent e){//当键盘按下
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_UP:
bU = false;
break;
case KeyEvent.VK_RIGHT:
bR = false;
break;
case KeyEvent.VK_DOWN:
bD = false;
break;
case KeyEvent.VK_LEFT:
bL = false;
break;
case KeyEvent.VK_CONTROL:
tc.m = fire();
break;
case KeyEvent.VK_A:
superFire();
break;
}
}
public void keypressed(KeyEvent e){//当键盘抬起
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_UP:
bU = true;
break;
case KeyEvent.VK_RIGHT:
bR = true;
break;
case KeyEvent.VK_DOWN:
bD = true;
break;
case KeyEvent.VK_LEFT:
bL = true;
break;
case KeyEvent.VK_F2:
if(!this.live){
this.life = 100;
this.live = true;
}
break;
}
Direct();
}
public void Direct() {//定义8个方向和一个stop状态
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 dir = Direction.STOP;
}
public void move(){ //移动
this.oldX = x;
this.oldY = y;
switch(dir){
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 L:
x -= XSPEED;
break;
case LU:
x -= XSPEED;
y -= YSPEED;
break;
case STOP:
break;
}
if(x<0) x=0;
if(y<26) y=26;
if(x + Tank.WI>TankClient.GAME_WIDTH)x=TankClient.GAME_WIDTH - Tank.WI;
if(y + Tank.HE>TankClient.GAME_HEIGHT)y=TankClient.GAME_HEIGHT - Tank.HE;
if(!good){ //使敌方坦克随机移动
dirs = Direction.values();
if(step == 0){
step = r.nextInt(15) + 5;
int rn = r.nextInt(dirs.length);
dir = dirs[rn];
}
if(r.nextInt(200) > 190)fire();//用来调整敌军坦克子弹密度
step --;
}
}
public Rectangle getRect(){
return new Rectangle(x,y,WI,HE);
}
public void stay(){
x = oldX;
y = oldY;
}
public boolean tHitWall(Wall w){
if(this.live && this.getRect().intersects(w.getRect())){
this.stay();
return true;
}
return false;
}
public boolean tHitTank(List<Tank> tanks){
for(int i = 0;i < tanks.size();i++){
Tank t = tanks.get(i);
if( this != t){
if(this.live && this.getRect().intersects(t.getRect())){
this.stay();
return true;
}
}
}
return false;
}
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
public boolean isGood() {
return good;
}
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
private class lifebar {
public void draw(Graphics g){
Color c = g.getColor();
g.setColor(Color.GREEN);
g.drawRect(x, y-10, WI, 5);
int w = WI * life/100;
g.fillRect(x, y-10, w, 5);
g.setColor(c);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -