贪吃蛇游戏实验报告_第1页
贪吃蛇游戏实验报告_第2页
贪吃蛇游戏实验报告_第3页
贪吃蛇游戏实验报告_第4页
贪吃蛇游戏实验报告_第5页
已阅读5页,还剩23页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

*****************实验报告实验名称:网络编程基础实训教程(贪吃蛇)指导教师:姓名:学号:班级:提交日期:

实验目的通过开发一款贪吃蛇游戏程序,熟练掌握C#编程语言、和面向对象程序设计方法,独立完成一个游戏程序的开发。实验题目使用C#编程语言,开发一款贪吃蛇游戏,如下图所示。功能描述游戏场地是一片矩形区域的草坪。一条蛇由蛇头和蛇身组成。当游戏开始之后,草坪中出现一颗豆和一条蛇,并且蛇不停地移动,蛇移动方向与蛇头一致。当游戏暂停之后,蛇停止移动。当蛇移动时,玩家使用“↑”、“↓”、“←”和“→”四个键控制蛇的移动方向。当蛇头与豆的位置重合时,豆被蛇吃掉,同时在草坪中再生成一颗新的豆,蛇身增加一节。当蛇头碰到蛇身时,则咬断蛇身,后半部分的蛇身消失。当蛇头碰到草坪四周时,蛇立即毙命,游戏结束。需求分析根据功能描述可知,贪吃蛇游戏的系统结构图如下所示。定义数据字典如下:草坪(Lawn):草坪是贪吃蛇游戏的场地。豆和蛇只能存在于草坪范围之内。草坪具有大小和颜色等属性。蛇(Snake):在贪吃蛇游戏中,蛇由若干节组成,其中第一节是蛇头,其余是蛇身。在游戏过程中,有且仅有一条蛇,并且蛇在不停地移动。如果蛇吃了豆,则蛇生长一节。如果蛇头碰到蛇身,则咬断蛇身,后半部分的蛇身消失。如果蛇头离开草坪,则蛇死亡游戏结束。蛇具有长度、颜色、运动方向、每一节的位置等属性。豆(Bean):在贪吃蛇游戏中,豆是蛇的食物。在游戏过程中,有且仅有一颗豆。如果蛇吃了豆,则重新生成一颗豆。豆具有位置、大小和颜色等属性。设计说明根据需求分析可知,Snake的每一节都有位置和大小等属性。而Bean也具有这两个属性。抽象出二者的共同特征,抽象出一般类Block,用于描述一个块。Block派生出Bean和SnakeBlock两个类,其中SnakeBlock类用于描述蛇的一节。为了使游戏的运行更易于控制,定义Game类用于启动、暂停和继续游戏。根据需求分析可知,Lawn仅包含大小和颜色两个属性。为了减少类的数量,可将其大小和颜色等属性添加到Game类中。综上所述,在贪吃蛇游戏中,有Block(块)、Bean(豆)、SankeBlock(节)、Snake(蛇)、Game(游戏)和MainForm(用户接口)六个类。贪吃蛇游戏的逻辑模型如下图所示。MainFormMainFormGameBlockBeanSnakeBlockSnake源代码Block(块)类的编码Block是用来构成Bean(豆)和Snake(蛇)的最基本的单位,是Bean和SnakeBlock的基类。Block类的参考代码如下。usingSystem;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.Text;namespaceWindowsFormsApplication1{classBlock{protectedPointorigion;//Block的左上顶点publicconstintWIDTH=10;//Block的宽度publicconstintHEIGHT=10;//Block的高度protectedColorcolor;//Block的颜色publicBlock(){origion=newPoint(0,0);color=newColor();}publicBlock(intx,inty,Color_color){origion=newPoint(x,y);color=_color;}publicPointOrigion{get{returnorigion;}}publicvoidDisplay(Graphicsg){SolidBrushbrush=newSolidBrush(color);g.FillRectangle(brush,origion.X,origion.Y,WIDTH,HEIGHT);Penpen=newPen(Color.Black);g.DrawRectangle(pen,newRectangle(origion.X,origion.Y,WIDTH-1,HEIGHT-1));}publicvoidClear(Graphicsg,ColorbackGroundColor){SolidBrushbrush=newSolidBrush(backGroundColor);g.FillRectangle(brush,origion.X,origion.Y,WIDTH,HEIGHT);}}}2)Bean(豆)类编码Bean表示豆,是由Block派生而来的。Bean类的参考代码如下。usingSystem;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.Text;namespaceWindowsFormsApplication1{classBean:Block{publicBean(Color_color){origion=newPoint(0,0);color=_color;}publicvoidCreat(Graphicsg,ColorbackGroundColor,intlawnWidth,intlawnHeight,Snakesnake){Clear(g,backGroundColor);boolbGetAPosition=false;//是否找到生成豆的位置Randomrandom=newRandom();while(!bGetAPosition){origion.X=random.Next(0,lawnWidth-1)/WIDTH*WIDTH;origion.Y=random.Next(0,lawnHeight-1)/HEIGHT*HEIGHT;inti;for(i=0;i<snake.Length;i++){if(origion==snake.blocks[i].Origion)break;}if(i==snake.Length)bGetAPosition=true;}Display(g);}}}3)SnakeBlock(节)类编码SnakeBlock表示蛇的一节,是由Block派生而来的。SnakeBlock类的参考代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.Text;namespaceWindowsFormsApplication1{classSnakeBlock:Block{privateboolisHead;publicboolIsHead{get{returnisHead;}}publicSnakeBlock(intx,inty,Color_color,bool_isHead){origion=newPoint(x,y);color=_color;isHead=_isHead;}publicvoidChangeHeadToBody(){if(isHead)isHead=false;}publicvoidDisplay(Graphicsg,Directiondirection){base.Display(g);if(isHead){//绘制蛇眼SolidBrushbrush=newSolidBrush(Color.Brown);switch(direction){caseDirection.Up:caseDirection.Down:g.FillRectangle(brush,origion.X+WIDTH/4,origion.Y+HEIGHT/2,2,2);g.FillRectangle(brush,origion.X+WIDTH/4*3,origion.Y+HEIGHT/2,2,2);break;caseDirection.Left:caseDirection.Right:g.FillRectangle(brush,origion.X+WIDTH/2,origion.Y+HEIGHT/4,2,2);g.FillRectangle(brush,origion.X+WIDTH/2,origion.Y+HEIGHT/4*3,2,2);break;}}}}}Snake(蛇)类编码4)Snake(蛇)类编码Snake表示蛇。Snake类的参考代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.Text;namespaceHungrySanke{publicenumDirection{Up,Down,Left,Right};classSnake{privateintlength;publicDirectiondirection;privateColorcolor;publicList<SnakeBlock>blocks;privateconstintINIT_LENGTH=3;publicintLength{get{returnlength;}}publicSnake(Color_color,Direction_direction){direction=_direction;color=_color;blocks=newList<SnakeBlock>();}publicvoidCreat(Graphicsg,ColorbackGroundColor,intlawnWidth,intlawnHeight){Clear(g,backGroundColor);blocks.Clear();length=INIT_LENGTH;intx;inty;Randomrandom=newRandom();x=random.Next(lawnWidth/4,lawnWidth/4*3)/Block.WIDTH*Block.WIDTH;y=random.Next(lawnHeight/4-1,lawnHeight/4*3)/Block.HEIGHT*Block.HEIGHT;blocks.Add(newSnakeBlock(x,y,color,true));switch(direction){caseDirection.Up:for(inti=1;i<length;i++){blocks.Add(newSnakeBlock(x,y+Block.HEIGHT*i,color,false));}break;caseDirection.Down:for(inti=1;i<length;i++){blocks.Add(newSnakeBlock(x,y-Block.HEIGHT*i,color,false));}break;caseDirection.Left:for(inti=1;i<length;i++){blocks.Add(newSnakeBlock(x+Block.WIDTH*i,y,color,false));}break;caseDirection.Right:for(inti=1;i<length;i++){blocks.Add(newSnakeBlock(x-Block.WIDTH*i,y,color,false));}break;}Display(g);}publicvoidGrow(){intx=2*blocks[blocks.Count-1].Origion.X-blocks[blocks.Count-2].Origion.X;inty=2*blocks[blocks.Count-1].Origion.Y-blocks[blocks.Count-2].Origion.Y;blocks.Insert(length,newSnakeBlock(x,y,color,false));length++;}publicvoidMove(){intx=0;inty=0;blocks[0].ChangeHeadToBody();switch(direction){caseDirection.Up:x=blocks[0].Origion.X;y=blocks[0].Origion.Y-Block.HEIGHT;break;caseDirection.Down:x=blocks[0].Origion.X;y=blocks[0].Origion.Y+Block.HEIGHT;break;caseDirection.Left:x=blocks[0].Origion.X-Block.WIDTH;y=blocks[0].Origion.Y;break;caseDirection.Right:x=blocks[0].Origion.X+Block.WIDTH;y=blocks[0].Origion.Y;break;}blocks.Insert(0,newSnakeBlock(x,y,color,true));blocks.RemoveAt(blocks.Count-1);}publicvoidDisplay(Graphicsg){for(inti=0;i<length;i++){blocks[i].Display(g,direction);}}publicvoidClear(Graphicsg,ColorbackGroundColor){for(inti=0;i<length;i++){blocks[i].Clear(g,backGroundColor);}}publicvoidRemoveAfter(Graphicsg,ColorbackGroundColor,intblockNum){for(inti=length-1;i>blockNum-1;i--){blocks[i].Clear(g,backGroundColor);blocks.RemoveAt(i);length=blockNum;}}publicboolCanEatBean(Beanbean){if(blocks[0].Origion==bean.Origion)returntrue;elsereturnfalse;}publicintCanEatSnake(){for(inti=3;i<blocks.Count;i++){if(blocks[0].Origion==blocks[i].Origion)returni;}return0;}publicvoidEatBean(Beanbean,Graphicsg,ColorbackGroundColor,intlawnWidth,intlawnHeight){bean.Clear(g,backGroundColor);Grow();bean.Creat(g,backGroundColor,lawnWidth,lawnHeight,this);}publicboolIsAlive(intlawnWidth,intlawnHeight){if(blocks[0].Origion.X<0)returnfalse;if(blocks[0].Origion.Y<0)returnfalse;if(blocks[0].Origion.X+Block.WIDTH>lawnWidth)returnfalse;if(blocks[0].Origion.Y+Block.HEIGHT>lawnHeight)returnfalse;elsereturntrue;}}}5)Game(游戏)类编码Game控制游戏的运行,负责在游戏开始时生成Bean和Snake,以及负责在游戏运行中Snake的移动、Snake的生长、Bean的重生,并随时检测Snake的生死状态。Game类的参考代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.Text;namespaceHungrySanke{classGame{publicSnakesnake;publicBeanbean;publicboolisSnakeAlive;publicintlawnWidth;publicintlawnHeight;publicGame(int_lawnWidth,int_lawnHeight){Randomrandom=newRandom();intx=random.Next(0,_lawnWidth-1)/Block.WIDTH*Block.WIDTH;inty=random.Next(0,_lawnHeight-1)/Block.HEIGHT*Block.HEIGHT;Directiondirection=(Direction)random.Next(1,4);snake=newSnake(Color.YellowGreen,direction);bean=newBean(Color.Pink);isSnakeAlive=false;lawnWidth=_lawnWidth;lawnHeight=_lawnHeight;}publicvoidBegin(Graphicsg,ColorbackGroundColor,intlawnWidth,intlawnHeight){isSnakeAlive=true;snake.Clear(g,backGroundColor);snake.Creat(g,backGroundColor,lawnWidth,lawnHeight);bean.Creat(g,backGroundColor,lawnWidth,lawnHeight,snake);}publicvoidOnTime(Graphicsg,ColorbackGroundColor,intlawnWidth,intlawnHeight){if(isSnakeAlive){snake.Clear(g,backGroundColor);snake.Move();snake.Display(g);bean.Display(g);if(snake.CanEatBean(bean)){bean.Clear(g,backGroundColor);snake.EatBean(bean,g,backGroundColor,lawnWidth,lawnHeight);bean.Display(g);}intblockNum=snake.CanEatSnake();if(blockNum>0){snake.RemoveAfter(g,backGroundColor,blockNum);}if(!snake.IsAlive(lawnWidth,lawnHeight))isSnakeAlive=false;}}}}Form1的设计代码usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceHungrySanke{publicpartialclassForm1:Form{privateGamegame;publicForm1(){InitializeComponent();game=newGame(lawn.Width,lawn.Height);}privatevoidForm1_Load(objectsender,EventArgse){}privatevoidtimer1_Tick(objectsender,EventArgse){if(game.isSnakeAlive){Graphicsg;g=lawn.CreateGraphics();game.OnTime(g,lawn.BackColor,lawn.Width,lawn.Height);if(!game.isSnakeAlive){MessageBox.Show("GameOver");}}}privatevoidToolStrpMenuItemStart_Click(objectsender,EventArgse){Graphicsg;g=lawn.CreateGraphics();game.Begin(g,lawn.BackColor,lawn.Width,lawn.Height);timer1.Enabled=true;ToolStripMenuItemPause.Enabled=true;ToolStripMenuItemContinue.Enabled=false;}privatevoidToolStripMenuItemPause_Click(objectsender,EventArgse){timer1.Enabled=false;ToolStripMenuItemPause.Enabled=false;ToolStripMenuItemContinue.Enabled=true;}privatevoidToolStripMenuItemCONTINUE_Click(objectsender,EventArgse){timer1.Enabled=true;ToolStripMenuItemPause.Enabled=true;ToolStripMenuItemContinue.Enabled=false;}privatevoidForm1_KeyDown(objectsender,KeyEventArgse){if(game.snake.direction==Direction.Down||game.snake.direction==Direction.Up){switch(e.KeyCode){caseKeys.Left:game.snake.direction=Direction.Left;break;caseKeys.Right:game.snake.direction=Direction.Right;break;}}else

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论