您好,欢迎访问三七文档
C#实验实验报告专业:_软件工程_班级:____学号:学生姓名:___计算机科学与技术学院年6月15日南通大学实验报告学院:计算机科学与技术学院专业:软件工程班级:姓名学号实验组1实验时间指导教师成绩实验项目名称C#坦克大战实验目的1.了解文件流的概念。2.学会文本文件的读写。3.学会窗体程序的书写。实验内容写一则坦克大战的程序并完美运行。第3页共12页实验环境Pc机;Visualstudio环境。Win764位。实验步骤windows窗体应用程序的实验步骤。序列化、二进制文件的读写及随机记录的读取实验程序实验内容程序:Bullet代码如下:usingSystem.Drawing;//add//addnamespace坦¬1克?{internalclassbullet{privatereadonlybooltype;//己o方¤?子Á¨®弹Ì¡¥true,敌ÌD方¤?子Á¨®弹Ì¡¥falseprivateintdirect;//子Á¨®弹Ì¡¥行D进?方¤?向¨°privateintheight=32;privateintleft;第4页共12页privateinttop;//子Á¨®弹Ì¡¥坐Á?标À¨º(ꡧTop,Left)privateintwidth=32;publicbullet(inttype)//子Á¨®弹Ì¡¥类¤¨¤构1造¨¬函¡¥数ºy{if(type==6)//己o方¤?this.type=true;elsethis.type=false;}publicintTop//Top属º?性?{get{returntop;}set{top=value;}}publicintLeft//Left属º?性?{get{returnleft;}set{left=value;}}publicintDirect//Direct属º?性?(子Á¨®弹Ì¡¥行D进?方¤?向¨°)ê?{get{returndirect;}set{direct=value;}}publicvoidmove(){switch(Direct){case0:Top--;break;case1:Top++;break;case2:Left--;break;case3:Left++;第5页共12页break;}}publicvoidDraw(Graphicsg){ImagebulletImage;if(type)//己o方¤?bulletImage=Image.FromFile(BMP/missile1.bmp);elsebulletImage=Image.FromFile(BMP/missile2.bmp);//得Ì?到Ì?绘?制?这a个?子Á¨®弹Ì¡¥图ª?形?的Ì?在¨²游®?戏¡¤面?板ã?中D的Ì?矩?形?区?域®¨°RectangledestRect=newRectangle(left*width,top*height,width,height);RectanglesrcRect=newRectangle(0,0,width,height);g.DrawImage(bulletImage,destRect,srcRect,GraphicsUnit.Pixel);}publicboolhitE(inttanktype)//是º?否¤?击¡Â中D对?方¤?坦¬1克?{if(type==false)//敌ÌD方¤?子Á¨®弹Ì¡¥if(tanktype=2&&tanktype=5)//坦¬1克?的Ì?类¤¨¤型¨ª(2---5敌ÌD方¤?,ê?6己o方¤?)ê?returnfalse;elsereturntrue;if(type)//己o方¤?子Á¨®弹Ì¡¥if(tanktype==6)//坦¬1克?的Ì?类¤¨¤型¨ª(2---5敌ÌD方¤?,ê?6己o方¤?)ê?returnfalse;elsereturntrue;returnfalse;}}}Playsound代码如下:usingSystem.Runtime.InteropServices;namespaceTank{publicclassPlaySound{privateconstintSND_SYNC=0x0;第6页共12页privateconstintSND_ASYNC=0x1;privateconstintSND_NODEFAULT=0x2;privateconstintSND_LOOP=0x8;privateconstintSND_NOSTOP=0x10;publicstaticvoidPlay(stringfile){intflags=SND_ASYNC|SND_NODEFAULT;sndPlaySound(file,flags);}[DllImport(winmm.dll)]privatestaticexternintsndPlaySound(stringfile,intuFlags);}}Program代码如下:usingSystem;usingSystem.Windows.Forms;namespace坦¬1克?{internalstaticclassProgram{///summary///应®|用®?程¨¬序¨°的Ì?主¡Â入¨?口¨²点Ì?。¡ê////summary[STAThread]privatestaticvoidMain(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(newForm1());}}}Tank代码如下:usingSystem;usingSystem.Collections;usingSystem.Drawing;usingTank;//add第7页共12页//addnamespace坦¬1克?{internalclassTank{privatereadonlyintheight;//坦¬1克?的Ì?高?度¨¨privatereadonlyintwidth;//坦¬1克?的Ì?宽¨ª度¨¨publicArrayListbList=newArrayList();//子Á¨®弹Ì¡¥序¨°列¢Dprivateintdirect;//0--上¦?,1--下?,2--左Á¨®,3--右®¨°privateintleft;//坦¬1克?位?置?的Ì?横¨¢坐Á?标À¨ºprivateinttop;//坦¬1克?位?置?的Ì?纵ÁY坐Á?标À¨ºprivateinttype;//坦¬1克?的Ì?类¤¨¤型¨ª(2---5敌ÌD方¤?,ê?6己o方¤?)ê?publicTank(inttank_type)//构1造¨¬函¡¥数ºy{Randomr=newRandom();direct=r.Next(0,4);//产¨²生¦¨²0—a3的Ì?数ºywidth=32;height=32;left=r.Next(0,10);//产¨²生¦¨²0—a9的Ì?数ºytop=r.Next(0,10);//产¨²生¦¨²0—a9的Ì?数ºytype=tank_type;}publicintTop//Top属º?性?{get{returntop;}set{if(top=0&&top=9){top=value;//if(top==0||top==9)newDirect();}}}publicintType//坦¬1克?的Ì?类¤¨¤型¨ª属º?性?{get{returntype;}set{第8页共12页if(top=1&&top=5){type=value;}}}publicintLeft//Left属º?性?{get{returnleft;}set{if(left=0&&left=9){left=value;//if(left==0||left==9)newDirect();}}}publicintDirect//Direct属º?性?(坦¬1克?方¤?向¨°)ê?{get{returndirect;}set{direct=value;}}publicvoidnewDirect()//改?变À?方¤?向¨°{Randomr=newRandom();intnew_Direct=r.Next(0,4);//产¨²生¦¨²0—a3的Ì?数ºywhile(direct==new_Direct)new_Direct=r.Next(0,4);//产¨²生¦¨²0—a3的Ì?数ºydirect=new_Direct;}publicvoidDraw(Graphicsg,inttype)//根¨´据Y坦¬1克?类¤¨¤型¨ª选?择?不?同ª?图ª?片?{ImagetankImage=Image.FromFile(BMP/ETANK1.BMP);if(type==2)tankImage=Image.FromFile(BMP/ETANK2.BMP);if(type==3)tankImage=Image.FromFile(BMP/ETANK3.BMP);if(type==4)tankImage=Image.FromFile(BMP/ETANK4.BMP);if(type==5)tankImage=Image.FromFile(BMP/ETANK1.BMP);if(type==6)tankImage=Image.FromFile(BMP/MYTANK.BMP);//得Ì?到Ì?绘?制?这a个?坦¬1克?图ª?形?的Ì?在¨²游®?戏¡¤面?板ã?中D的Ì?矩?形?区?域®¨°第9页共12页RectangledestRect=newRectangle(left*width,top*height,width,height);RectanglesrcRect=newRectangle(direct*width,0,width,height);g.DrawImage(tankImage,destRect,srcRect,GraphicsUnit.Pixel);}publicvoidExplore(Graphicsg)//坦¬1克?爆À?炸¡§动¡¥画-{//得Ì?到Ì?绘?制?这a个?坦¬1克?图ª?形?的Ì?在¨²游®?戏¡¤面?板ã?中D的Ì?矩?形?区?域®¨°RectangledestRect=newRectangle(left*width,top*height,width,height);RectanglesrcRect=newRectangle(0,0,width,height);ImagetankImage=Image.FromFile(BMP/explode1.bmp);g.DrawImage(tankImage,destRect,srcRect,GraphicsUnit.Pixel);tankImage=Image.FromFile(BMP/explode1.bmp);g.DrawImage(tankImage,destRect,srcRect,GraphicsUnit.Pixel);tankImage=Image.FromFile(BMP/explode2.bmp);g.DrawImage(tankImage,destRect,srcRect,GraphicsUnit.Pixel);PlaySound.Play(Sound/Explode.wav);}publicvoidfire(){bulletb=newbullet(type);//根¨´据Y坦¬1克?产¨²生¦¨²不?同ª?子Á¨®弹Ì¡¥b.Direct=Direct;//坦¬1克?的Ì?朝¡¥向¨°b.Top=Top;b.Left=Left;//b.move();bList.Add(b);if(type==6)PlaySound.Play(Sound/Shoot.wav);//己o方¤?发¤¡é射¦?出?声¦¨´}public
本文标题:C#综合实验报告
链接地址:https://www.777doc.com/doc-6334946 .html