您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 广告经营 > java课程设计--坦克大战游戏(带源码)
1.实验设计目的:(1)掌握类的定义和使用;(2)掌握对象的定义;(3)掌握线程的使用。2.实验设计内容:设计一款坦克大战游戏3.技术要点:游戏中的图形的碰撞校验4.实验条件:(1)主要设备:586或更高机型,256MB或更高的内存,40G或更大的硬盘。(2)主要软件:①操作系统可为Windows9X、WinMe、Win2000或更高版本等;②开发环境为jdk或者jcreator。(3)参考书目:①尹继平,张帆.java范例大全.机械工业出版社②施霞萍.java程序设计教程.机械工业出版社5.实验方法与步骤:这个游戏的开发从1.0版本到2.8版本一共28个版本。从第一个版本的游戏界面设计,到添加第一辆坦克,给坦克添加动作,再给坦克添加炮弹,添加游戏中的墙体,生成敌军坦克,给游戏添加图片等。在最终版的程序中一共七个类如图一所示其中Blood类是控制坦克的生命值的,Direction用是enum是控制游戏中一切物体运动方向。Explode是控制坦克被命中时爆炸的,Missile类是控制炮弹的。Tank类是生成坦克的,wall是生成游戏中的墙体的。TankClient是这个游戏的主函数的类,这里调用其他类,运行游戏。程序源码://TankClient类:importjava.awt.*;importjava.awt.event.*;importjava.util.*;importjava.util.List;importjava.util.ArrayList;publicclassTankClientextendsFrame{publicstaticfinalintGAME_WIDTH=800;publicstaticfinalintGAME_HEIGHT=600;TankmyTank=newTank(700,500,true,Direction.STOP,this);//TankenemyTank=newTank(100,100,false,this);Wallw1=newWall(400,200,20,300,this),w2=newWall(500,300,300,20,//Explodee=newExplode(70,80,this);ListExplodeexplodes=newArrayListExplode();ListMissilemissiles=newArrayListMissile();ListTanktanks=newArrayListTank();ImageoffScreenImage=null;publicvoidlauchFrame(){for(inti=0;i10;i++){tanks.add(newTank(100+40*i+1,100,false,Direction.D,this));}this.setLocation(300,100);this.setSize(GAME_WIDTH,GAME_HEIGHT);this.setTitle(TankWar);this.setResizable(false);this.setBackground(Color.GRAY);setVisible(true);this.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);//super.windowClosing(e);}});this.addKeyListener(newKeyMonitor());newThread(newPaintThread()).start();}Bloodb=newBlood();publicvoidpaint(Graphicsg){g.drawString(子弹个数:+missiles.size(),10,50);g.drawString(击中个数:+explodes.size(),10,70);g.drawString(坦克数量:+tanks.size(),10,90);g.drawString(生命值:+myTank.getLife(),10,110);if(tanks.size()=0){for(inti=0;i10;i++){tanks.add(newTank(100+40*i+1,100,false,Direction.D,this}}for(inti=0;imissiles.size();i++){Missilem=missiles.get(i);m.hitTanks(tanks);m.hitTank(myTank);m.hitWall(w1);m.hitWall(w2);//m.hitTank(enemyTank);m.draw(g);}for(inti=0;iexplodes.size();i++){Explodee=explodes.get(i);e.draw(g);}for(inti=0;itanks.size();i++){Tankt=tanks.get(i);t.collidesWithWall(w1);t.collidesWithWall(w2);t.collidesWithTanks(tanks);t.draw(g);}myTank.draw(g);//enemyTank.draw(g);myTank.eat(b);w1.draw(g);w2.draw(g);b.draw(g);}publicvoidupdate(Graphicsg){if(offScreenImage==null){offScreenImage=this.createImage(GAME_WIDTH,GAME_HEIGHT);}GraphicsgOffScreen=offScreenImage.getGraphics();Colorc=gOffScreen.getColor();gOffScreen.setColor(Color.GRAY);gOffScreen.fillRect(0,0,GAME_WIDTH,GAME_HEIGHT);gOffScreen.setColor(c);paint(gOffScreen);g.drawImage(offScreenImage,0,0,null);}publicstaticvoidmain(String[]args){TankClienttc=newTankClient();tc.lauchFrame();}privateclassPaintThreadimplementsRunnable{publicvoidrun(){while(true){repaint();try{Thread.sleep(50);}catch(InterruptedExceptione){e.printStackTrace();}}}}privateclassKeyMonitorextendsKeyAdapter{publicvoidkeyReleased(KeyEvente){myTank.KeyReleased(e);}@OverridepublicvoidkeyPressed(KeyEvente){myTank.keyPressed(e);}}}//Wall类importjava.awt.*;publicclassWall{intx,y,w,h;TankClienttc;publicWall(intx,inty,intw,inth,TankClienttc){this.x=x;this.y=y;this.w=w;this.h=h;this.tc=tc;}publicvoiddraw(Graphicsg){g.fillRect(x,y,w,h);}publicRectanglegetRect(){returnnewRectangle(x,y,w,h);}}//Tankimportjava.awt.*;importjava.awt.event.*;importjava.util.*;importjava.util.List;publicclassTank{publicstaticfinalintXSPEED=10;publicstaticfinalintYSPEED=10;publicstaticfinalintWIDTH=30;publicstaticfinalintHEIGHT=30;privatebooleanlive=true;privateBloodBar_Gbb_G=newBloodBar_G();privateBloodBar_Bbb_B=newBloodBar_B();privateintlife=100;publicintgetLife(){returnlife;}publicvoidsetLife(intlife){this.life=life;}privateclassBloodBar_G{publicvoiddraw(Graphicsg){Colorc=g.getColor();g.setColor(Color.RED);g.drawRect(x,y-5,WIDTH,5);intw=WIDTH*life/100;g.fillRect(x,y-5,w,5);g.setColor(c);}}privateclassBloodBar_B{publicvoiddraw(Graphicsg){Colorc=g.getColor();g.setColor(Color.GREEN);g.drawRect(x,y-5,WIDTH,5);intw=WIDTH*life/100;g.fillRect(x,y-5,w,5);g.setColor(c);}}publicbooleanisLive(){returnlive;}publicvoidsetLive(booleanlive){this.live=live;}TankClienttc;privatebooleangood;privateintoldX,oldY;publicbooleanisGood(){returngood;}intx,y;privatestaticRandomr=newRandom();privatebooleanbL=false,bU=false,bR=false,bD=false;//enumDirection{L,LU,U,RU,R,RD,D,LD,STOP};privateDirectiondir=Direction.STOP;privateDirectionptDir=Direction.D;privateints_step=r.nextInt(100)+3;privateintstep=s_step;publicTank(intx,inty,booleangood){this.x=x;this.y=y;this.oldX=x;this.oldY=y;this.good=good;}publicTank(intx,inty,booleangood,Directiondir,TankClienttc){this(x,y,good);this.dir=dir;this.tc=tc;}publicvoiddraw(Graphicsg){if(!live){if(!good){tc.tanks.remove(this);}return;}Colorc=g.getColor();if(good)g.setColor(Color.RED);elseg.setColor(Color.BLUE);g.fillOval(x,y,WIDTH,HEIGHT);g.setColor(c);if(good)bb_G.draw(g);if(!good)bb_B.draw(g);move();switch(ptDir){caseL:g.drawLine(x+Tank.WIDTH/2,y+Tank.HEIGHT/2,x,y+Tan
本文标题:java课程设计--坦克大战游戏(带源码)
链接地址:https://www.777doc.com/doc-7869855 .html