您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > 数据库 > java坦克大战源代码
packagecom.cwj.tankGame;importjavax.sound.sampled.*;importjava.io*;importjava.awt.*;importjava.io.IOException;importjava.util.*;importjavax.swing,*;importjavax.imageio.ImageIO;importjavax.swing.*;classAePlayWaveextendsThread{privateStringfilename;publicAePlayWave(Stringwavfile){filename=wavfile;}publicvoidrun(){FilesoundFile=newFile(filename);AudioInputStreamaudioInputStream=null;try{audioInputStream=AudioSystem.getAudioInputStream(soundFile);}catch(Exceptione1){e1.printStackTrace();return;}AudioFormatformat=audioInputStream.getFormat();SourceDataLineauline=null;DataLine.Infoinfo=newDataLine.Info(SourceDataLine.class,format);try{auline=(SourceDataLine)AudioSystem.getLine(info);auline.open(format);}catch(Exceptione){e.printStackTrace();return;}auline.start();intnBytesRead=0;//这是缓冲byte[]abData=newbyte[512];try{while(nBytesRead!=-1){nBytesRead=audioInputStream.read(abData,0,abData.length);if(nBytesRead=0)auline.write(abData,0,nBytesRead);}}catch(IOExceptione){e.printStackTrace();return;}finally{auline.drain();auline.close();}}}classBomb{intx;inty;intlifeTime=10;booleanisLive=true;publicBomb(intx,inty){this.x=x;this.y=y;}publicvoidlifeDown(){if(lifeTime0){lifeTime--;}else{this.isLive=false;}}}classEnemyTankextendsTankimplementsRunnable{intn=1;//敌方坦克转弯n次发射一颗子弹intMaxShot=10;intrunDistance=10;//变一次方向前进的距离//定义一个向量可以访问MyPanel上所有地方坦克VectorEnemyTankets=newVectorEnemyTank();VectorShotss=newVectorShot();publicvoidsetEts(VectorEnemyTankv){this.ets=v;}publicintabs(intx){if(x0)returnx;elsereturn-x;}publicbooleanisTouchOther(){switch(this.direction){case0://上for(inti=0;iets.size();i++){EnemyTankt=ets.get(i);if(t!=this)//不是自己这辆坦克{if(abs(this.x-t.x)=40&&this.yt.y&&this.y-t.y=40){returntrue;}}}break;case2://下for(inti=0;iets.size();i++){EnemyTankt=ets.get(i);if(t!=this)//不是自己这辆坦克{if(abs(this.x-t.x)=40&&t.ythis.y&&t.y-this.y=40){returntrue;}}}break;case1://左for(inti=0;iets.size();i++){EnemyTankt=ets.get(i);if(t!=this)//不是自己这辆坦克{if(abs(this.y-t.y)=40&&this.xt.x&&this.x-t.x=40){returntrue;}}}break;case3://右for(inti=0;iets.size();i++){EnemyTankt=ets.get(i);if(t!=this)//不是自己这辆坦克{if(abs(this.y-t.y)=40&&this.xt.x&&t.x-this.x=40){returntrue;}}}break;}returnfalse;}publicEnemyTank(intx,inty){super(x,y);}inttime=0;publicvoidsleep(){try{Thread.sleep(50);}catch(Exceptione){e.printStackTrace();}}publicvoidrun(){while(true){switch(direction){case0:for(inti=0;ithis.runDistance;i++){if(y20&&!this.isTouchOther())y-=speed;this.sleep();}break;case1:for(inti=0;ithis.runDistance;i++){if(x20&&!this.isTouchOther())x-=speed;this.sleep();}break;case2:for(inti=0;ithis.runDistance;i++){if(y580&&!this.isTouchOther())y+=speed;this.sleep();}break;case3:for(inti=0;ithis.runDistance;i++){if(x780&&!this.isTouchOther())x+=speed;this.sleep();}break;}this.direction=(int)(Math.random()*4);if(this.isLive==false){break;}else{time++;if(time%n==0)if(ss.size()MaxShot){Shots=newShot(x,y);s.setDirection(direction);ss.add(s);Threadt=newThread(s);t.start();}}}}}classMyTankextendsTank{intMAXSHOT=20;publicintgetMAXSHOT(){returnMAXSHOT;}publicvoidsetMAXSHOT(intmAXSHOT){MAXSHOT=mAXSHOT;}Shotshot=null;VectorShotss=newVectorShot();publicMyTank(intx,inty){super(x,y);}publicvoidfire(){if(this.isLive==true){shot=newShot(x,y);ss.add(shot);Threadt=newThread(shot);t.start();shot.setDirection(this.direction);}}publicvoidmoveUp(){if(y20)y-=speed;}publicvoidmoveLeft(){if(x20)x-=speed;}publicvoidmoveDown(){if(y580)y+=speed;}publicvoidmoveRight(){if(x780)x+=speed;}}classNode{intx;publicintgetX(){returnx;}publicvoidsetX(intx){this.x=x;}publicintgetY(){returny;}publicvoidsetY(inty){this.y=y;}publicintgetDirection(){returndirection;}publicvoidsetDirection(intdirection){this.direction=direction;}inty;intdirection;publicNode(intx,inty){this.x=x;this.y=y;}}classRecord{//记录每关有多少敌人privatestaticintetNum=20;privatestaticintsum=0;privatestaticFileWriterfw=null;privatestaticBufferedWriterbw=null;privatestaticFileReaderfr=null;privatestaticBufferedReaderbr=null;privatestaticVectorNodenodes=null;publicstaticVectorNodegetNodes(){returnnodes;}publicstaticvoidsetNodes(VectorNodenodes){Record.nodes=nodes;}publicstaticVectorEnemyTankets=null;publicstaticVectorEnemyTankgetEts(){returnets;}publicstaticvoidsetEts(VectorEnemyTankets){Record.ets=ets;}publicstaticvoidrestoreRecord(){try{nodes=newVectorNode();Filefile=newFile(e:\\tankGame.txt);if(!file.isDirectory()){if(file.createNewFile())System.out.println(成功创建文件e:\\tankGame.txt);}fr=newFileReader(e:\\tankGame.txt);br=newBufferedReader(fr);StringstrSum=br.readLine();if(strSum!=null)sum=Integer.parseInt(strSum);elsereturn;Stringstr=;while((str=br.readLine())!=null){String[]xyd=str.split();intx,y,direction;x=Integer.parseInt(xyd[0]);y=Integer.parseInt(xyd[1]);direction=Integer.parseInt(xyd[2]);Nodenode=newNode(x,y);node.setDirection(direction);nodes.add(node);}for(inti=0;inodes.size();i++){System.out.println(nodes.get(i).x++nodes.get(i).y++nodes.get(i).direction);}}catch(Exceptione){e.printStackTrace();}f
本文标题:java坦克大战源代码
链接地址:https://www.777doc.com/doc-5520764 .html