Shells=shells.get(i);
if(s.islive&&s.isgood){
s.hitTanks(enemys);
}elseif(s.islive&&!
s.isgood){
s.hitTank(t);
}
s.draw(g);
}
}
//双缓冲,防止游戏地图“频闪”
publicvoidupdate(Graphicsg){
paint(g);
if(bufferImage==null){
bufferImage=this.createImage(MAPWIDTH,MAPHEIGHT);
}
Graphicsgbuffer=bufferImage.getGraphics();
Colorc=gbuffer.getColor();
gbuffer.setColor(Color.GREEN);
gbuffer.fillRect(0,0,MAPWIDTH,MAPHEIGHT);
gbuffer.setColor(Color.RED);
gbuffer.drawString("当前炮弹数目:
"+shells.size(),20,40);
gbuffer.drawString("生命值:
",20,60);
gbuffer.fillRect(65,55,t.getLife(),5);
gbuffer.setColor(c);
paint(gbuffer);
g.drawImage(bufferImage,0,0,null);
}
//内部类,主要通过调用repaint方法,实现绘图功能
classPaintThreadimplementsRunnable{
@Override
publicvoidrun(){
while(true){
repaint();
try{
Thread.sleep(260);
}catch(InterruptedExceptione){
e.printStackTrace();
}
}
}
}
//内部类,键盘监听器
classDirectionHandlerextendsKeyAdapter{
@Override
publicvoidkeyPressed(KeyEventarg0){
t.checkDirection(arg0);
}
}
二.绘制坦克功能:
publicclassTank{
//坦克的尺寸和移动速度
publicstaticfinalintWIDTH=50,HEIGHT=50,xspeed=5,yspeed=5;
//坦克的初始位置
publicintx=400,y=300;
//坦克的初始化方向,STOP即停止,Direction是自定义的枚举常量,见枚举源代码
publicDirectiondirection=Direction.STOP;
//地图
TankMaptm;
//坦克是否存活
booleanisLive=true;
//坦克属于哪一方,true为用户方,false为敌方
booleanisgood=true;
//坦克初始化生命值
intlife=100;
//定义坦克的初始化方向,以便插图坦克的图片
publicintzhuanxiang=1;
//获取和设置存活状况
publicbooleanisLive(){
returnisLive;
}
publicvoidsetLive(booleanisLive){
this.isLive=isLive;
}
//获取和设置生命值
publicintgetLife(){
returnlife;
}
publicvoidsetLife(intlife){
this.life=life;
}
//构造方法
publicTank(){};
publicTank(TankMapt){
tm=t;
}
//绘制坦克
//根据坦克的移动方向,插入不同的坦克的图片
publicvoiddraw(Graphicsg){
if(zhuanxiang==1)
{
ImageIconicon=newImageIcon("Etank_shang.gif");
ImagetankImage=icon.getImage();
g.drawImage(tankImage,x,y,null);
}
elseif(zhuanxiang==-2)
{
ImageIconicon=newImageIcon("Etank_you.gif");
ImagetankImage=icon.getImage();
g.drawImage(tankImage,x,y,null);
}
elseif(zhuanxiang==2)
{
ImageIconicon=newImageIcon("Etank_zuo.gif");
ImagetankImage=icon.getImage();
g.drawImage(tankImage,x,y,null);
}
else
{
ImageIconicon=newImageIcon("Etank_xia.gif");
ImagetankImage=icon.getImage();
g.drawImage(tankImage,x,y,null);
}
/*Colorc=g.getColor();
g.setColor(Color.RED);
g.fillOval(x,y,WIDTH,HEIGHT);
g.setColor(c);*/
move(direction);
}
//根据方向,移动坦克,不允许坦克移出地图尺寸
publicvoidmove(Directiond){
if(d==Direction.STOP){
}elseif(d==Direction.UP){
zhuanxiang=1;
y-=yspeed;
if(y<=20)
y=20;
}elseif(d==Direction.RIGHT){
zhuanxiang=-2;
x+=xspeed;
if(x>=800-WIDTH)
x=800-WIDTH;
}elseif(d==Direction.DOWN){
zhuanxiang=-1;
y+=yspeed;
if(y>=600-HEIGHT)
y=600-HEIGHT;
}elseif(d==Direction.LEFT){
zhuanxiang=2;
x-=xspeed;
if(x<=0)
x=0;
}
}
//确定用户单机的方向键,修正坦克即将移动的方向
publicvoidcheckDirection(KeyEventk){
if(k.getKeyCode()==KeyEvent.VK_UP){
direction=Direction.UP;
}elseif(k.getKeyCode()==KeyEvent.VK_RIGHT){
direction=Direction.RIGHT;
}elseif(k.getKeyCode()==KeyEvent.VK_DOWN){
direction=Direction.DOWN;
}elseif(k.getKeyCode()==KeyEvent.VK_LEFT){
direction=Direction.LEFT;
}
if(k.getKeyCode()==KeyEvent.VK_F){
fire();
}
}
//发射炮弹的方法
publicvoidfire(){
}
//返回坦克当前位置矩形,便于判断是否与敌方坦克或炮弹重叠
publicRectanglegetRec(){
returnnewRectangle(this.x,this.y,this.WIDTH,this.HEIGHT);
}
}
三.敌方坦克的绘制方法
publicclassEnemyTankextendsTank{
//初始化敌方坦克的属性
publicstaticfinalintWIDTH=50,HEIGHT=50,xspeed=5,yspeed=5;
publicintx=400,y=300;
publicDirectiondirection=Direction.DOWN;
publicbooleanislive=true;
Colorcolor=Color.YELLOW;
TankMaptm;
//随机数产生器,供敌方坦克随机产生前进方向使用
Randomr=newRandom();
publicintzhuanxiang;
//方向更改限制,避免敌方坦克每移动一次就更改方向
intrandomCount=r.nextInt(10)+5;
//构造方法
publicEnemyTank(){}
publicEnemyTank(intwx,intwy,Colorc){
x=wx;
y=wy;
color=c;
}
@Override
publicvoiddraw(Graphicsg){
//根据敌方坦克的移动方向插入不同的坦克图片
if(zhuanxiang==1)
{
ImageIconicon=newImageIcon("Etank_shang.gif");
ImagetankImage=icon.getImage();
g.drawImage(tankImage,x,y,null);
}
elseif(zhuanxiang==-2)
{
ImageIconicon=newImageIcon("Etank_you.gif");
ImagetankImage=icon.getImage();
g.drawImage(tankImage,x,y,null);
}
elseif(zhuanxiang==2)
{
ImageIconicon=newImageIcon("Etank_zuo.gif");
ImagetankImage=icon.getImage();
g.drawImage(tankImage,x,y,null);
}
else
{
ImageIconicon=newImageIcon("Etank_xia.gif");
ImagetankImage=icon.getImage();
g.drawImage(tankImage,x,y,null);
}
move();
}
publicvoidmove(){
//当方向更改限制为0时,产生一个随机方向
if(randomCount==0){
Directiondirs[]=Direction.values();
direction=dirs[r.nextInt(dirs.length-1)];
randomCount=r.nextInt(10)+5;
}
//方向更改限制减1
randomCount--;
//随机发射炮弹,当产生的随机数大于35时,发射炮弹
if(r.nextInt(40)>36){
fire();
}
//确定敌方的坦克的移动
if(direction==Direction.UP){
y-=yspeed;
zhuanxiang=1;
if(y<=HEIGHT)
y=HEIGHT;
}elseif(direction==Direction.RIGHT){
x+=xspeed;
zhuanxiang=-2;
if(x>=800-WIDTH)
x=800-WIDTH;
}elseif(direction==Direction.DOWN){
y+=yspeed;
zhuanxiang=-1;
if(y>=600-HEIGHT)
y=600-HEIGHT;
}elseif(direction==Direction.LEFT){
x-=xspeed;
zhuanxiang=2;
if(x<=0)
x=0;
}
}
@Override
publicvoidfire(){
Shell(this.x+WIDTH/2,this.y+HEIGHT/2,this.direction,tm,Color.BLUE,false));
}
publicRectanglegetRec(){
returnnewRectangle(this.x,this.y,this.WIDTH,this.HEIGHT);
}
}
四.炮弹绘制方法
publicclassShell{
//初始化炮弹的基本属性
publicfinalintWIDTH=5,HEIGHT=5,xspeed=14,yspeed=14;
publicintx,y;
Directiondir=Direction.STOP;
publicbooleanislive=true;
TankMaptm=null;
Colorcolor=Color.RED;
booleanisgood=true;
booleanisLive=true;
//构造方法
publicShell(){}
publicShell(intxd,intyd,Directiond,TankMapt){
x=xd;
y=yd;
dir=d;
tm=t;
}
publicShell(intxd,intyd,Directiond,TankMapt,Colorc,booleang){
x=xd;
y=yd;
dir=d;
tm=t;
color=c;
isgood=g;
}
//绘制炮弹
publicvoiddraw(Graphicsg){
if(islive){
Colorc=g.getColor();
g.setColor(color);
g.fillOval(x,y,WIDTH,HEIGHT);
g.setColor(c);
move(dir);
}else{
}
}
//炮弹的移动,如果炮弹移动出了地图尺寸,则设定炮弹死亡
publicvoidmove(Directiond){
if(d==Direction.UP){
y-=yspeed;
if(y<=0)islive=false;
}elseif(d==Direction.RIGHT){
x+=xspeed;
if(x>=800)islive=false;
}elseif(d==Direction.DOWN){
y+=yspeed;
if(y>=600)islive=false;
}elseif(d==Direction.LEFT){
x-=xspeed;
if(x<=0)islive=false;
}
}
publicRectanglegetRec(){
returnnewRectangle(this.x,this.y,this.WIDTH,this.HEIGHT);
}
//打击坦克方法
publicbooleanhitTank(Tankt){
if(this.isLive&&t.isLive&&this.getRec().intersects(t.getRec())){
//用户坦克生命值减20
t.setLife(t.getLife()-20);
//如果用户坦克生命值《=0,游戏结束
if(t.getLife()<=0){
t.setLive(false);
t.direction=Direction.STOP;
JOptionPane.showMessageDialog(tm,"GameOver!
");
System.exit(0);
}
//炮弹死亡
this.isLive=false;
//产生爆炸
Explore=newExplor(x-3,y-3,this.tm);
returntrue;
}
returnfalse;
}
//用户坦克打击敌方坦克的方法
publicboo