📄 tank.java
字号:
package com.wuxiaohong.tank;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.Random;
import java.util.List;
public class Tank {
public static final int WIDTH = 30;
public static final int HEIGHT = 30;
private int x,y;
private int oldx,oldy;
private int XSPEED = 5;
private int YSPEED = 5;
private boolean bL=false,bU=false,bR = false,bD = false;
enum direction {L,LU,U,RU,R,RD,D,LD,STOP};
private direction dir = direction.STOP;
TankClient tc;
private direction ptDir = direction.D;
private boolean good = true;
public boolean live = true;
public static Random rm= new Random();
private int step =rm.nextInt(12)+3;
private int life = 100;
private BooldBar booldBar = new BooldBar();
public Tank(int x, int y,boolean good) {
this.x = x;
this.y = y;
this.good = good;
}
public Tank(int x,int y,boolean good,direction dir,TankClient tc)
{
this(x,y,good);
this.tc = tc;
this.dir = dir;
}
public void draw(Graphics g)
{
if(!live)
{
if(!good) tc.tanks.remove(this);
return;
}
Color c = g.getColor();
if(good==true)g.setColor(Color.RED);
else g.setColor(Color.BLUE);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
if(good)
{
booldBar.draw(g);
}
move();
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()
{
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 += XSPEED;
break;
case LD:
x -= XSPEED;
y += YSPEED;
break;
case STOP:
break;
}
if(x<0)x =0;
if(y<30)y=30;
if(x+Tank.WIDTH>TankClient.GAME_WITH)x=TankClient.GAME_WITH-Tank.WIDTH;
if(y+Tank.HEIGHT>TankClient.GAME_HIGHT)y=TankClient.GAME_HIGHT-Tank.HEIGHT;
if(!good)
{
direction dire[] = direction.values();
if(step==0)
{
step = rm.nextInt(12)+3;
int rmn = rm.nextInt(dire.length);
dir = dire[rmn];
}
step--;
if(rm.nextInt(40)>37)this.fire();
}
}
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if(key == e.VK_RIGHT)
{
bR =true;
}
else if(key == e.VK_LEFT)
{
bL = true;
}
else if(key == e.VK_UP)
{
bU = true;
}
else if(key == e.VK_DOWN)
{
bD= true;
}
localdirection();
}
public void localdirection()
{
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;
}
if(dir!=direction.STOP)
{
this.ptDir = this.dir;
}
}
public void keyRealse(KeyEvent e) {
int key = e.getKeyCode();
if(key == e.VK_RIGHT)
{
bR =false;
}
else if(key == e.VK_LEFT)
{
bL = false;
}
else if(key == e.VK_UP)
{
bU = false;
}
else if(key == e.VK_DOWN)
{
bD= false;
}
else if(key == e.VK_CONTROL)
{
if(this.live)fire();
}
else if((key==e.VK_A))
{
superFire();
}
else if(key==e.VK_F2)
{
this.live = true;
this.life =100;
}
localdirection();
}
private Missile fire() {
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,good,ptDir,tc);
tc.missiles.add(m);
return m;
}
public Rectangle Rect()
{
return new Rectangle(x,y,WIDTH,HEIGHT);
}
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
public boolean isGood() {
return good;
}
public void stay()
{
x = this.oldx;
y = this.oldy;
}
/**
* 撞墙
* @param w被撞的墙
* @return撞上了返回TURE,否则FALSE
*/
public boolean hitWall(Wall w)
{
if(this.Rect().intersects(w.getRect()))
{
stay();
return true;
}
return false;
}
public boolean hitTank(List<Tank> t)
{
for(int i = 0 ;i<t.size();i++)
{
Tank tank = t.get(i);
if(this.live&&tank.isLive()&&this.Rect().intersects(tank.Rect()))
{
//tank.stay();
//this.stay();
return true;
}
}
return false;
}
public void superFire()
{
direction dirs[] = direction.values();
for(int i = 0;i<8;i++)
{
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,good,dirs[i],tc);
tc.missiles.add(m);
}
}
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
private class BooldBar
{
public void draw(Graphics g)
{
Color c = g.getColor();
g.drawRect(x, y-10, WIDTH, 10);
int leftBoold = WIDTH*(life/100);
g.setColor(Color.RED);
g.fillRect(x, y-10,leftBoold, 10);
g.setColor(c);
}
}
public void eatBoold(Boold t)
{
if(t.isLive()&&this.live&&this.Rect().intersects(t.getRect()))
{
this.life =100;
t.setLive(false);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -