还剩19页未读,继续阅读
本资源只提供10页预览,全部文档请下载后查看!喜欢就下载吧,查找使用更方便
文本内容:
郑州轻工业学院实训报告实训名称:指导教师:姓名:学号:班级:提交日期:g.FillRectanglebrush,origion.X+WIDTH/2,origion.Y+HEIGHT/4*3,2,2;break;}}(蛇)类编码
4.Snakeusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Drawing;namespace贪食蛇{public enumDirectionUp,Down,Left,Right;class Snake{private intlength;public Direction direction;private Colorcolor;public ListSnakeBlockblocks;private constint INITLENGTH=3;public intLengthget returnlength;}public SnakeColor_color,Direction_direction direction=direction;color=_color;blocks=new ListSnakeBlock;public voidGreatGraphics g,Color backGroundColor,int lawnWidth,int lawnHeightClear g,backGroundColor;blocks.Clear;length=INIT_LENGTH;int x=50;//防止石头与蛇相遇,固定蛇的初始位置int y=440;〃防止石头与蛇相遇,固定蛇的初始位置Random random=new Random;blocks.Addnew SnakeBlockx,y,color,true;switch directioncase Direction.Up:for int i=1;ilength;i++blocks.Add new SnakeBlockx,y+Block.HEIGHT*i,color,false;break;case Direction.Down:for int i=1;ilength;i++{blocks.Add newSnakeBlock x,y-Block.HEIGHT*i,color,false;break;case Direction.Left:for inti=1;ilength;i++{blocks.Addnew SnakeBlockx+Block.WIDTH*i,y,color,false;break;case Direction.Right:for inti=1;ilength;i++{blocks.Addnew SnakeBlockx-Block.WIDTH*i,y,color,false;break;}Display g;public voidGrowint x=2*blocks[blocks.Count-1],Origion.X一blocks[blocks.Count-2].Origion.X;int y=2*blocks[blocks.Count-1].Origion.Y-blocks[blocks.Count-2].Origion.Y;blocks.Insertlength,new SnakeBlockx,y,color,false;length++;public voidMove int x=0;int y=0;blocks
[0].ChangeHeadToBody;switch directioncase Direction.Up:x二blocks
[0].Origion.X;y=blocksEO].Origion.Y-Block.HEIGHT;break;case Direction.Down:x=blocks
[0].Origion.X;y=blocks
[0].Origion.Y+Block.HEIGHT;break;case Direction.Left:x=blocks
[0].Origion.X-Block.WIDTH;y=blocks
[0].Origion.Y;break;case Direction.Right:x=blocks
[0].Origion.X+Block.WIDTH;y=blocks
[0].Origion.Y;break;}blocks.Insert0,newSnakeBlockx,y,color,true;blocks.RemoveAt blocks.Count-1;public voidDisplayGraphics g{for inti=0;ilength;i++blocks[i].Displayg,direction;public voidClearGraphics g,Color backGroundColorfor inti=0;ilength;i++blocks[i].Clearg,backGroundColor;}public voidRemoveAfterGraphics g,Color backGroundColor,int blockNumfor inti=length-1;iblockNum-1;i一blocks[i].Clear g,backGroundColor;blocks.RemoveAt i;length=blockNum;}}public boolCanEatBeanBean beanif blocks
[0].Origion二二bean.Origion return true;elsereturn false;}public boolSnakeMeetStonePoint stonepoint{if blocks
[0].Origion.X=stonepoint.Xblocks
[0].Origion.X=stonepoint.X+149blocks
[0].Origion.Y=stonepoint.Yblocks
[0]・Origion.Y=stonepoint.Y+9return true;else return false;}public boolSnakeMeetStone2Point stonepoint2if blocks
[0].Origion.X=stonepoint
2.Xblocks
[0].Origion.X=stonepoint
2.X+149blocks
[0].Origion.Y=stonepoint
2.Yblocks
[0].Origion.Y=stonepoint
2.Y+9returntrue;elsereturn false;}public intCanEatSnakefor inti=3;iblocks.Count;i++ifblocks
[0].Origion==blocks[iL Origionreturn i;return0;public voidEatBeanBean bean,Graphics g,Color backGroundColor,int lawnWidth,intlawnHeightPoint stonepoint二Game.stonepoint;Point stonepoint2=Game.stonepoint2;bean.Clearg,backGroundColor;Grow;bean.Creat g,backGroundColor,lawnWidth,lawnHeight,this,stonepoint,stonepoint2;publicblocks
[0].Origion.Xif0return false;blocks
[0].Origion.Yif0returnfalse;blocks
[0].Origion.Xif Block.WIDTHlawnWidthreturn false;blocks
[0].Origion.Yif Block.HEIGHTlawnHeightreturn false;bool IsAliveint lawnWidth,int lawnHeightelsereturntrue;(游戏)类设计
5.GameGame控制游戏的运行,负责在游戏开始时生成Bean和Snake,以及负责在游戏运行中Snake的移动、Snake的生长、Bean的重生,以及分数的增加,并随时检测Snake的生死状态Game类的参考代码如下:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Drawing;namespace贪食蛇class Game:Blockpublic staticPoint stonepoint;public staticPoint stonepoint2;public Snake snake;public Beanbean;public boolisSnakeAlive;public int lawnWidth;public int lawnHeight;public Gameint lawnWidth,intlawnHeightRandom random=new Random;intx二random.Next0,_lawnWidth-1/Block.WIDTH*Block.WIDTH;int y=random.Next0,_lawnHeight-1/Block.HEIGHT*Block.HEIGHT;Direction direction=Directionrandom.Next1,4;snake=new SnakeColor.YellowGreen,direction;bean二new BeanColor.Pink;isSnakeAlive=false;lawnWidth=_1awnWidth;lawnHeight=lawnHeight;}public voidBegin Graphics g,Color backGroundColor,intlawnWidth,int lawnHeightg.ClearColor.Turquoise;int stoneway X,stoneway Y;int stoneway_X2,stoneway_Y2;Random random=new Random;int stonewayl_X=random.Next2,29;〃让障碍物以10个单位生成int stoneway1Y=random.Next2,39;〃让障碍物以10个单位生成int stoneway2_X=random.Next10,30;〃让障碍物以10个单位生成int stoneway2_Y二random.Next39,47;〃让障碍物以10个单位生成int[]array x={stonewaylX,stoneway2X;int[]array_y={stonewayl_Y,stoneway2_Y;inti=random.Next0,2;int j=random.Next0,2;stoneway_X=10*array_x[i];stoneway_Y=10*array_y[i];stoneway_X2=10*array x[j];stoneway_Y2=10*array y[j];isSnakeAlive=true;snake.Clearg,backGroundColor;snake.Greatg,backGroundColor,lawnWidth,lawnHeight;bean.Great g,backGroundColor,lawnWidth,lawnHeight,snake,stonepoint,stonepoint2;g.FillRectanglenew SolidBrushColor.Red,stonewayX,stoneway Y,150,10;〃障碍物的显示g.FillRectanglenew SolidBrushColor.Red,stoneway_X2,stoneway_Y2,150,10;//障碍物的显示stonepoint.X二stoneway_X;stonepoint.Y=stoneway Y;stonepoint
2.X二stoneway_X2;stonepoint
2.Y=stoneway_Y2;Bean,score=0;Bean,grade=0;}public voidOnTimeGraphics g,Color backGroundColor,intlawnWidth,int lawnHeightif isSnakeAlivesnake.Clearg,backGroundColor;snake.Move;snake.Display g;bean.Display g;if snake.CanEatBeanbean bean.Clearg,backGroundColor;snake.EatBeanbean,g,backGroundColor,lawnWidth,lawnHeight;bean.Displayg;}int blockNum=snake.CanEatSnake;if blockNum0snake.RemoveAfterg,backGroundColor,blockNum;Bean,score=0;if!snake.IsAlivelawnWidth,lawnlleight||snake.SnakeMeetStone stonepoint||snake.SnakeMeetStone2stonepoint2isSnakeAlive=false;}}主界面类设计
6.MainFormMainForm可通过继承实现,通过向MainForm中添加控件及设置控件的属性系统会自动向MainForm类中添加相应的对象字段,如本程序的menuStrip1和timerl等1添加属性添加属性game,来控制游戏的开始和运行,参考代码如下private Gamegame;2修改构造函数在构造函数的最后对属性game进行实例化,参考代码如下game=new Gamelawn.Width,lawn.Height;3Panal容器Panel容器作为游戏运行的场地蛇Snake豆Bean和石头Stone都在Panel容器中绘制Panel容器的添加步骤如下•在MainForm中添加一个Panel容器口Panelo•拖动Panel容器的大小,使其充满整个窗体•修改Panel容器的属性Name=lawn•修改Panel容器的属性BackColor=Turquoiseo4Timer组件Timer组件用于在用户定义的时间间隔引发事件Timer组件的添加步骤如下•在MainForm中添加一个Timer组件®Timero•修改Timer组件的属性Enalbed=false•双击®Timer,生成Tick事件Tick事件的参考代码如下label
4.Text=DateTime.Today.ToShortDateString;label
5.Text二DateTime.Now.ToLongTimeString;if game.isSnakeAlive Graphics g;g=lawn.CreateGraphics;game.OnTimeg,lawn.BackColor,lawn.Width,lawn.Height;if!game.isSnakeAlive if MessageBox.Show〃游戏结束!!!\n需要再来一局吗〃,〃提示〃,MessageBoxButtons.YesNo,MessageBoxIcon.Warning二二DialogResult.Yes game.Beging,lawn.BackColor,lawn.Width,lawn.Height;timerl.Enabled=true;label
8.Enabled=true;label
9.Enabled=false;label
2.Text=Bean,score.ToStringO;labelll.Text=Bean,grade.ToStringO;timerl.Interval二150-Bean,grade*20;labell
3.Text=timerl.Interval.ToStringO;}•在MainForm中再添加一个Timer组件®Timero
②•双击mer生成「ck事件Tick事件的参考代码如下private voidtimer2_Tick objectsender,EventArgs eif game.isSnakeAlive timer
2.Interval=1000;labell
5.Text=string.Format zz{0}:{1}:{2},z,hour.ToStringO,min.ToStringO,sec.ToStringO;sec++;if sec==60min++;sec=0;if min==60hour++;min=0;if!game.isSnakeAlivetimer
2.Enabled二true;}5Label控件Label控件可用于提供用户可以使用的功能Label控件的添加步骤如下•••••在MainForm中添加一个label ALabel•••••••••••••••••••・••分别修改label控件的属性•分别设置label控件的Text属性,使其分别显示为开始游戏Q,退出游戏区,暂停理,继续旦,游戏得分等一系列控件属性,•双击“开始游戏Q”生成Click事件Click事件的参考代码如下Graphicsg;g=lawn.CreateGraphics;game.Beging,lawn.BackColor,lawn.Width,lawn.Height;timerl.Enabled=true;.实训目的1I通过开发一款贪吃蛇小游戏程序,使自己熟练掌握C#编程语言、和面向对象程序设计方法使自己在掌握VC#面向对象编程理论的基础上,推动自己初步掌握基于VC#的WinForm程序的编程方法,为今后的网络协议编程和Web及Web服务编程打下基础实训题目
2.使用C#语言在VS平台上编程实现贪食蛇小游戏的运行,如下图所示:贪食蛇开始游戏@游戏得分7退出游戏⑻游戏等级1哲停⑪游戏速度130游戏运行时现在时间间2011-9-813回区timer
2.Enabled=true;label
8.Enabled=true;label
9.Enabled=false;双击暂停“亚”,生成Click事件•000000008Click事件的参考代码如下timerl.Enabled=false;label
8.Enabled=false;label
9.Enabled=true;双击菜单项“继续E”,生成Click事件•Click事件的参考代码如下timerl.Enabled=true;label
8.Enabled=true;label
9.Enabled=false;双击菜单项“退出游戏R”,生成Click事件•Click事件的参考代码如下if MessageBox.Show〃确实要退出游戏吗?”、〃提示〃,MessageBoxButtons.YesNo,MessageBoxIcon.Information二二DialogResult.Yesthis.Close;6KeyDown事件在贪吃蛇游戏中,使用“,、1”、“一”和“一”四个方向键改变蛇的移动方向,使用“Q”控制游戏的开始,“E”控制游戏的继续,“W”控制游戏的暂停,“R”控制游戏退出KeyDown事件用于在用户按方向键时,提供改变蛇移动方向;按此几个快捷键时,控制游戏的各种功能KeyDown事件的添加步骤如下•双击事件KeyDown,生成MainForm的KeyDown事件•KeyDown事件的参考代码如下string S=;S=e.KeyCode.ToString;switch S〃八〃caseQ:Graphicsg;g=lawn.CreateGraphics;game.Beging,lawn.BackColor,lawn.Width,lawn.Height;timerl.Enabled=true;label
8.Enabled=true;label
9.Enabled=false;break;case W:timerl.Enabled=false;label
8.Enabled=false;label
9.Enabled=true;break;〃「〃case E:timerl.Enabled true;label
8.Enabled true;label
9.Enabled false;break;case〃R〃ifMessageBox.Show〃你要退出游戏吗?〃,〃提示〃,MessageBoxButtons.YesNo,MessageBoxIcon.Information==DialogResult.Yes this.Close;break;ifgame,snake,direction==Direction.Down||game,snake,direction==Direction.Up{if S=〃Left〃game,snake,direction=Direction.Left;if S二二〃Rightgame,snake,direction=Direction.Right;}else ifS=〃Down〃game,snake,direction=Direction.Up;game,snake,direction二Direction.Down;ifS=〃Up〃.测试报告7的生成与显示1Bean1)测试程序是否能在游戏开始时,生成一颗豆2)测试程序是否能在游戏运行中,正常显示一颗豆3)测试程序是否能在豆被蛇吃掉时,消除原先的豆,并重新生成一颗豆)的生成、显示、移动与生长2Snake1)测试程序是否能在游戏开始时,生成一条蛇2)测试程序是否能在游戏运行中,正常显示一条蛇的运动3)测试程序是否能在游戏运行中,根据用户的按键情况,改变蛇的移动方向,并控制游戏的各种功能4)测试程序是否能在蛇吃掉豆时,生长一节5)测试程序是否能在蛇头碰到蛇身时,咬断蛇身,分数为零,但是游戏的等级不会发生改变6)测试程序是否能在蛇头碰到场地边界或者碰到石块时死亡,•・••••••••••结束游戏,游戏运行时间停止,并且提示再来一局的提示,如果玩家确定则游戏再次运行.游戏升级8此贪食蛇小游戏的功能升级描述如下1)增加了显示当前时间,和游戏速度的功能,当游戏运行时显示当前的北京时间,和当前游戏的运行速度,游戏的运行时间2)增加计分功能当游戏开始时,分数清零蛇每吃一颗豆,分数加1当蛇咬断蛇身时,分数再次清零,但是游戏的等级不会发生改变,相对应的速度也不会发生改变3)当游戏开始时,草坪中生成若干石块如果蛇头碰到石块,则蛇立即毙命,游戏结束,游戏运行时间停止,并且提示玩家是否再来一局,如果玩家确定则游戏再次运行4)增加了游戏等级设定,以蛇每吃到5个豆时,等级升1,同时游戏速度会发生改变.心得体会10通过这次实训,初步了解和掌握winform程序,了解WinForm程序的作用与意义,更加全面的了解C#这门面向对象的语言的特点与优势让我明白了实践的重要性,理论和实践相结合的必要性,也让我感受到了这门语言的实用性与重要性更加激发了我对计算机学习的兴趣,提升了我对学习的自信心,为以后学习的路上增加了一道曙光功能描述
3.游戏基本功能描述如下:1)游戏场地是一片矩形透明的区域2)蛇由蛇头和蛇身组成3)当进入游戏时,任务栏出现如图所示的提示:’提示N贪食蛇小游戏马上开始,祝你好运贪食蛇4)玩家可以点击鼠标来控制游戏的开始,暂停,继续,退出也可以使用快捷键“Q”控制游戏的开始,“W”控制游戏的暂停,“E”控制游戏的继续,“R”控制游戏的退出当游戏开始之后,草坪中随机出现一颗豆和一条蛇和障碍物,5并且蛇不停地移动,蛇移动的方向与蛇头的方向一致,同时显•••示现在的时间,和游戏运行时间,当游戏结束时游戏运行时间结束,当游戏重新开局时时,分,秒分别清零6)当游戏暂停之后,蛇停止移动7)当蛇移动时,玩家可以使用可”、“广、“一”和“一”四个方向键改变蛇的移动方向8)当蛇头与豆的位置重合时,豆被蛇吃掉,同时在草坪中再生成一颗新的豆,蛇身增加一节,同时游戏中分数增加,当分数达到5的倍数时,游戏等级提升,游戏速度也会相应的加快9)当蛇头碰到蛇身时,则咬断蛇身,后半部分的蛇身消失,同时分数减为0,但是游戏等级不会发生改变,速度也不会因此发生改变10)当蛇头碰到草坪四周,或者是红色的石头时,蛇立即毙命,游戏结束,提示玩家是否再来一局,如图所示:提示E…”…1,\游戏结束!!!;\需要再来一局吗?是
①••…[否®]需求分析
4.根据功能描述可知,次小游戏的系统描述图如下「蛇接口玩家-----------------豆►草坪定义数据如下1)草坪(Lawn)草坪是贪吃蛇游戏的场地蛇,豆和石头只能存在于草坪范围之内草坪具有大小和颜色等属性2)蛇(Snake)在贪吃蛇游戏中,蛇由若干节组成,其中第一节是蛇头,其余是蛇身在游戏过程中,仅有一条蛇,并且蛇在不停地移动如果蛇吃了豆,则蛇生长一节如果蛇头碰到蛇身,则咬断蛇身,后半部分的蛇身消失分数也随即清零,游戏运行时间停止如果蛇头离开草坪,或者与石头相碰撞,则蛇死亡,游戏结束蛇具有长度、颜色、运动方向、每一节的位置等属性3)豆(Bean)在贪吃蛇游戏中,豆是蛇的食物在游戏过程中,有且仅有一颗豆如果蛇吃了豆,则重新生成一颗豆豆具有位置、大小和颜色等属性4石头Stone石头是妨碍蛇正常通过的障碍物,如果蛇••••••••••••••••••••••••••••••接触到石头,则游戏结束石头的出现具有随机性石头•••••••••••••••••••••••••具有长度,颜色,随机位置等属性设计说明
5.根据需求分析可知,Snake的每一节都有位置和大小等属性而Bean也具有这两个属性抽象出二者的共同特征,抽象出一般类Block,用于描述一个块Block派生出Bean和SnakeBlock两个类,其中SnakeBlock类用于描述蛇的一节
6.为了使游戏的运行更易于控制,定义Game类用于启动、暂停和继续游戏••••
7.根据需求分析可知,Lawn仅包含大小和颜色两个属性为了减少类的数量,可将其大小和颜色等属性添加到Game类中
8.综上所述,在贪吃蛇游戏中,有Block(块)、Bean(豆)、SankeBlock(节)、Snake(蛇)、Game(游戏)和MainForm(用户接口)六个类
9.贪吃蛇游戏的逻辑模型如下图所示Block是用来构成Bean豆和Snake蛇的最基本的单位,是Bean和SnakeBlock的基类Block类的参考代码如下using System;using System.Drawing;namespace HungrySnakeclass Block{protected Pointorigion;〃Block的左上顶点public constint WIDTH=10;〃Block的宽度public constint HEIGHT=10;//Block的高度protected Colorcolor;〃Block的颜色public Blockorigion=new Point0,0;color=new Color;public Blockintx,int y,Color_color origion=new Pointx,y;color=_color;}public PointOrigion getreturn origion;}public voidDisplay Graphicsg SolidBrush brush=new SolidBrushcolor;g.FillRectanglebrush,origion.X,origion.Y,WIDTH,HEIGHT;Pen pen=new PenColor.Black;g.DrawRectanglepen,new Rectangleorigion.X,origion.Y,WIDTH-1,HEIGHT-1;public voidClearGraphics g,Color backGroundColor|SolidBrush brush=new SolidBrushbackGroundColor;g.FillRectanglebrush,origion.X,origion.Y,WIDTH,HEIGHT;(豆)类编码
2.BeanBean表示豆,是由Block派生而来的Bean类的参考代码如下using System;using System.Drawing;namespace贪食蛇class Bean:Blockpublic staticint score=0;public staticint grade=0;public BeanColor_color origion=new Point0,0;color=color;public voidGreatGraphics g,Color backGroundColor,intlawnWidth,intlawnlleight,Snakesnake,Point stonepoint,Point stonepoint2Clearg,backGroundColor;bool bGetAPosition=false;〃是否找到生成豆的位置Random random二new Random;while!bGetAPositionorigion.X=random.Next0,lawnWidth-1/WIDTH*WIDTH;origion.Y=random.Next0,lawnHeight-1/HEIGHT*HEIGHT;if origion.X=stonepoint.Xorigion.X=stonepoint.X+149origion.Y=stonepoint.Yorigion.Y=stonepoint.Y+9origion.X=stonepoint
2.Xorigion.X=stonepoint
2.X+149origion.Y=stonepoint
2.Yorigion.Y=stonepoint
2.Y+9{origion.X-=50;origion.Y-=50;}inti;for i=0;isnake.Length;i++if origion==snake,blocks[i].Origion break;ifi二二snake.Length bGetAPosition=true;score++;if score%5=Ofescore!=0grade++;}Display g;节类编码
3.SnakeBlockSnakeBlock表示蛇的一节,是由Block派生而来的SnakeBlock类的参考代码如下using System;using System.Drawing;namespace贪食蛇class SnakeBlock:Blockprivate boolisHead;public boolIsHead getreturn isHead;}public SnakeBlockintx,int y,Color_color,bool_isHead origion=new Pointx,y;color=_color;isHead二_isHead;public voidChangeHeadToBody if isHead isHead二false;public voidDisplay Graphicsg,Directiondirectionbase.Display g;ifisHead〃绘制蛇眼SolidBrushbrush=new SolidBrushColor.Brown;switch directioncase Direction.Up:case Direction.Down:g.FillRectanglebrush,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;case Direction.Left:caseDirection.Right:g.FillRectanglebrush,origion.X+WIDTH/2,origion.Y+HEIGHT/4,2,2;。
个人认证
优秀文档
获得点赞 0