您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 广告经营 > Java游戏开发之坦克大战代码
Java游戏开发之坦克大战代码1Java游戏开发之坦克大战代码标题:Java游戏开发之坦克大战代码关键词:Java坦克大战,坦克大战Java代码这是一个Java坦克大战的源码,实现了大部分功能,是学习Java绘图技术的好例子主类:packagecom.qq.TankGame;importjava.awt.Color;importjava.awt.Font;importjava.awt.Graphics;importjava.awt.Image;importjava.awt.Paint;importjava.awt.Panel;importjava.awt.Toolkit;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.KeyEvent;importjava.awt.event.KeyListener;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.FileNotFoundException;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;importjava.util.Vector;importjavax.swing.JFrame;importjavax.swing.JMenu;importjavax.swing.JMenuBar;importjavax.swing.JMenuItem;importjavax.swing.JPanel;importjavax.swing.JTextArea;/////**//*功能:java绘图//*@authorAdministrator//*//*/publicclassTankGameextendsJFrameimplementsActionListener{MyPanelmp=null;Java游戏开发之坦克大战代码2myStartPanelmsp=null;EnemyTaskem=null;JMenuBarjmb=null;JMenujm=null;JMenuItemjmi1=null;JMenuItemjmi2=null;JMenuItemjmi3=null;publicstaticvoidmain(String[]args){TankGametankGame=newTankGame();}publicTankGame(){jmb=newJMenuBar();jm=newJMenu(游戏(G));jm.setMnemonic('G');jmi1=newJMenuItem(开始游戏(S));jmi1.setMnemonic('S');jmi1.addActionListener(this);jmi1.setActionCommand(newGame);jmi2=newJMenuItem(继续游戏(O));jmi2.setMnemonic('O');jmi3=newJMenuItem(退出游戏(X));jmi3.setMnemonic('X');jmi3.addActionListener(this);jmi3.setActionCommand(exit);//启动mp线程//em=newEnemyTask();//mp=newMyPanel();msp=newmyStartPanel();this.setJMenuBar(jmb);this.add(msp);//this.add(mp);jmb.add(jm);jm.add(jmi1);jm.add(jmi2);jm.add(jmi3);this.setSize(600,500);//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);Java游戏开发之坦克大战代码3//this.addKeyListener(mp);Threadt1=newThread(msp);t1.start();//Threadt=newThread(mp);//t.start();}@OverridepublicvoidactionPerformed(ActionEventarg0){if(arg0.getActionCommand().equals(newGame)){mp=newMyPanel();Threadt=newThread(mp);t.start();this.remove(msp);this.remove(mp);this.add(mp);this.setVisible(true);this.addKeyListener(mp);//Recoder.getRecording();}elseif(arg0.getActionCommand()==exit){//Recoder.KeepRecording();Recoder.KeepExit();//KeepExit();System.exit(0);}}}classmyStartPanelextendsJPanelimplementsRunnable{inttimes=0;publicvoidpaint(Graphicsg){super.paint(g);g.fillRect(0,0,400,300);if(times%2==0){g.setColor(Color.BLUE);Fontf=newFont(华文新魏,Font.BOLD,30);g.setFont(f);g.drawString(stage:1,150,150);}}@Overridepublicvoidrun(){Java游戏开发之坦克大战代码4while(true){try{Thread.sleep(300);}catch(InterruptedExceptione){e.printStackTrace();}this.repaint();times++;}}}classMyPanelextendsJPanelimplementsKeyListener,Runnable{int[]TanksX=newint[20];int[]TanksY=newint[20];int[]ShotsX=newint[100];int[]ShotsY=newint[100];//int[][]Tanks=newint[20][2];//int[][]Shots=newint[20][2];int[]ETdirects=newint[20];int[]HeroShotsX=newint[10];int[]HeroShotsY=newint[10];staticHerohero=null;EnemyTasket=null;shots=null;booleanisPaintShot=true;inta=0,b=0,c=0;int[]directs=newint[10];static//敌人坦克集合VectorEnemyTaskets=newVectorEnemyTask();intenSize=3;//敌人坦克初始数量intenSizes=5;//敌人坦克画面总数量inthintEnemyTanks=0;//击中敌人坦克数量publicvoidshowInfo(Graphicsg){//画提示信息//画出提出坦克this.drawTank(80,330,g,0,1);this.drawTank(160,330,g,0,0);g.setColor(Color.BLACK);g.drawString(Recoder.getEnNum()+,105,350);Java游戏开发之坦克大战代码5g.drawString(Recoder.getMyLife()+,185,350);//画出成绩g.setColor(Color.BLACK);Fontf=newFont(宋体,Font.BOLD,15);g.setFont(f);g.drawString(你的总成绩:,400,20);this.drawTank(410,30,g,0,1);g.drawString((20-Recoder.getEnNum())+,435,50);}publicvoidpaint(Graphicsg){super.paint(g);g.fillRect(0,0,400,300);this.showInfo(g);//画出提示信息//自己坦克if(hero.isLive==true){this.drawTank(hero.getX(),hero.getY(),g,hero.direct,hero.type);}else{if(Recoder.getMyLife()0){hero=newHero(30,270,0,0,10,true);hero.isLive=true;}}//画出敌人坦克for(inti=0;iets.size();i++){EnemyTasket=ets.get(i);if(!et.isLive){ets.remove(i);if(Recoder.getEnNum()0){this.drawTank(50,0,g,1,1);ets.add(et);//et.isLive=true;Threadt=newThread(et);t.start();}//if(Recoder.getEnNum()0){//et.isLive=true;//}}if(et.isLive){Java游戏开发之坦克大战代码6this.drawTank(et.getX(),et.getY(),g,et.direct,et.type);System.out.println(et.ss.size()+et.ss.size());for(intj=0;jet.ss.size();j++){//敌人坦克子弹s=et.ss.get(j);if(s.isLive){if(isPaintShot){g.fill3DRect(s.x,s.y,5,10,false);}}else{et.ss.remove(j);}}}//if(!et.isLive){//ets.remove(i);//}}//画出子弹for(inti=0;ihero.ss.size();i++){if(hero.ss.get(i)!=null&&hero.ss.get(i).isLive==true){g.fill3DRect(hero.ss.get(i).x,hero.ss.get(i).y,5,10,false);}if(hero.ss.get(i).isLive==false){hero.ss.remove(hero.ss.get(i));}}}//publicvoidhintTank2(shots,Heroet){//switch(et.direct){//case0://case1://if(s.xet.x+20&&s.xet.x//&&s.yet.y+30&&s.yet.y){//s.isLive=false;//et.isLive=false;//}////break;//case2://case3://if(s.xet.x+30&&s.xet.xJava游戏开发之坦克大战代码7//&&s.yet.y+20&&s.yet.y){//s.isLive=false;//et.isLive=false;//}////break;//////default:////break;//}////}publicbooleanhintTank(shots,Tanket){booleanb=false;switch(et.direct){case0:case1:if(s.xe
本文标题:Java游戏开发之坦克大战代码
链接地址:https://www.777doc.com/doc-5372837 .html