《贪食蛇(实训报告).docx》由会员分享,可在线阅读,更多相关《贪食蛇(实训报告).docx(21页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、郑州轻工业学院实训报告实训名称: 指导教师:姓名: 学号: 班级:提交日期:g. FillRectangle(brush, origion.X + WIDTH / 2, origion.Y +HEIGHT / 4 * 3, 2, 2);break;)4. Snake (蛇)类编码using System;using System. Collections. Generic;using System. Linq;using System. Text;using System. Drawing;namespace贪食蛇public enum Direction(Up,Down,Left,Right
2、);class Snakeprivate int length;public Direction direction;private Color color;public List blocks;private const int INITLENGTH = 3;public int Length(get (return length;)public Snake(Color _color, Direction _direction) (direction = direction;color = _color;blocks = new List ();public void Great(Graph
3、ics g, Color backGroundColor, int lawnWidth, int lawnHeight)(Clear (g, backGroundColor);blocks. Clear ();length = INIT_LENGTH;int x=50;/防止石头与蛇相遇,固定蛇的初始位置int y=440;防止石头与蛇相遇,固定蛇的初始位置Random random = new Random();blocks. Add(new SnakeBlock(x, y, color, true); switch (direction) (case Direction. Up:for (
4、int i = 1; i length; i+) (blocks. Add (new SnakeBlock(x, y + Block. HEIGHT * i, color, false);)break;case Direction. Down:for (int i = 1; i length; i+) blocks. Add (new SnakeBlock (x, y - Block. HEIGHT * i, color, false);) break;case Direction. Left:for (int i = 1; i length; i+) blocks. Add(new Snak
5、eBlock (x + Block. WIDTH * i, y, color, false);) break;case Direction. Right: for (int i = 1; i length; i+) blocks. Add(new SnakeBlock (x - Block. WIDTH * i, y, color, false);) break;Display (g);public void Grow()int x = 2 * blocksblocks. Count - 1, Origion. X 一 blocksblocks. Count -2. Origion. X;in
6、t y = 2 * blocksblocks. Count - 1. Origion. Y - blocksblocks. Count -2. Origion. Y;blocks. Insert(length, new SnakeBlock(x, y, color, false);length+;)public void Move() (int x = 0;int y = 0;blocks0. ChangeHeadToBody ();switch (direction)(case Direction. Up:x 二 blocks0. Origion. X;y = blocksEO. Origi
7、on. Y - Block. HEIGHT; break;case Direction. Down:x = blocks0. Origion. X;y = blocks0. Origion.Y + Block. HEIGHT; break;case Direction. Left:x = blocks0.Origion. X - Block. WIDTH;y = blocks0. Origion. Y;break;case Direction. Right:x = blocks0. Origion. X + Block. WIDTH;y = blocks0. Origion. Y;break;
8、blocks. Insert(0, new SnakeBlock(x, y, color, true);blocks. RemoveAt (blocks.Count - 1);)public void Display(Graphics g)for (int i = 0; i length; i+)(blocksi. Display(g, direction);public void Clear(Graphics g, Color backGroundColor) for (int i = 0; i blockNum - 1; i一) (blocksi. Clear (g, backGround
9、Color);blocks. RemoveAt (i);length = blockNum;public bool CanEatBean(Bean bean)(if (blocks0. Origion 二二 bean. Origion) return true ;elsereturn false ;public bool SnakeMeetStone(Point stonepoint )if (blocks0. Origion. X =stonepoint. X & blocks0. Origion. X = stonepoint. Y & blocks 0 Origion. Y = ston
10、epoint2. X & blocks0. Origion. X = stonepoint2. Y & blocks0. Origion. Y = stonepoint2. Y + 9) return true;elsereturn false;public int CanEatSnake()for (int i = 3; i lawnWidth)Block.HEIGHT lawnHeight)elsereturn true;5.Game (游戏)类设计Game控制游戏的运行,负责在游戏开始时生成Bean和Snake,以及负责在游戏运行中Snake的移动、Snake的生长、Bean的重生,以及
11、 分数的增加,并随时检测Snake的生死状态。Game类的参考代码如下: using System;using System. Collections.Generic;using System. Linq;using System. Text;using System. Drawing;namespace贪食蛇class Game:Blockpublic static Point stonepoint;public static Point stonepoint2;public Snake snake;public Bean bean;public bool isSnakeAlive;publ
12、ic int lawnWidth;public int lawnHeight;public Game(int lawnWidth, int lawnHeight) (Random random = new Random ();int x 二 random. Next(0, _lawnWidth - 1) / Block. WIDTH * Block. WIDTH;int y = random. Next (0, _lawnHeight - 1) / Block. HEIGHT * Block. HEIGHT;Direction direction = (Direction)random. Ne
13、xt(1, 4);snake = new Snake(Color. YellowGreen, direction);bean 二 new Bean(Color.Pink);isSnakeAlive = false;lawnWidth = _1awnWidth;lawnHeight = lawnHeight;public void Begin (Graphics g, Color backGroundColor, int lawnWidth, int lawnHeight)(g. Clear(Color. Turquoise);int stoneway X, stoneway Y;int sto
14、neway_X2, stoneway_Y2;Random random = new Random ();int stonewayl_X = random. Next (2, 29);让障碍物以 10个单位生成int stoneway 1 Y = random. Next (2, 39);让障碍物以 10个单位生成int stoneway2_X = random. Next (10, 30);让障碍物以 10个单位生成int stoneway2_Y 二 random. Next (39, 47);让障碍物以 10个单位生成int array x = stonewaylX, stoneway2 X
15、);int array_y = stonewayl_Y , stoneway2_Y );int i = random. Next(0, 2);int j = random. Next(0, 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. Clear(g, backGroundColor);snake. Great(g, backGroundColor, lawnWidth, law
16、nHeight);bean. Great (g, backGroundColor, lawnWidth, lawnHeight, snake, stonepoint , stonepoint2 );g. FillRectangle(new SolidBrush(Color . Red ), stoneway X , stoneway Y , 150, 10);障碍物的显示g. FillRectangle(new SolidBrush(Color . Red ), stoneway_X2, stoneway_Y2, 150, 10 );/障碍物的显示stonepoint. X 二 stonewa
17、y_X;stonepoint . Y = stoneway Y;stonepoint2. X 二 stoneway_X2;stonepoint2 . Y = stoneway_Y2;Bean, score = 0;Bean, grade = 0;public void OnTime(Graphics g, Color backGroundColor, int lawnWidth, int lawnHeight)(if (isSnakeAlive)(snake. Clear(g, backGroundColor);snake. Move ();snake. Display (g);bean. D
18、isplay (g);if (snake. CanEatBean(bean) (bean. Clear(g, backGroundColor);snake. EatBean(bean, g, backGroundColor, lawnWidth, lawnHeight);bean. Display(g); int blockNum = snake. CanEatSnake(); if (blockNum 0)snake. RemoveAfter(g, backGroundColor, blockNum);Bean, score = 0;)if (!snake. IsAlive(lawnWidt
19、h,lawnlleight)| (snake . SnakeMeetStone (stonepoint)| (snake . SnakeMeetStone2 (stonepoint2)isSnakeAlive = false;)6.MainForm (主界面)类设计MainForm可通过继承 实现,通过向MainForm中添加控件及设置控件的属性系统会自动向MainForm类中添加相应的对象字段,如本程序的menuStrip 1和timerl等。1)添加属性:添加属性game,来控制游戏的开始和运行,参考代码如下: private Game game;2)修改构造函数:在构造函数的最后对属性g
20、ame进行实例化,参考代码如下:game = new Game(lawn.Width, lawn.Height);3) Panal容器:Panel容器作为游戏运行的场地。蛇(Snake)豆(Bean)和石头(Stone)都在Panel容器中绘制。Panel容器的添加步骤如下: 在MainForm中添加一个Panel容器 口 Panelo 拖动Panel容器的大小,使其充满整个窗体。 修改Panel容器的属性Name=lawn。 修改 Panel 容器的属性BackColor= Turquoise o4) Timer组件:Timer组件用于在用户定义的时间间隔引发事件。Timer组件的添 加步骤
21、如下: 在MainForm 中添加一个Timer 组件 Timero 修改Timer组件的属性Enalbed=false。 双击 Timer ,生成Tick事件。Tick事件的参考代码如下:label4. Text = DateTime. Today. ToShortDateString ();label5. Text 二 DateTime . Now. ToLongTimeString ();if (game. isSnakeAlive) (Graphics g;g = lawn. CreateGraphics();game. OnTime(g, lawn . BackColor , law
22、n . Width , lawn .Height );if (!game. isSnakeAlive) (if (MessageBox. Show(游戏结束! !n需要再来一局吗? ,提示,MessageBoxButtons. YesNo, MessageBoxIcon. Warning)二二 DialogResult.Yes) (game. Begin(g, lawn. BackColor, lawn. Width, lawn. Height);timerl. Enabled = true;label8. Enabled = true;label9. Enabled = false;) )
23、label2. Text = Bean, score. ToStringO ; labelll. Text = Bean, grade. ToStringO ; timerl. Interval 二(150 - Bean, grade * 20); labell3. Text = timerl. Interval. ToStringO ; 在MainForm 中再添加一个Timer组件 Timero双击mer生成ck事件。Tick事件的参考代码如下:private void timer2_Tick (object sender, EventArgs e)if (game. isSnakeAli
24、ve) timer2. Interval = 1000;labell5. Text = string. Format (zz0 : 1 : 2,z, hour. ToStringO, min. ToStringO, sec. ToStringO);sec+;if (sec = 60) (min+;sec = 0;)if (min = 60) (hour+;min = 0;)if (!game. isSnakeAlive)timer2. Enabled 二 true;)5) Label控件:Label控件可用于提供用户可以使用的功能。Label控件的添加步 骤如下: 在MainForm 中添加一
25、个label A Label。 分别修改label控件的属性。 分别设置label控件的Text属性,使其分别显示为:开始游戏 (Q),退出游戏(区),暂停(理),继续(旦),游戏得分等一 系列控件属性。 双击“开始游戏(Q) ”,生成Click事件。Click事件的参考代码如下:Graphics g;g = lawn. CreateGraphics();game. Begin(g, lawn. BackColor, lawn. Width, lawn. Height);timerl. Enabled = true;1 .实训I目的通过开发一款贪吃蛇小游戏程序,使自己熟练掌握C#编程语言、 和
26、面向对象程序设计方法。使自己在掌握VC#面向对象编程理论的基 础上,推动自己初步掌握基于VC#的WinForm程序的编程方法,为今 后的网络协议编程和Web及Web服务编程打下基础。2 .实训题目使用C#语言在VS平台上编程实现贪食蛇小游戏的运行,如下图所示:贪食蛇13回区)开始游戏)退出游戏哲 停000000008游戏运行时间0:0:43游戏得分7游戏等级1游戏速度130现在时间:2011-9-810:38:03timer2. Enabled = true;label8 . Enabled = true;label9. Enabled = false; 双击暂停“(亚)”,生成Click事件
27、。Click事件的参考代码如下:timerl. Enabled = false;label8. Enabled = false;label9. Enabled = true; 双击菜单项“继续(E) ”,生成Click事件。Click事件的参考代码如下:timerl. Enabled = true;label8. Enabled = true;label9. Enabled = false; 双击菜单项“退出游戏(R) ”,生成Click事件。Click事件的参考代码如下:if (MessageBox. Show(确实要退出游戏吗?”、提示,MessageBoxButtons. YesNo,
28、MessageBoxIcon. Information)二二 DialogResult. Yes)this. Close ();6) KeyDown 事件:在贪吃蛇游戏中,使用“,、1”、“一”和“一”四个方向 键改变蛇的移动方向,使用“Q”控制游戏的开始,“E”控制游戏 的继续,“W”控制游戏的暂停,“R”控制游戏退出。KeyDown事 件用于在用户按方向键时,提供改变蛇移动方向;按此几个快捷键时, 控制游戏的各种功能。KeyDown事件的添加步骤如下: 双击事件KeyDown,生成MainForm的KeyDown事件。 KeyDown事件的参考代码如下:string S =;S =e. K
29、eyCode . ToString ();switch (S)八 case Q :Graphics g;g = lawn. CreateGraphics();game. Begin(g, lawn. BackColor, lawn. Width, lawn. Height);timerl. Enabled = true;label8. Enabled = true;label9. Enabled = false; break;case W :timerl. Enabled = false; label8. Enabled = false; label9. Enabled = true; bre
30、ak; case E :timerl. Enabledtrue;label8. Enabledtrue;label9. Enabledfalse;break ;case R:if (MessageBox. Show(你要退出游戏吗? ,提示,MessageBoxButtons. YesNo, MessageBoxIcon. Information) = DialogResult. Yes) (this. Close ();)break;)if (game, snake, direction = Direction. Down | | game, snake, direction = Direc
31、tion. Up)if (S=Left)game, snake, direction = Direction. Left;if (S二二Right)game, snake, direction = Direction.Right; else (if (S=Down)game, snake, direction 二 Direction. Down; if(S=Up )game, snake, direction=Direction. Up;7.测试报告1) Bean的生成与显示1)测试程序是否能在游戏开始时,生成一颗豆。2)测试程序是否能在游戏运行中,正常显示一颗豆。3)测试程序是否能在豆被蛇吃
32、掉时,消除原先的豆,并重 新生成一颗豆。2) Snake的生成、显示、移动与生长1)测试程序是否能在游戏开始时,生成一条蛇。2)测试程序是否能在游戏运行中,正常显示一条蛇的运动。3)测试程序是否能在游戏运行中,根据用户的按键情况,改变 蛇的移动方向,并控制游戏的各种功能。4)测试程序是否能在蛇吃掉豆时,生长一节。5)测试程序是否能在蛇头碰到蛇身时,咬断蛇身,分数为零, 但是游戏的等级不会发生改变。6)测试程序是否能在蛇头碰到场地边界或者碰到石块时死亡, 结束游戏,游戏运行时间停止,并且提示再来一局的提示, 如果玩家确定则游戏再次运行。8 .游戏升级此贪食蛇小游戏的功能升级描述如下:1)增加了显
33、示当前时间,和游戏速度的功能,当游戏运行时显示 当前的北京时间,和当前游戏的运行速度,游戏的运行时间。2)增加计分功能。当游戏开始时,分数清零。蛇每吃一颗豆,分数加1。当蛇咬断蛇身时,分数再次清零,但是游戏的等级不 会发生改变,相对应的速度也不会发生改变。3)当游戏开始时,草坪中生成若干石块。如果蛇头碰到石块,则蛇立即毙命,游戏结束,游戏运行时间停止,并且提示玩家是 否再来一局,如果玩家确定则游戏再次运行。4)增加了游戏等级设定,以蛇每吃到5个豆时,等级升1,同时 游戏速度会发生改变。10 .心得体会通过这次实训,初步了解和掌握winform程序,了解WinForm程序 的作用与意义,更加全面
34、的了解C#这门面向对象的语言的特点与优势。 让我明白了实践的重要性,理论和实践相结合的必要性,也让我感受 到了这门语言的实用性与重要性。更加激发了我对计算机学习的兴趣, 提升了我对学习的自信心,为以后学习的路上增加了一道曙光。3.功能描述游戏基本功能描述如下:1)游戏场地是一片矩形透明的区域。2)蛇由蛇头和蛇身组成。3)当进入游戏时,任务栏出现如图所示的提示:&提示N贪食蛇小游戏马上开始,祝你好运贪食蛇4)玩家可以点击鼠标来控制游戏的开始,暂停,继续,退出。也 可以使用快捷键:“Q”控制游戏的开始,“W”控制游戏的暂 停,“E”控制游戏的继续,“R”控制游戏的退出。5)当游戏开始之后,草坪中随
35、机出现一颗豆和一条蛇和障碍物,并且蛇不停地移动,蛇移动的方向与蛇头的方向一致,同时显 示现在的时间,和游戏运行时间,当游戏结束时游戏运行时间 结束,当游戏重新开局时:时,分,秒分别清零。6)当游戏暂停之后,蛇停止移动。7)当蛇移动时,玩家可以使用可”、“广、“一”和“一”四个方向键改变蛇 的移动方向。8)当蛇头与豆的位置重合时,豆被蛇吃掉,同时在草坪中再生成 一颗新的豆,蛇身增加一节,同时游戏中分数增加,当分数达 到5的倍数时,游戏等级提升,游戏速度也会相应的加快。9)当蛇头碰到蛇身时,则咬断蛇身,后半部分的蛇身消失,同时 分数减为0,但是游戏等级不会发生改变,速度也不会因此发生改变。10)当
36、蛇头碰到草坪四周,或者是红色的石头时,蛇立即毙命,游戏结束,提示玩家是否再来一局,如图所示:提示, 游戏结束! ! ;需要再来一局吗? E”是1 否4.需求分析根据功能描述可知,次小游戏的系统描述图如下: 蛇 接口玩家 豆 草坪定义数据如下:1)草坪(Lawn):草坪是贪吃蛇游戏的场地。蛇,豆和石头 只能存在于草坪范围之内。草坪具有大小和颜色等属性。2)蛇(Snake):在贪吃蛇游戏中,蛇由若干节组成,其中第 一节是蛇头,其余是蛇身。在游戏过程中,仅有一条蛇, 并且蛇在不停地移动。如果蛇吃了豆,则蛇生长一节。如 果蛇头碰到蛇身,则咬断蛇身,后半部分的蛇身消失。分 数也随即清零,游戏运行时间停止
37、。如果蛇头离开草坪, 或者与石头相碰撞,则蛇死亡,游戏结束。蛇具有长度、 颜色、运动方向、每一节的位置等属性。3)豆(Bean):在贪吃蛇游戏中,豆是蛇的食物。在游戏过程中,有且仅有一颗豆。如果蛇吃了豆,则重新生成一颗 豆。豆具有位置、大小和颜色等属性。4)石头(Stone):石头是妨碍蛇正常通过的障碍物,如果蛇 接触到石头,则游戏结束。石头的出现具有随机性。石头 具有长度,颜色,随机位置等属性。5 .设计说明根据需求分析可知,Snake的每一节都有位置和大小等属性。而Bean也具有这两个属性。抽象出二者的共同特征,抽象出一般类Block, 用于描述一个块。Block派生出Bean和Snake
38、Block两个类,其中 SnakeBlock类用于描述蛇的一节。6 .为了使游戏的运行更易于控制,定义Game类用于启动、暂停和继 续游戏。 7 .根据需求分析可知,Lawn仅包含大小和颜色两个属性。为了减少 类的数量,可将其大小和颜色等属性添加到Game类中。8 .综上所述,在贪吃蛇游戏中,有Block(块)、Bean(豆)、SankeBlock (节)、Snake (蛇)、Game (游戏)和MainForm (用户接口)六个类。9 .贪吃蛇游戏的逻辑模型如下图所示。Block是用来构成Bean (豆)和Snake (蛇)的最基本的单位,是Bean和SnakeBlock的基类。Block类
39、的参考代码如下。using System;using System. Drawing;namespace HungrySnake (class Blockprotected Point origion; Block的左上顶点public const int WIDTH = 10; Block的宽度public const int HEIGHT = 10; /Block的高度protected Color color; Block的颜色 public Block() (origion = new Point (0, 0);color = new Color ();public Block(int
40、 x, int y, Color _color) origion = new Point(x, y); color = _color;public Point Origion (get (return origion;)public void Display (Graphics g) (SolidBrush brush = new SolidBrush (color);g. FillRectangle(brush, origion. X, origion. Y, WIDTH, HEIGHT);Pen pen = new Pen (Color. Black);g. DrawRectangle(p
41、en, new Rectangle(origion. X, origion.Y, WIDTH - 1, HEIGHT - 1);)public void Clear(Graphics g, Color backGroundColor) |SolidBrush brush = new SolidBrush(backGroundColor);g. FillRectangle(brush, origion. X, origion. Y, WIDTH, HEIGHT); )2.Bean (豆)类编码Bean表示豆,是由Block派生而来的。Bean类的参考代码如下。using System;using System. Drawing; namespace贪食蛇class Bean : Blockpublic static int score=0;public static int grade = 0; public Bean (Color _color) (origion = new Point (0, 0);color = color;public void Great(Graphics g, Color backGroundColor, int lawnWidth, intlawnlleight, Snake snake, Point stonepoint, Point stone
限制150内