//此文件有三个.java文件组成//以包名为标志,复制时请注意packagecom.tank4_3;importjavax.imageio.ImageIO;importjava.io.*;importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;importjava.util.*;publicclassMyTankGameextendsJFrameimplementsActionListener{MyJPanelmp=null;MyStartPanelmsp=null;JMenuBarjmb=null;JMenujm1=null;JMenujm2=null;JMenuItemjmi1=null;JMenuItemjmi2=null;JMenuItemjmi3=null;JMenuItemjmi4=null;JMenuItemjmi5=null;JMenuItemjmi6=null;JMenuItemjmi21=null;publicstaticvoidmain(String[]args){MyTankGameytg=newMyTankGame();}publicMyTankGame(){msp=newMyStartPanel();Threadt=newThread(msp);t.start();jmb=newJMenuBar();jm1=newJMenu(游戏(G));jm1.setMnemonic('g');jmi1=newJMenuItem(新游戏(N));jmi1.setMnemonic('n');jmi1.addActionListener(this);jmi1.setActionCommand(newgame);jmi2=newJMenuItem(暂停(S));jmi2.setMnemonic('s');jmi2.addActionListener(this);jmi2.setActionCommand(suspend);jmi3=newJMenuItem(继续(C));jmi3.setMnemonic('c');jmi3.addActionListener(this);jmi3.setActionCommand(continue);jmi6=newJMenuItem(读取游戏(X));jmi6.setMnemonic('x');jmi6.addActionListener(this);jmi6.setActionCommand(loadGame);jmi4=newJMenuItem(储存游戏(Q));jmi4.setMnemonic('q');jmi4.addActionListener(this);jmi4.setActionCommand(save);jmi5=newJMenuItem(退出(E));jmi5.setMnemonic('e');jmi5.addActionListener(this);jmi5.setActionCommand(exit);jm2=newJMenu(帮助(H));jm2.setMnemonic('h');jmi21=newJMenuItem(操作说明(U));jmi21.setMnemonic('u');jmi21.addActionListener(this);jmi21.setActionCommand(help);jmb.add(jm1);jmb.add(jm2);jm1.add(jmi1);jm1.add(jmi2);jm1.add(jmi3);jm1.add(jmi6);jm1.add(jmi4);jm1.add(jmi5);jm2.add(jmi21);this.setJMenuBar(jmb);this.add(msp);setSize(430,430);setResizable(false);setTitle(坦克大战4.3);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}publicvoidactionPerformed(ActionEventarg0){//TODOAuto-generatedmethodstub//按开始后显示战斗场面if(arg0.getActionCommand().equals(newgame)){//进入战斗后可以重新开始if(this.mp!=null){this.remove(mp);}this.mp=newMyJPanel(new);add(this.mp);Threadt=newThread(this.mp);t.start();addKeyListener(this.mp);GameRecord.setEnNum(20);GameRecord.setMyNum(3);GameRecord.setHitNum(0);//要显示战斗场面,先要移除开始画面this.remove(msp);//刷新this.setVisible(true);}//战斗暂停elseif(arg0.getActionCommand().equals(suspend)){//让子弹和坦克的速度变为0MyTank.setSpeed(0);EnemyTank.setSpeed(0);Bullet.setSpeed(0);//让坦克不能改变方向EnemyTank.setStrar(false);MyTank.setStrar(false);}//战斗重新开始elseif(arg0.getActionCommand().equals(continue)){//恢复子弹和坦克的速度MyTank.setSpeed(3);EnemyTank.setSpeed(1);Bullet.setSpeed(3);EnemyTank.setStrar(true);MyTank.setStrar(true);}//读取游戏elseif(arg0.getActionCommand().equals(loadGame)){if(this.mp!=null){this.remove(mp);}this.mp=newMyJPanel(load);add(this.mp);Threadt=newThread(this.mp);t.start();addKeyListener(this.mp);//要显示战斗场面,先要移除开始画面if(this.msp!=null){this.remove(msp);}//刷新this.setVisible(true);}//储存游戏elseif(arg0.getActionCommand().equals(save)){//调用保存游戏函数GameRecord.writeRecord();//跳出保存成功窗口ReSuccessre=newReSuccess();}//退出elseif(arg0.getActionCommand().equals(exit)){System.exit(0);}//帮助elseif(arg0.getActionCommand().equals(help)){Helph=newHelp();}}}//画出开始提示面板classMyStartPanelextendsJPanelimplementsRunnable{inttime=0;publicvoidpaint(Graphicsg){super.paint(g);g.fillRect(0,0,300,310);if(time%2==0){g.setColor(Color.yellow);g.setFont(newFont(宋体,Font.BOLD,22));g.drawString(Stage:1,100,140);}}//让字体闪动publicvoidrun(){while(true){try{Thread.sleep(500);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}time++;if(time==13){break;}this.repaint();}}}classMyJPanelextendsJPanelimplementsKeyListener,Runnable{MyTankmytank=null;Imageimage1=null;Imageimage2=null;Imageimage3=null;Stringflag;VectorEnemyTankenem=newVectorEnemyTank();//VectorNodenodes=newVectorNode();VectorBobmbo=newVectorBobm();//战斗敌人坦克的数量intenSize=4;publicMyJPanel(Stringflag){this.flag=flag;this.mytank=newMyTank(100,284);//新的游戏if(flag.equals(new)){for(inti=0;ithis.enSize;i++){EnemyTankenemy=newEnemyTank(i*90,0);//传到EnemyTankenemy.setDirect(1);enemy.setEnems(enem);//传到GameRecordGameRecord.setEnems(enem);Threadth=newThread(enemy);th.start();this.enem.add(enemy);}//读取游戏}elseif(flag.equals(load)){GameRecord.readerRecord();System.out.print(GameRecord.nodes.size());for(inti=0;iGameRecord.nodes.size();i++){Nodenode=GameRecord.nodes.get(i);EnemyTankenemy=newEnemyTank(node.x,node.y);enemy.setDirect(node.direct);//传到EnemyTankenemy.setEnems(enem);//传到GameRecordGameRecord.setEnems(enem);Threadth=newThread(enemy);th.start();this.enem.add(enemy);}}//初始化图像(可以看到第一辆坦克的爆炸效果)try{this.image1=ImageIO.read(newFile(tank1.gif));this.image2=ImageIO.read(newFile(tank2.gif));this.image3=ImageIO.read(newFile(tank3.gif));}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}//this.image1=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource(/tank1.gif));//this.image2=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource(/tank2.gif));//this.image3=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource(/tank3.gif));}//记录信息的显示(坦克剩余的数量,击中坦克数量)publicvoidrecordpaint(Graphicsg){drawTank(30,320,