坦克大战源代码终极版.docx
《坦克大战源代码终极版.docx》由会员分享,可在线阅读,更多相关《坦克大战源代码终极版.docx(28页珍藏版)》请在冰点文库上搜索。
坦克大战源代码终极版
packagecom.tankgame4;
importjava.util.Vector;
//TANK
classTank
{
publicvoidsetDirect(intdirect){
this.direct=direct;
}
publicintgetDirect(){
returndirect;
publicintgetX(){
returnx;
publicvoidsetX(intx){
this.x=x;
publicintgetY(){
}returny;
intx=0;
inty=0;
intcolor=0;
intspeed=1;
intdirect=0;
booleanisLive=true;
publicTank(intx,inty)
this.y=y;
publicvoidsetY(inty){
}this.y=y;
//MYTANK
classHeroextendsTank
Shotshot=null;
Vectorshotm=newVector();
publicHero(intx,inty)
super(x,y);
//坦克具有一个打击敌人的方法
publicvoidshotenemy(intx,inty,intdirect)
switch(direct)
case0:
shot=newShot(this.x+10,this.y,0);
shotm.add(shot);
break;
this.color=5;
publicintgetColor(){
returncolor;
publicvoidsetColor(intcolor){
}this.color=color;
case1:
shot=newShot(this.x+30,this.y+10,1);
shot=newShot(this.x+10,this.y+30,2);
shot=newShot(this.x,this.y+10,3);
case2:
case3:
}}}
Threadth=newThread(shot);
th.start();
//调整速度
publicvoidmoveup()
}y-=speed;
publicvoidmoveright()
}x+=speed;
publicvoidmovedown()
y+=speed;
publicvoidmoveleft()
}x-=speed;
//敌人的坦克
classEnemyTankextendsTankimplementsRunnable
Vectorensh=newVector();
Vectorets=newVector();
publicEnemyTank(intx,inty)
//获取MPanel上的敌人坦克
publicvoidsetets(Vectorvv)
}this.ets=vv;
this.setColor
(2);
this.setDirect
//判断敌人的坦克是否碰撞
publicbooleanisTouch()
booleanb=false;
EnemyTanket=null;
for(inti=0;i{et=ets.get(i);if(et!=this){if(et.direct==0||et.direct==2){if(this.x>=et.x&&this.x<=et.x+20&&this.y<=et.y+30&&this.y>et.y){}returntrue;if(this.x+20>=et.x&&this.x+20<=et.x+20&&this.y<=et.y+30&&this.y>et.y){returntrue;}}if(et.direct==1||et.direct==3){if(this.x>=et.x&&this.x<=et.x+30&&this.y<=et.y+20&&this.y>et.y)}}{}returntrue;if(this.x+20>=et.x&&this.x+20<=et.x+30&&this.y<=et.y+20&&this.y>et.y)}{}returntrue;break;case1:for(inti=0;i{et=ets.get(i);if(et!=this){if(et.direct==0||et.direct==2){if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y<=et.y+30&&this.y>et.y){}returntrue;if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y+20<=et.y+30&&this.y+20>et.y)}{}returntrue;if(et.direct==1||et.direct==3){if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y<=et.y+20&&this.y>et.y){returntrue;}if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y+20<=et.y+20&&this.y+20>et.y)}}}{}returntrue;break;case2:for(inti=0;i{et=ets.get(i);if(et!=this){if(et.direct==0||et.direct==2){if(this.x>=et.x&&this.x<=et.x+20&&this.y+30<=et.y+30&&this.y+30>et.y){}returntrue;if(this.x+20>=et.x&&this.x+20<=et.x+20&&this.y+30<=et.y+30&&this.y+30>et.y)}if(et.direct==1||et.direct==3){{}returntrue;if(this.x>=et.x&&this.x<=et.x+30&&this.y+30<=et.y+20&&this.y+30>et.y){}returntrue;if(this.x+20>=et.x&&this.x+20<=et.x+30&&this.y+30<=et.y+20&&this.y+30>et.y){returntrue;}}}}break;case3:for(inti=0;i{et=ets.get(i);if(et!=this){{}if(et.direct==0||et.direct==2){if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y<=et.y+30&&this.y>et.y)returntrue;if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y+20<=et.y+30&&this.y+20>et.y){returntrue;}}if(et.direct==1||et.direct==3){if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y<=et.y+20&&this.y>et.y){}returntrue;if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y+20<=et.y+20&&this.y+20>et.y)}}{}returntrue;}}break;returnb;}publicvoidrun(){while(true){switch(this.direct){case0:for(inti=0;i<30;i++){if(y>0&&this.isTouch()==false)y-=this.speed;try{}Thread.sleep(50);//TODOAuto-generatedcatchblocke.printStackTrace();}catch(InterruptedExceptione){}break;case1:for(inti=0;i<30;i++){if(x<365&&this.isTouch()==false)x+=this.speed;try{Thread.sleep(50);}catch(InterruptedExceptione){}//TODOAuto-generatedcatchblocke.printStackTrace();}break;case2:for(inti=0;i<30;i++){}if(y<270&&this.isTouch()==false)y+=this.speed;try{}Thread.sleep(50);//TODOAuto-generatedcatchblocke.printStackTrace();}catch(InterruptedExceptione){break;case3:}this.direct=(int)(Math.random()*4);if(this.isLive==false){for(inti=0;i<30;i++){if(x>0&&this.isTouch()==false)x-=this.speed;}break;try{Thread.sleep(50);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblock}e.printStackTrace();}}}break;}if(ensh.size()<5){}Shotes=null;switch(this.direct){case0:es=newShot(this.getX()+10,this.getY(),0);ensh.add(es);break;case1:es=newShot(this.getX()+30,this.getY()+10,1);ensh.add(es);break;case2:es=newShot(this.getX()+10,this.getY()+30,2);ensh.add(es);}break;es=newShot(this.getX(),this.getY()+10,3);ensh.add(es);break;case3:Threadth=newThread(es);th.start();//炸弹类classBomb{intx;inty;intlift=9;booleanisLive=true;publicBomb(intx,inty){this.x=x;this.y=y;}//炸弹的生命值publicvoidliftdown(){}if(lift>0){lift--;}else{}isLive=false;}//子弹类classShotimplementsRunnable{intshotX;intshotY;intdirect;intshotspeed=1;booleanisLive=true;publicShot(intx,inty,intdirect){}this.shotX=x;this.shotY=y;this.direct=direct;publicintgetShotX(){}publicvoidsetShotX(intshotX){}publicintgetShotY(){this.shotX=shotX;returnshotX;}returnshotY;publicvoidsetShotY(intshotY){this.shotY=shotY;}publicintgetShotspeed(){returnshotspeed;}publicvoidsetShotspeed(intshotspeed){}this.shotspeed=shotspeed;publicvoidrun(){while(true){try{Thread.sleep(50);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}switch(direct){case0:shotY-=shotspeed;break;case1:shotX+=shotspeed;break;case2:shotY+=shotspeed;break;case3:shotX-=shotspeed;}if(shotX<0||shotX>400||shotY<0||shotY>300){break;}}}}isLive=false;break;/***功能:坦克大战4.0*/packagecom.tankgame4;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.KeyEvent;importjava.awt.event.KeyListener;importjava.util.Vector;importjava.io.*;importjavax.swing.*;importjavax.imageio.*;publicclassMyTankGame4extendsJFrameimplementsActionListener{MyPanelmp=null;MyStartPanelmsp=null;//菜单定义JMenuBarjmb=null;JMenujm1=null;JMenuItemjmi1=null;publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubMyTankGame4mtg=newMyTankGame4();}//构造函数}publicMyTankGame4(){}//创建菜单jmb=newJMenuBar();jm1=newJMenu("Game(G)");jm1.setMnemonic('G');jmi1=newJMenuItem("NewGame(N)");jmi1.setMnemonic('N');jmi1.addActionListener(this);jmi1.setActionCommand("NewGame");jm1.add(jmi1);jmb.add(jm1);this.setJMenuBar(jmb);msp=newMyStartPanel();Threadst=newThread(msp);st.start();this.add(msp);this.setTitle("坦克大战");this.setSize(400,300);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);publicvoidactionPerformed(ActionEventarg0){if(arg0.getActionCommand().equals("NewGame"))}{}this.remove(msp);mp=newMyPanel();Threadmt=newThread(mp);mt.start();this.add(mp);this.addKeyListener(mp);this.setVisible(true);//游戏开始面板classMyStartPanelextendsJPanelimplementsRunnable{}publicvoidrun(){}while(true){try{Thread.sleep(300);}times++;}catch(InterruptedExceptione){}//TODOAuto-generatedcatchblocke.printStackTrace();inttimes=0;publicvoidpaint(Graphicsg){}super.paint(g);g.fillRect(0,0,400,300);if(times%2==0){Fontmyfont=newFont("行楷",Font.BOLD,30);g.setFont(myfont);g.setColor(Color.yellow);g.drawString("第一关",130,130);}this.repaint();//我的面板classMyPanelextendsJPanelimplementsKeyListener,Runnable{//定义一个我的坦克Herohero=null;EnemyTanket=null;Imageimage1=null;Imageimage2=null;Imageimage3=null;Imageim=null;Vectorbombs=newVector();Vectoretm=newVector();intensize=5;publicvoidpaint(Graphicsg){super.paint(g);g.fillRect(0,0,400,300);g.drawImage(im,0,0,400,300,this);//画出自己的坦克if(hero.isLive==true){}this.drawTank(this.hero.getX(),this.hero.getY(),g,this.hero.getDirect(),this.hero.getColor());//画出子弹for(inti=0;i{Shotmyshot=hero.shotm.get(i);}if(myshot!=null&&myshot.isLive==true){}if(myshot.isLive==false)hero.shotm.remove(myshot);g.fill3DRect(myshot.getShotX(),myshot.getShotY(),2,2,false);//画出炸弹for(inti=0;i{Bombb=bombs.get(i);if(b.lift>6){g.drawImage(image1,b.x,b.y,30,30,this);}elseif(b.lift>3)//}{g.drawImage(image2,b.x,b.y,30,30,this);}else{g.drawImage(image3,b.x,b.y,30,30,this);}b.liftdown();if(b.lift==0)bombs.remove(b);//画出敌人的坦克for(inti=0;i{et=etm.get(i);if(et!=null&&et.isLive==true){}this.drawTank(et.getX(),et.getY(),g,et.getDirect(),et.getColor());//画出敌人的子弹for(intj=0;j{Shotenshot=et.ensh.get(j);if(enshot.isLive==true){g.fill3DRect(enshot.getShotX(),enshot.getShotY(),2,2,false);System.out.println("第"+i+"辆坦克的第"+j+"颗子弹的Y="+enshot.getShotY());}else}//判断子弹是否击中坦克的函数publicvoidhitTank(Shots,Tankt)}}{}et.ensh.remove(enshot);{}//画出坦克的函数publicvoiddrawTank(intxx,intyy,Graphicsg,intdirect,inttype){//判断什么颜色类型的坦克switch(type){case0:g.set
et=ets.get(i);
if(et!
=this)
if(et.direct==0||et.direct==2)
if(this.x>=et.x&&this.x<=et.x+20&&this.y<=et.y+30&&this.y>et.y)
}returntrue;
if(this.x+20>=et.x&&this.x+20<=et.x+20&&this.y<=et.y+30&&this.y>et.y)
returntrue;
if(et.direct==1||et.direct==3)
if(this.x>=et.x&&this.x<=et.x+30&&this.y<=et.y+20&&this.y>et.y)
}}{
if(this.x+20>=et.x&&this.x+20<=et.x+30&&this.y<=et.y+20&&this.y>et.y)
}{
for(inti=0;i{et=ets.get(i);if(et!=this){if(et.direct==0||et.direct==2){if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y<=et.y+30&&this.y>et.y){}returntrue;if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y+20<=et.y+30&&this.y+20>et.y)}{}returntrue;if(et.direct==1||et.direct==3){if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y<=et.y+20&&this.y>et.y){returntrue;}if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y+20<=et.y+20&&this.y+20>et.y)}}}{}returntrue;break;case2:for(inti=0;i{et=ets.get(i);if(et!=this){if(et.direct==0||et.direct==2){if(this.x>=et.x&&this.x<=et.x+20&&this.y+30<=et.y+30&&this.y+30>et.y){}returntrue;if(this.x+20>=et.x&&this.x+20<=et.x+20&&this.y+30<=et.y+30&&this.y+30>et.y)}if(et.direct==1||et.direct==3){{}returntrue;if(this.x>=et.x&&this.x<=et.x+30&&this.y+30<=et.y+20&&this.y+30>et.y){}returntrue;if(this.x+20>=et.x&&this.x+20<=et.x+30&&this.y+30<=et.y+20&&this.y+30>et.y){returntrue;}}}}break;case3:for(inti=0;i{et=ets.get(i);if(et!=this){{}if(et.direct==0||et.direct==2){if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y<=et.y+30&&this.y>et.y)returntrue;if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y+20<=et.y+30&&this.y+20>et.y){returntrue;}}if(et.direct==1||et.direct==3){if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y<=et.y+20&&this.y>et.y){}returntrue;if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y+20<=et.y+20&&this.y+20>et.y)}}{}returntrue;}}break;returnb;}publicvoidrun(){while(true){switch(this.direct){case0:for(inti=0;i<30;i++){if(y>0&&this.isTouch()==false)y-=this.speed;try{}Thread.sleep(50);//TODOAuto-generatedcatchblocke.printStackTrace();}catch(InterruptedExceptione){}break;case1:for(inti=0;i<30;i++){if(x<365&&this.isTouch()==false)x+=this.speed;try{Thread.sleep(50);}catch(InterruptedExceptione){}//TODOAuto-generatedcatchblocke.printStackTrace();}break;case2:for(inti=0;i<30;i++){}if(y<270&&this.isTouch()==false)y+=this.speed;try{}Thread.sleep(50);//TODOAuto-generatedcatchblocke.printStackTrace();}catch(InterruptedExceptione){break;case3:}this.direct=(int)(Math.random()*4);if(this.isLive==false){for(inti=0;i<30;i++){if(x>0&&this.isTouch()==false)x-=this.speed;}break;try{Thread.sleep(50);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblock}e.printStackTrace();}}}break;}if(ensh.size()<5){}Shotes=null;switch(this.direct){case0:es=newShot(this.getX()+10,this.getY(),0);ensh.add(es);break;case1:es=newShot(this.getX()+30,this.getY()+10,1);ensh.add(es);break;case2:es=newShot(this.getX()+10,this.getY()+30,2);ensh.add(es);}break;es=newShot(this.getX(),this.getY()+10,3);ensh.add(es);break;case3:Threadth=newThread(es);th.start();//炸弹类classBomb{intx;inty;intlift=9;booleanisLive=true;publicBomb(intx,inty){this.x=x;this.y=y;}//炸弹的生命值publicvoidliftdown(){}if(lift>0){lift--;}else{}isLive=false;}//子弹类classShotimplementsRunnable{intshotX;intshotY;intdirect;intshotspeed=1;booleanisLive=true;publicShot(intx,inty,intdirect){}this.shotX=x;this.shotY=y;this.direct=direct;publicintgetShotX(){}publicvoidsetShotX(intshotX){}publicintgetShotY(){this.shotX=shotX;returnshotX;}returnshotY;publicvoidsetShotY(intshotY){this.shotY=shotY;}publicintgetShotspeed(){returnshotspeed;}publicvoidsetShotspeed(intshotspeed){}this.shotspeed=shotspeed;publicvoidrun(){while(true){try{Thread.sleep(50);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}switch(direct){case0:shotY-=shotspeed;break;case1:shotX+=shotspeed;break;case2:shotY+=shotspeed;break;case3:shotX-=shotspeed;}if(shotX<0||shotX>400||shotY<0||shotY>300){break;}}}}isLive=false;break;/***功能:坦克大战4.0*/packagecom.tankgame4;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.KeyEvent;importjava.awt.event.KeyListener;importjava.util.Vector;importjava.io.*;importjavax.swing.*;importjavax.imageio.*;publicclassMyTankGame4extendsJFrameimplementsActionListener{MyPanelmp=null;MyStartPanelmsp=null;//菜单定义JMenuBarjmb=null;JMenujm1=null;JMenuItemjmi1=null;publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubMyTankGame4mtg=newMyTankGame4();}//构造函数}publicMyTankGame4(){}//创建菜单jmb=newJMenuBar();jm1=newJMenu("Game(G)");jm1.setMnemonic('G');jmi1=newJMenuItem("NewGame(N)");jmi1.setMnemonic('N');jmi1.addActionListener(this);jmi1.setActionCommand("NewGame");jm1.add(jmi1);jmb.add(jm1);this.setJMenuBar(jmb);msp=newMyStartPanel();Threadst=newThread(msp);st.start();this.add(msp);this.setTitle("坦克大战");this.setSize(400,300);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);publicvoidactionPerformed(ActionEventarg0){if(arg0.getActionCommand().equals("NewGame"))}{}this.remove(msp);mp=newMyPanel();Threadmt=newThread(mp);mt.start();this.add(mp);this.addKeyListener(mp);this.setVisible(true);//游戏开始面板classMyStartPanelextendsJPanelimplementsRunnable{}publicvoidrun(){}while(true){try{Thread.sleep(300);}times++;}catch(InterruptedExceptione){}//TODOAuto-generatedcatchblocke.printStackTrace();inttimes=0;publicvoidpaint(Graphicsg){}super.paint(g);g.fillRect(0,0,400,300);if(times%2==0){Fontmyfont=newFont("行楷",Font.BOLD,30);g.setFont(myfont);g.setColor(Color.yellow);g.drawString("第一关",130,130);}this.repaint();//我的面板classMyPanelextendsJPanelimplementsKeyListener,Runnable{//定义一个我的坦克Herohero=null;EnemyTanket=null;Imageimage1=null;Imageimage2=null;Imageimage3=null;Imageim=null;Vectorbombs=newVector();Vectoretm=newVector();intensize=5;publicvoidpaint(Graphicsg){super.paint(g);g.fillRect(0,0,400,300);g.drawImage(im,0,0,400,300,this);//画出自己的坦克if(hero.isLive==true){}this.drawTank(this.hero.getX(),this.hero.getY(),g,this.hero.getDirect(),this.hero.getColor());//画出子弹for(inti=0;i{Shotmyshot=hero.shotm.get(i);}if(myshot!=null&&myshot.isLive==true){}if(myshot.isLive==false)hero.shotm.remove(myshot);g.fill3DRect(myshot.getShotX(),myshot.getShotY(),2,2,false);//画出炸弹for(inti=0;i{Bombb=bombs.get(i);if(b.lift>6){g.drawImage(image1,b.x,b.y,30,30,this);}elseif(b.lift>3)//}{g.drawImage(image2,b.x,b.y,30,30,this);}else{g.drawImage(image3,b.x,b.y,30,30,this);}b.liftdown();if(b.lift==0)bombs.remove(b);//画出敌人的坦克for(inti=0;i{et=etm.get(i);if(et!=null&&et.isLive==true){}this.drawTank(et.getX(),et.getY(),g,et.getDirect(),et.getColor());//画出敌人的子弹for(intj=0;j{Shotenshot=et.ensh.get(j);if(enshot.isLive==true){g.fill3DRect(enshot.getShotX(),enshot.getShotY(),2,2,false);System.out.println("第"+i+"辆坦克的第"+j+"颗子弹的Y="+enshot.getShotY());}else}//判断子弹是否击中坦克的函数publicvoidhitTank(Shots,Tankt)}}{}et.ensh.remove(enshot);{}//画出坦克的函数publicvoiddrawTank(intxx,intyy,Graphicsg,intdirect,inttype){//判断什么颜色类型的坦克switch(type){case0:g.set
if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y<=et.y+30&&this.y>et.y)
if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y+20<=et.y+30&&this.y+20>et.y)
if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y<=et.y+20&&this.y>et.y)
if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y+20<=et.y+20&&this.y+20>et.y)
}}}{
for(inti=0;i{et=ets.get(i);if(et!=this){if(et.direct==0||et.direct==2){if(this.x>=et.x&&this.x<=et.x+20&&this.y+30<=et.y+30&&this.y+30>et.y){}returntrue;if(this.x+20>=et.x&&this.x+20<=et.x+20&&this.y+30<=et.y+30&&this.y+30>et.y)}if(et.direct==1||et.direct==3){{}returntrue;if(this.x>=et.x&&this.x<=et.x+30&&this.y+30<=et.y+20&&this.y+30>et.y){}returntrue;if(this.x+20>=et.x&&this.x+20<=et.x+30&&this.y+30<=et.y+20&&this.y+30>et.y){returntrue;}}}}break;case3:for(inti=0;i{et=ets.get(i);if(et!=this){{}if(et.direct==0||et.direct==2){if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y<=et.y+30&&this.y>et.y)returntrue;if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y+20<=et.y+30&&this.y+20>et.y){returntrue;}}if(et.direct==1||et.direct==3){if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y<=et.y+20&&this.y>et.y){}returntrue;if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y+20<=et.y+20&&this.y+20>et.y)}}{}returntrue;}}break;returnb;}publicvoidrun(){while(true){switch(this.direct){case0:for(inti=0;i<30;i++){if(y>0&&this.isTouch()==false)y-=this.speed;try{}Thread.sleep(50);//TODOAuto-generatedcatchblocke.printStackTrace();}catch(InterruptedExceptione){}break;case1:for(inti=0;i<30;i++){if(x<365&&this.isTouch()==false)x+=this.speed;try{Thread.sleep(50);}catch(InterruptedExceptione){}//TODOAuto-generatedcatchblocke.printStackTrace();}break;case2:for(inti=0;i<30;i++){}if(y<270&&this.isTouch()==false)y+=this.speed;try{}Thread.sleep(50);//TODOAuto-generatedcatchblocke.printStackTrace();}catch(InterruptedExceptione){break;case3:}this.direct=(int)(Math.random()*4);if(this.isLive==false){for(inti=0;i<30;i++){if(x>0&&this.isTouch()==false)x-=this.speed;}break;try{Thread.sleep(50);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblock}e.printStackTrace();}}}break;}if(ensh.size()<5){}Shotes=null;switch(this.direct){case0:es=newShot(this.getX()+10,this.getY(),0);ensh.add(es);break;case1:es=newShot(this.getX()+30,this.getY()+10,1);ensh.add(es);break;case2:es=newShot(this.getX()+10,this.getY()+30,2);ensh.add(es);}break;es=newShot(this.getX(),this.getY()+10,3);ensh.add(es);break;case3:Threadth=newThread(es);th.start();//炸弹类classBomb{intx;inty;intlift=9;booleanisLive=true;publicBomb(intx,inty){this.x=x;this.y=y;}//炸弹的生命值publicvoidliftdown(){}if(lift>0){lift--;}else{}isLive=false;}//子弹类classShotimplementsRunnable{intshotX;intshotY;intdirect;intshotspeed=1;booleanisLive=true;publicShot(intx,inty,intdirect){}this.shotX=x;this.shotY=y;this.direct=direct;publicintgetShotX(){}publicvoidsetShotX(intshotX){}publicintgetShotY(){this.shotX=shotX;returnshotX;}returnshotY;publicvoidsetShotY(intshotY){this.shotY=shotY;}publicintgetShotspeed(){returnshotspeed;}publicvoidsetShotspeed(intshotspeed){}this.shotspeed=shotspeed;publicvoidrun(){while(true){try{Thread.sleep(50);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}switch(direct){case0:shotY-=shotspeed;break;case1:shotX+=shotspeed;break;case2:shotY+=shotspeed;break;case3:shotX-=shotspeed;}if(shotX<0||shotX>400||shotY<0||shotY>300){break;}}}}isLive=false;break;/***功能:坦克大战4.0*/packagecom.tankgame4;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.KeyEvent;importjava.awt.event.KeyListener;importjava.util.Vector;importjava.io.*;importjavax.swing.*;importjavax.imageio.*;publicclassMyTankGame4extendsJFrameimplementsActionListener{MyPanelmp=null;MyStartPanelmsp=null;//菜单定义JMenuBarjmb=null;JMenujm1=null;JMenuItemjmi1=null;publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubMyTankGame4mtg=newMyTankGame4();}//构造函数}publicMyTankGame4(){}//创建菜单jmb=newJMenuBar();jm1=newJMenu("Game(G)");jm1.setMnemonic('G');jmi1=newJMenuItem("NewGame(N)");jmi1.setMnemonic('N');jmi1.addActionListener(this);jmi1.setActionCommand("NewGame");jm1.add(jmi1);jmb.add(jm1);this.setJMenuBar(jmb);msp=newMyStartPanel();Threadst=newThread(msp);st.start();this.add(msp);this.setTitle("坦克大战");this.setSize(400,300);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);publicvoidactionPerformed(ActionEventarg0){if(arg0.getActionCommand().equals("NewGame"))}{}this.remove(msp);mp=newMyPanel();Threadmt=newThread(mp);mt.start();this.add(mp);this.addKeyListener(mp);this.setVisible(true);//游戏开始面板classMyStartPanelextendsJPanelimplementsRunnable{}publicvoidrun(){}while(true){try{Thread.sleep(300);}times++;}catch(InterruptedExceptione){}//TODOAuto-generatedcatchblocke.printStackTrace();inttimes=0;publicvoidpaint(Graphicsg){}super.paint(g);g.fillRect(0,0,400,300);if(times%2==0){Fontmyfont=newFont("行楷",Font.BOLD,30);g.setFont(myfont);g.setColor(Color.yellow);g.drawString("第一关",130,130);}this.repaint();//我的面板classMyPanelextendsJPanelimplementsKeyListener,Runnable{//定义一个我的坦克Herohero=null;EnemyTanket=null;Imageimage1=null;Imageimage2=null;Imageimage3=null;Imageim=null;Vectorbombs=newVector();Vectoretm=newVector();intensize=5;publicvoidpaint(Graphicsg){super.paint(g);g.fillRect(0,0,400,300);g.drawImage(im,0,0,400,300,this);//画出自己的坦克if(hero.isLive==true){}this.drawTank(this.hero.getX(),this.hero.getY(),g,this.hero.getDirect(),this.hero.getColor());//画出子弹for(inti=0;i{Shotmyshot=hero.shotm.get(i);}if(myshot!=null&&myshot.isLive==true){}if(myshot.isLive==false)hero.shotm.remove(myshot);g.fill3DRect(myshot.getShotX(),myshot.getShotY(),2,2,false);//画出炸弹for(inti=0;i{Bombb=bombs.get(i);if(b.lift>6){g.drawImage(image1,b.x,b.y,30,30,this);}elseif(b.lift>3)//}{g.drawImage(image2,b.x,b.y,30,30,this);}else{g.drawImage(image3,b.x,b.y,30,30,this);}b.liftdown();if(b.lift==0)bombs.remove(b);//画出敌人的坦克for(inti=0;i{et=etm.get(i);if(et!=null&&et.isLive==true){}this.drawTank(et.getX(),et.getY(),g,et.getDirect(),et.getColor());//画出敌人的子弹for(intj=0;j{Shotenshot=et.ensh.get(j);if(enshot.isLive==true){g.fill3DRect(enshot.getShotX(),enshot.getShotY(),2,2,false);System.out.println("第"+i+"辆坦克的第"+j+"颗子弹的Y="+enshot.getShotY());}else}//判断子弹是否击中坦克的函数publicvoidhitTank(Shots,Tankt)}}{}et.ensh.remove(enshot);{}//画出坦克的函数publicvoiddrawTank(intxx,intyy,Graphicsg,intdirect,inttype){//判断什么颜色类型的坦克switch(type){case0:g.set
if(this.x>=et.x&&this.x<=et.x+20&&this.y+30<=et.y+30&&this.y+30>et.y)
if(this.x+20>=et.x&&this.x+20<=et.x+20&&this.y+30<=et.y+30&&this.y+30>et.y)
{{
if(this.x>=et.x&&this.x<=et.x+30&&this.y+30<=et.y+20&&this.y+30>et.y)
if(this.x+20>=et.x&&this.x+20<=et.x+30&&this.y+30<=et.y+20&&this.y+30>et.y)
}}}}
for(inti=0;i{et=ets.get(i);if(et!=this){{}if(et.direct==0||et.direct==2){if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y<=et.y+30&&this.y>et.y)returntrue;if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y+20<=et.y+30&&this.y+20>et.y){returntrue;}}if(et.direct==1||et.direct==3){if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y<=et.y+20&&this.y>et.y){}returntrue;if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y+20<=et.y+20&&this.y+20>et.y)}}{}returntrue;}}break;returnb;}publicvoidrun(){while(true){switch(this.direct){case0:for(inti=0;i<30;i++){if(y>0&&this.isTouch()==false)y-=this.speed;try{}Thread.sleep(50);//TODOAuto-generatedcatchblocke.printStackTrace();}catch(InterruptedExceptione){}break;case1:for(inti=0;i<30;i++){if(x<365&&this.isTouch()==false)x+=this.speed;try{Thread.sleep(50);}catch(InterruptedExceptione){}//TODOAuto-generatedcatchblocke.printStackTrace();}break;case2:for(inti=0;i<30;i++){}if(y<270&&this.isTouch()==false)y+=this.speed;try{}Thread.sleep(50);//TODOAuto-generatedcatchblocke.printStackTrace();}catch(InterruptedExceptione){break;case3:}this.direct=(int)(Math.random()*4);if(this.isLive==false){for(inti=0;i<30;i++){if(x>0&&this.isTouch()==false)x-=this.speed;}break;try{Thread.sleep(50);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblock}e.printStackTrace();}}}break;}if(ensh.size()<5){}Shotes=null;switch(this.direct){case0:es=newShot(this.getX()+10,this.getY(),0);ensh.add(es);break;case1:es=newShot(this.getX()+30,this.getY()+10,1);ensh.add(es);break;case2:es=newShot(this.getX()+10,this.getY()+30,2);ensh.add(es);}break;es=newShot(this.getX(),this.getY()+10,3);ensh.add(es);break;case3:Threadth=newThread(es);th.start();//炸弹类classBomb{intx;inty;intlift=9;booleanisLive=true;publicBomb(intx,inty){this.x=x;this.y=y;}//炸弹的生命值publicvoidliftdown(){}if(lift>0){lift--;}else{}isLive=false;}//子弹类classShotimplementsRunnable{intshotX;intshotY;intdirect;intshotspeed=1;booleanisLive=true;publicShot(intx,inty,intdirect){}this.shotX=x;this.shotY=y;this.direct=direct;publicintgetShotX(){}publicvoidsetShotX(intshotX){}publicintgetShotY(){this.shotX=shotX;returnshotX;}returnshotY;publicvoidsetShotY(intshotY){this.shotY=shotY;}publicintgetShotspeed(){returnshotspeed;}publicvoidsetShotspeed(intshotspeed){}this.shotspeed=shotspeed;publicvoidrun(){while(true){try{Thread.sleep(50);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}switch(direct){case0:shotY-=shotspeed;break;case1:shotX+=shotspeed;break;case2:shotY+=shotspeed;break;case3:shotX-=shotspeed;}if(shotX<0||shotX>400||shotY<0||shotY>300){break;}}}}isLive=false;break;/***功能:坦克大战4.0*/packagecom.tankgame4;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.KeyEvent;importjava.awt.event.KeyListener;importjava.util.Vector;importjava.io.*;importjavax.swing.*;importjavax.imageio.*;publicclassMyTankGame4extendsJFrameimplementsActionListener{MyPanelmp=null;MyStartPanelmsp=null;//菜单定义JMenuBarjmb=null;JMenujm1=null;JMenuItemjmi1=null;publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubMyTankGame4mtg=newMyTankGame4();}//构造函数}publicMyTankGame4(){}//创建菜单jmb=newJMenuBar();jm1=newJMenu("Game(G)");jm1.setMnemonic('G');jmi1=newJMenuItem("NewGame(N)");jmi1.setMnemonic('N');jmi1.addActionListener(this);jmi1.setActionCommand("NewGame");jm1.add(jmi1);jmb.add(jm1);this.setJMenuBar(jmb);msp=newMyStartPanel();Threadst=newThread(msp);st.start();this.add(msp);this.setTitle("坦克大战");this.setSize(400,300);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);publicvoidactionPerformed(ActionEventarg0){if(arg0.getActionCommand().equals("NewGame"))}{}this.remove(msp);mp=newMyPanel();Threadmt=newThread(mp);mt.start();this.add(mp);this.addKeyListener(mp);this.setVisible(true);//游戏开始面板classMyStartPanelextendsJPanelimplementsRunnable{}publicvoidrun(){}while(true){try{Thread.sleep(300);}times++;}catch(InterruptedExceptione){}//TODOAuto-generatedcatchblocke.printStackTrace();inttimes=0;publicvoidpaint(Graphicsg){}super.paint(g);g.fillRect(0,0,400,300);if(times%2==0){Fontmyfont=newFont("行楷",Font.BOLD,30);g.setFont(myfont);g.setColor(Color.yellow);g.drawString("第一关",130,130);}this.repaint();//我的面板classMyPanelextendsJPanelimplementsKeyListener,Runnable{//定义一个我的坦克Herohero=null;EnemyTanket=null;Imageimage1=null;Imageimage2=null;Imageimage3=null;Imageim=null;Vectorbombs=newVector();Vectoretm=newVector();intensize=5;publicvoidpaint(Graphicsg){super.paint(g);g.fillRect(0,0,400,300);g.drawImage(im,0,0,400,300,this);//画出自己的坦克if(hero.isLive==true){}this.drawTank(this.hero.getX(),this.hero.getY(),g,this.hero.getDirect(),this.hero.getColor());//画出子弹for(inti=0;i{Shotmyshot=hero.shotm.get(i);}if(myshot!=null&&myshot.isLive==true){}if(myshot.isLive==false)hero.shotm.remove(myshot);g.fill3DRect(myshot.getShotX(),myshot.getShotY(),2,2,false);//画出炸弹for(inti=0;i{Bombb=bombs.get(i);if(b.lift>6){g.drawImage(image1,b.x,b.y,30,30,this);}elseif(b.lift>3)//}{g.drawImage(image2,b.x,b.y,30,30,this);}else{g.drawImage(image3,b.x,b.y,30,30,this);}b.liftdown();if(b.lift==0)bombs.remove(b);//画出敌人的坦克for(inti=0;i{et=etm.get(i);if(et!=null&&et.isLive==true){}this.drawTank(et.getX(),et.getY(),g,et.getDirect(),et.getColor());//画出敌人的子弹for(intj=0;j{Shotenshot=et.ensh.get(j);if(enshot.isLive==true){g.fill3DRect(enshot.getShotX(),enshot.getShotY(),2,2,false);System.out.println("第"+i+"辆坦克的第"+j+"颗子弹的Y="+enshot.getShotY());}else}//判断子弹是否击中坦克的函数publicvoidhitTank(Shots,Tankt)}}{}et.ensh.remove(enshot);{}//画出坦克的函数publicvoiddrawTank(intxx,intyy,Graphicsg,intdirect,inttype){//判断什么颜色类型的坦克switch(type){case0:g.set
returnb;
publicvoidrun(){
while(true)
switch(this.direct)
for(inti=0;i<30;i++)
if(y>0&&this.isTouch()==false)y-=this.speed;
try{
}Thread.sleep(50);
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(InterruptedExceptione){
if(x<365&&this.isTouch()==false)x+=this.speed;
Thread.sleep(50);
}//TODOAuto-generatedcatchblock
}if(y<270&&this.isTouch()==false)y+=this.speed;
this.direct=(int)(Math.random()*4);
if(this.isLive==false)
{for(inti=0;i<30;i++)
if(x>0&&this.isTouch()==false)x-=this.speed;
break;try{
}e.printStackTrace();
}}}break;
if(ensh.size()<5)
}Shotes=null;
es=newShot(this.getX()+10,this.getY(),0);
ensh.add(es);
es=newShot(this.getX()+30,this.getY()+10,1);
es=newShot(this.getX()+10,this.getY()+30,2);
}break;
es=newShot(this.getX(),this.getY()+10,3);
Threadth=newThread(es);
//炸弹类
classBomb
intx;
inty;
intlift=9;
publicBomb(intx,inty)
//炸弹的生命值
publicvoidliftdown()
}if(lift>0)
lift--;
}else
}isLive=false;
//子弹类
classShotimplementsRunnable
intshotX;
intshotY;
intdirect;
intshotspeed=1;
publicShot(intx,inty,intdirect)
}this.shotX=x;
this.shotY=y;
publicintgetShotX(){
publicvoidsetShotX(intshotX){
publicintgetShotY(){this.shotX=shotX;
returnshotX;
}returnshotY;
publicvoidsetShotY(intshotY){
this.shotY=shotY;
publicintgetShotspeed(){
returnshotspeed;
publicvoidsetShotspeed(intshotspeed){
}this.shotspeed=shotspeed;
shotY-=shotspeed;
shotX+=shotspeed;
shotY+=shotspeed;
shotX-=shotspeed;
if(shotX<0||shotX>400||shotY<0||shotY>300)
{break;
}}}}isLive=false;
/**
*功能:
坦克大战4.0
*/
importjava.awt.*;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.KeyEvent;
importjava.awt.event.KeyListener;
importjava.io.*;
importjavax.swing.*;
importjavax.imageio.*;
publicclassMyTankGame4extendsJFrameimplementsActionListener{
MyPanelmp=null;
MyStartPanelmsp=null;
//菜单定义
JMenuBarjmb=null;
JMenujm1=null;
JMenuItemjmi1=null;
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
MyTankGame4mtg=newMyTankGame4();
//构造函数
}publicMyTankGame4()
}//创建菜单
jmb=newJMenuBar();
jm1=newJMenu("Game(G)");
jm1.setMnemonic('G');
jmi1=newJMenuItem("NewGame(N)");
jmi1.setMnemonic('N');
jmi1.addActionListener(this);
jmi1.setActionCommand("NewGame");
jm1.add(jmi1);
jmb.add(jm1);
this.setJMenuBar(jmb);
msp=newMyStartPanel();
Threadst=newThread(msp);
st.start();
this.add(msp);
this.setTitle("坦克大战");
this.setSize(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
publicvoidactionPerformed(ActionEventarg0){
if(arg0.getActionCommand().equals("NewGame"))
}this.remove(msp);
mp=newMyPanel();
Threadmt=newThread(mp);
mt.start();
this.add(mp);
this.addKeyListener(mp);
//游戏开始面板
classMyStartPanelextendsJPanelimplementsRunnable
}publicvoidrun(){
}while(true)
Thread.sleep(300);
}times++;
inttimes=0;
publicvoidpaint(Graphicsg)
}super.paint(g);
g.fillRect(0,0,400,300);
if(times%2==0)
Fontmyfont=newFont("行楷",Font.BOLD,30);
g.setFont(myfont);
g.setColor(Color.yellow);
g.drawString("第一关",130,130);
this.repaint();
//我的面板
classMyPanelextendsJPanelimplementsKeyListener,Runnable
//定义一个我的坦克
Herohero=null;
Imageimage1=null;
Imageimage2=null;
Imageimage3=null;
Imageim=null;
Vectorbombs=newVector();
Vectoretm=newVector();
intensize=5;
super.paint(g);
g.drawImage(im,0,0,400,300,this);
//画出自己的坦克
if(hero.isLive==true)
}this.drawTank(this.hero.getX(),this.hero.getY(),
g,this.hero.getDirect(),this.hero.getColor());
//画出子弹
for(inti=0;i{Shotmyshot=hero.shotm.get(i);}if(myshot!=null&&myshot.isLive==true){}if(myshot.isLive==false)hero.shotm.remove(myshot);g.fill3DRect(myshot.getShotX(),myshot.getShotY(),2,2,false);//画出炸弹for(inti=0;i{Bombb=bombs.get(i);if(b.lift>6){g.drawImage(image1,b.x,b.y,30,30,this);}elseif(b.lift>3)//}{g.drawImage(image2,b.x,b.y,30,30,this);}else{g.drawImage(image3,b.x,b.y,30,30,this);}b.liftdown();if(b.lift==0)bombs.remove(b);//画出敌人的坦克for(inti=0;i{et=etm.get(i);if(et!=null&&et.isLive==true){}this.drawTank(et.getX(),et.getY(),g,et.getDirect(),et.getColor());//画出敌人的子弹for(intj=0;j{Shotenshot=et.ensh.get(j);if(enshot.isLive==true){g.fill3DRect(enshot.getShotX(),enshot.getShotY(),2,2,false);System.out.println("第"+i+"辆坦克的第"+j+"颗子弹的Y="+enshot.getShotY());}else}//判断子弹是否击中坦克的函数publicvoidhitTank(Shots,Tankt)}}{}et.ensh.remove(enshot);{}//画出坦克的函数publicvoiddrawTank(intxx,intyy,Graphicsg,intdirect,inttype){//判断什么颜色类型的坦克switch(type){case0:g.set
Shotmyshot=hero.shotm.get(i);
}if(myshot!
=null&&myshot.isLive==true)
if(myshot.isLive==false)hero.shotm.remove(myshot);g.fill3DRect(myshot.getShotX(),
myshot.getShotY(),2,2,false);
//画出炸弹
for(inti=0;i{Bombb=bombs.get(i);if(b.lift>6){g.drawImage(image1,b.x,b.y,30,30,this);}elseif(b.lift>3)//}{g.drawImage(image2,b.x,b.y,30,30,this);}else{g.drawImage(image3,b.x,b.y,30,30,this);}b.liftdown();if(b.lift==0)bombs.remove(b);//画出敌人的坦克for(inti=0;i{et=etm.get(i);if(et!=null&&et.isLive==true){}this.drawTank(et.getX(),et.getY(),g,et.getDirect(),et.getColor());//画出敌人的子弹for(intj=0;j{Shotenshot=et.ensh.get(j);if(enshot.isLive==true){g.fill3DRect(enshot.getShotX(),enshot.getShotY(),2,2,false);System.out.println("第"+i+"辆坦克的第"+j+"颗子弹的Y="+enshot.getShotY());}else}//判断子弹是否击中坦克的函数publicvoidhitTank(Shots,Tankt)}}{}et.ensh.remove(enshot);{}//画出坦克的函数publicvoiddrawTank(intxx,intyy,Graphicsg,intdirect,inttype){//判断什么颜色类型的坦克switch(type){case0:g.set
Bombb=bombs.get(i);
if(b.lift>6)
g.drawImage(image1,b.x,b.y,30,30,this);
}elseif(b.lift>3)
//}{
g.drawImage(image2,b.x,b.y,30,30,this);
g.drawImage(image3,b.x,b.y,30,30,this);
b.liftdown();
if(b.lift==0)bombs.remove(b);
//画出敌人的坦克
for(inti=0;i{et=etm.get(i);if(et!=null&&et.isLive==true){}this.drawTank(et.getX(),et.getY(),g,et.getDirect(),et.getColor());//画出敌人的子弹for(intj=0;j{Shotenshot=et.ensh.get(j);if(enshot.isLive==true){g.fill3DRect(enshot.getShotX(),enshot.getShotY(),2,2,false);System.out.println("第"+i+"辆坦克的第"+j+"颗子弹的Y="+enshot.getShotY());}else}//判断子弹是否击中坦克的函数publicvoidhitTank(Shots,Tankt)}}{}et.ensh.remove(enshot);{}//画出坦克的函数publicvoiddrawTank(intxx,intyy,Graphicsg,intdirect,inttype){//判断什么颜色类型的坦克switch(type){case0:g.set
et=etm.get(i);
=null&&et.isLive==true)
}this.drawTank(et.getX(),et.getY(),
g,et.getDirect(),et.getColor());
//画出敌人的子弹
for(intj=0;j{Shotenshot=et.ensh.get(j);if(enshot.isLive==true){g.fill3DRect(enshot.getShotX(),enshot.getShotY(),2,2,false);System.out.println("第"+i+"辆坦克的第"+j+"颗子弹的Y="+enshot.getShotY());}else}//判断子弹是否击中坦克的函数publicvoidhitTank(Shots,Tankt)}}{}et.ensh.remove(enshot);{}//画出坦克的函数publicvoiddrawTank(intxx,intyy,Graphicsg,intdirect,inttype){//判断什么颜色类型的坦克switch(type){case0:g.set
Shotenshot=et.ensh.get(j);
if(enshot.isLive==true)
g.fill3DRect(enshot.getShotX(),
enshot.getShotY(),2,2,false);
System.out.println("第"+i+"辆坦克的第"+j+"颗子弹的Y="+enshot.getShotY());
//判断子弹是否击中坦克的函数
publicvoidhitTank(Shots,Tankt)}}{
}et.ensh.remove(enshot);
//画出坦克的函数
publicvoiddrawTank(intxx,intyy,Graphicsg,intdirect,inttype)
//判断什么颜色类型的坦克
switch(type)
g.set
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2