贪吃蛇实训总结报告_第1页
贪吃蛇实训总结报告_第2页
贪吃蛇实训总结报告_第3页
贪吃蛇实训总结报告_第4页
贪吃蛇实训总结报告_第5页
已阅读5页,还剩28页未读 继续免费阅读

下载本文档

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

文档简介

郑州轻工业学院实训汇报实训名称:贪吃蛇指导老师:姓名:学号:班级:提交日期: 一.试验目标经过开发一款贪吃蛇游戏程序,熟练掌握C#编程语言、和面向对象程序设计方法,独立完成一个游戏程序开发。二.试验题目此次实训题目为贪吃蛇。经过使用C#编程语言来实现。三.功效描述1.游戏基础功效描述游戏场地是一片矩形区域空地组成,蛇能够在这片定义区域中自由移动。定义一个Block来组成石块,蛇头,蛇身和豆。蛇由蛇头和蛇身组成。当游戏开始以后,定义区域中出现一颗豆和一条蛇,而且蛇不停地移动,蛇移动方向和蛇头方向一致。经过space或暂停键能够使游戏停止,蛇停止移动,分数停止积分。当蛇移动时,玩家能够使用“↑”、“↓”、“←”和“→”四个方向键改变蛇移动方向。当蛇头和豆位置重合时,豆被蛇吃掉,同时在草坪中再生成一颗新豆,蛇身增加一节。当蛇头碰到石块时,蛇死亡,游戏结束。当蛇头咬到蛇身时,则蛇死亡,游戏结束。当蛇头碰到草坪四面时,蛇立即毙命,游戏结束。游戏分为不一样等级,第一关游戏速度为200,当蛇吃五个豆子就能够过关,等级显示为2,速度增加100变为300.第二关为吃够10个豆子,速度增加50,以后以这类推。游戏菜单中设有游戏帮助,玩家能够经过点击帮助了解游戏玩法。对于不是第一次玩玩家能够改变蛇运动速度,来增加难度。在菜单中设有加速和减速两个键,单击能够改变速度。游戏菜单中还有时间显示。当游戏暂停时,能够经过“继续”键使游戏继续进行。四.需求分析本游戏用户能够自己练习和娱乐。本游戏需要满足以下几点要求:利用方向键“↑、↓、←、→”来改变蛇运行方向。空格键暂停游戏,并在随机地方产生食物。吃到食物就变成新蛇体,碰到壁或本身则游戏结束,不然正常运行。可行性分析:贪吃蛇游戏是一个简单大众游戏,自从计算机实现以来,深受广大电脑玩家喜爱,做一个简单贪吃蛇小游戏。贪吃蛇关键算法是怎样实现蛇移动和吃掉食物后怎样变成新蛇体。没有碰到食物时候,把目前运动方向上下个节点入队,并以蛇节点颜色绘制这个节点,然后把头指针所指节点出队,并以游戏框架内部背景色重绘出队节点,这么就能够达成移动效果。而在吃到食物时候,则只需把食物入队即可。实现贪吃蛇基础蛇身移动、吃到食物身体增加、和碰到墙壁或本身死亡,能够暂停和开始。玩家能够依据自己水平选择不一样等级进行游戏挑战。贪吃蛇游戏中定义以下:1) 空白区域(Lawn):定义区域是贪吃蛇游戏场地。豆、石头和蛇只能存在于空白区域范围之内。依据个人爱好还能够添加背景,改变区域大小和颜色。2) 蛇(Snake):在贪吃蛇游戏中,蛇由若干节组成,其中第一节是蛇头,在蛇头上面定义两个点,作为蛇眼睛,其它是蛇身。在游戏过程中,有且仅有一条蛇,而且蛇在不停地移动。假如蛇吃了豆,则蛇生长一节。假如蛇头碰到蛇身,蛇死亡,游戏结束。假如蛇头离开所定义区域,则蛇死亡游戏结束。当蛇头撞到定义石块上时候游戏结束。在定义蛇时候能够改变蛇初始长度,也能够改变蛇颜色和大小。3) 豆(Bean):在贪吃蛇游戏中,豆是蛇食物。在游戏过程中,有且仅有一颗豆。假如蛇吃了豆,则重新生成一颗豆。豆出现是随机性。4)石块(stone):游戏中石块和豆是同时出现,不一样是,豆是随机产生,而石块是固定,它坐标在写代码时候就定义好了,不能够改变。它大小和颜色也能够随便改变。5)菜单(MenuStrip):在贪吃蛇游戏中有游戏菜单,里面有开局、暂停、继续、加速、减速、帮助等菜单。还有Label控件,显示速度、时间、日期和积分。五.设计说明Snake每一节全部有位置和大小等属性。而Bean和stone也含有这两个属性。抽象出这三者共同特征,抽象出通常类Block,用于描述一个块。Block派生出Bean和SnakeBlock两个类,其中SnakeBlock类用于描述蛇一节。为了使游戏运行更易于控制,定义Game类用于开启、暂停和继续游戏。依据需求分析可知,Lawn仅包含大小和颜色两个属性。为了降低类数量,可将其大小和颜色等属性添加到Game类中。总而言之,在贪吃蛇游戏中,有Block(块)、Bean(豆)、SankeBlock(节)、Snake(蛇)、Game(游戏)和MainForm(用户接口)六个类。游戏逻辑构思图以下:定义空白区域定义空白区域游戏开始游戏开始生成石块生成石块生成豆生成蛇生成豆生成蛇重新开局重新开局 吃到蛇身增加 豆蛇身增加增加等级增加等级统计分数石块统计分数石块碰到石壁碰到石壁游戏结束游戏结束游戏设计以下:游戏图标及名称游戏图标及名称豆子豆子成绩显示速度显示成绩显示速度显示游戏菜单蛇身等级显示石块蛇身等级显示石块游戏定游戏定义区域日期立即间显示六.源代码usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceWindowsFormsApplication1{publicpartialclassMainForm:Form{inta=200;//定义初速度为200privateGamegame;//DateTimec;publicMainForm(){InitializeComponent();game=newGame(lawn.Width,lawn.Height);timer1.Enabled=true;timer1.Interval=a;timer1.Enabled=false;Velocity.Text=a.ToString();}Block是用来组成Bean(豆)和Snake(蛇)还有Stone(石块)最基础单位,是Bean和SnakeBlock基类。Block类参考代码以下。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);}}这是对豆定义,是由Block派生而来,在系统开始时,使豆产生,它出现位置为随机生成。其代码以下:classBean:Block{publicstaticintf=0;//吃到豆子个数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);f++;}newpublicvoidDisplay(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));}//Display(g);}SnakeBlock表示蛇一节,是由Block派生而来。它组成蛇身,其代码以下所表示: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.Black);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;}}}}控制蛇头运动方向publicenumDirection{Up,Down,Left,Right};对蛇编码也是由Block派生而来,在这里面能够定义蛇颜色和长度。classSnake{publicstaticbooleatebean;privateintlength;publicDirectiondirection;privateColorcolor;publicList<SnakeBlock>blocks;privateconstintINIT_LENGTH=10;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;}}石块定义定义以下,在空白区域中定义一个位置(坐标),使之固定不动,在蛇头撞到这个坐标时候,参数传到判定蛇生死函数,时蛇死亡,游戏结束。publicboolMeetStone(Pointorigion){if((blocks[0].Origion.X==(origion.X+40)&&blocks[0].Origion.Y==(origion.Y+40))||(blocks[0].Origion.X==(origion.X+300)&&blocks[0].Origion.Y==(origion.Y+80))||(blocks[0].Origion.X==(origion.X+150)&&blocks[0].Origion.Y==(origion.Y+200))||(blocks[0].Origion.X==(origion.X+100)&&blocks[0].Origion.Y==(origion.Y+400)))returntrue;elsereturnfalse;}当蛇碰到一个豆时,使豆成为蛇体一部分,其颜色和大小跟蛇一样,蛇身增加。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);eatebean=true;}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;}}Game控制游戏运行,负责在游戏开始时生成Bean、Stone和Snake,和负责在游戏运行中Snake移动、Snake生长、Bean重生和石块生产。并随时检测Snake生死状态。Game类参考代码以下:classGame{Pointorigion;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.Red,direction);bean=newBean(Color.Red);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);游戏中石块填充,能够依据自己制订坐标来固定石块,能够改变其颜色。SolidBrushbrush=newSolidBrush(Color.DarkViolet);Penpen=newPen(Color.DarkViolet);g.FillRectangle(brush,origion.X+40,origion.Y+40,10,10);g.DrawRectangle(pen,newRectangle(origion.X+40,origion.Y+40,10,10));g.FillRectangle(brush,origion.X+300,origion.Y+80,10,10);g.DrawRectangle(pen,newRectangle(origion.X+300,origion.Y+80,10,10));g.FillRectangle(brush,origion.X+150,origion.Y+200,10,10);g.DrawRectangle(pen,newRectangle(origion.X+150,origion.Y+200,10,10));g.FillRectangle(brush,origion.X+100,origion.Y+400,10,10);g.DrawRectangle(pen,newRectangle(origion.X+100,origion.Y+400,10,10));}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);isSnakeAlive=false;MessageBox.Show("就算再饿也不能咬自己啊,死了吧!");}if(!snake.IsAlive(lawnWidth,lawnHeight)||snake.MeetStone(origion))isSnakeAlive=false;}}}Timer组件用于在用户定义时间间隔引发事件。在游戏运行过程中,经过时间函数能够判定蛇生死,蛇吃豆子多少。还有日期显示,时间显示。统计蛇吃豆子多少。当吃够要求豆时候,等级增加,游戏速度增加,蛇运动速度增加。privatevoidtimer1_Tick(objectsender,EventArgse){Date.Text=DateTime.Today.ToLongDateString();time.Text=DateTime.Now.ToLongTimeString();Success.Text=(Bean.f*10).ToString();if(Bean.f==5){jibie.Text=(1+(Bean.f+1)%5).ToString();timer1.Enabled=true;timer1.Interval=100;Velocity.Text=(a+100).ToString();}if(Bean.f==10){jibie.Text=(2+(Bean.f+1)%10).ToString();timer1.Enabled=true;timer1.Interval=80;Velocity.Text=(a+150).ToString();}if(Bean.f==20){jibie.Text=(3+(Bean.f+1)%20).ToString();timer1.Enabled=true;timer1.Interval=70;Velocity.Text=(a+200).ToString();}if(Bean.f==30){jibie.Text=(3+(Bean.f+1)%30).ToString();timer1.Enabled=true;timer1.Interval=60;Velocity.Text=(a+250).ToString();}if(Bean.f==40){jibie.Text=(3+(Bean.f+1)%40).ToString();timer1.Enabled=true;timer1.Interval=50;Velocity.Text=(a+300).ToString();}if(Bean.f==50){MessageBox.Show("闯关成功!");}判定蛇生死if(game.isSnakeAlive){Graphicsg;g=lawn.CreateGraphics();game.OnTime(g,lawn.BackColor,lawn.Width,lawn.Height);if(!game.isSnakeAlive){MessageBox.Show("死翘翘!");jibie.Text=1.ToString();}}}游戏开始按钮,游戏开始,正确数据全部初始化,其中有timer函数,和等级,分数全部初始化,蛇运行速度也初始到原来速度。豆统计清零。privatevoidToolStripMenuItemStart_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;Bean.f=0;timer1.Interval=200;Velocity.Text=a.ToString();c=DateTime.Now;}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;}KeyDown事件。在贪吃蛇游戏中,使用“↑”、“↓”、“←”和“→”四个方向键改变蛇移动方向。KeyDown事件用于在用户按键时,提供改变蛇移动方向功效。privatevoidMainForm_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{switch(e.KeyCode){

温馨提示

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

评论

0/150

提交评论