Tankt=tanks.get(i);
t.collidesWithWall(w1);
t.collidesWithWall(w2);
t.collidesWithTanks(tanks);
t.draw(g);
}
myTank.draw(g);
myTank.eat(b);
w1.draw(g);
w2.draw(g);
b.draw(g);
}
publicvoidupdate(Graphicsg){
if(offScreenImage==null){
offScreenImage=this.createImage(GAME_WIDTH,GAME_HEIGHT);
}
GraphicsgOffScreen=offScreenImage.getGraphics();
Colorc=gOffScreen.getColor();
gOffScreen.setColor(Color.GREEN);
gOffScreen.fillRect(0,0,GAME_WIDTH,GAME_HEIGHT);
gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreenImage,0,0,null);
}
/**
*本方法显示坦克主窗口
*
*/
publicvoidlauchFrame(){
for(inti=0;i<10;i++){
tanks.add(newTank(50+40*(i+1),50,false,Tank.Direction.D,this));
}
//this.setLocation(400,300);
this.setSize(GAME_WIDTH,GAME_HEIGHT);
this.setTitle("TankWar");
this.addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
});
this.setResizable(false);
this.setBackground(Color.GREEN);
this.addKeyListener(newKeyMonitor());
setVisible(true);
newThread(newPaintThread()).start();
}
publicstaticvoidmain(String[]args){
TankClienttc=newTankClient();
tc.lauchFrame();
}
privateclassPaintThreadimplementsRunnable{
publicvoidrun(){
while(true){
repaint();
try{
Thread.sleep(50);
}catch(InterruptedExceptione){
e.printStackTrace();
}
}
}
}
privateclassKeyMonitorextendsKeyAdapter{
publicvoidkeyReleased(KeyEvente){
myTank.keyReleased(e);
}
publicvoidkeyPressed(KeyEvente){
myTank.keyPressed(e);
}
}
}
packagecom.bjsxt.tank;
importjava.awt.*;
importjava.awt.event.*;
importjava.util.*;
publicclassTank{
publicstaticfinalintXSPEED=5;
publicstaticfinalintYSPEED=5;
publicstaticfinalintWIDTH=30;
publicstaticfinalintHEIGHT=30;
privatebooleanlive=true;
privateBloodBarbb=newBloodBar();
privateintlife=100;
TankClienttc;
privatebooleangood;
privateintx,y;
privateintoldX,oldY;
privatestaticRandomr=newRandom();
privatebooleanbL=false,bU=false,bR=false,bD=false;
enumDirection{L,LU,U,RU,R,RD,D,LD,STOP};
privateDirectiondir=Direction.STOP;
privateDirectionptDir=Direction.D;
privateintstep=r.nextInt(12)+3;
publicTank(intx,inty,booleangood){
this.x=x;
this.y=y;
this.oldX=x;
this.oldY=y;
this.good=good;
}
publicTank(intx,inty,booleangood,Directiondir,TankClienttc){
this(x,y,good);
this.dir=dir;
this.tc=tc;
}
publicvoiddraw(Graphicsg){
if(!
live){
if(!
good){
tc.tanks.remove(this);
}
return;
}
Colorc=g.getColor();
if(good)g.setColor(Color.RED);
elseg.setColor(Color.BLUE);
g.fillOval(x,y,WIDTH,HEIGHT);
g.setColor(c);
if(good)bb.draw(g);
switch(ptDir){
caseL:
g.drawLine(x+Tank.WIDTH/2,y+Tank.HEIGHT/2,x,y+Tank.HEIGHT/2);
break;
caseLU:
g.drawLine(x+Tank.WIDTH/2,y+Tank.HEIGHT/2,x,y);
break;
caseU:
g.drawLine(x+Tank.WIDTH/2,y+Tank.HEIGHT/2,x+Tank.