




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、面向对象编程技术实验指导教程郑州轻工业学院计算机与通信工程学院网络工程系目录1 实验目的32 实验题冃33 实验步骤43.1 需求43.2 设计53.3 编码63.4 测试174 实验报告185 实验验收18附录1实验报告格式191实验目的通过开发款贪吃蛇游戏程序,熟练掌握c#编程语言、和面向对象程序设计方法,独立完成一个游戏程序的开发。2实验题目使用c#编程语言,开发一款贪吃蛇游戏,如下图所示。游戏基本功能描述如下:1)游戏场地是一片矩形区域的草坪。2)一条蛇由蛇头和蛇身组成。3)当游戏开始之后,草坪中出现一颗豆和一条蛇,并且蛇不停地 移动,蛇移动的方向与蛇头的方向一致。4)当蛇移动时,玩家
2、使用下、“厂、“1,和j咽个键控制蛇的方向。5)当蛇头与豆的位置重合时,豆被蛇吃掉,同时在草坪中再生成 颗新的豆,蛇身增加一节。6)当蛇头碰到草坪四周或蛇身时,蛇立即毙命,游戏结束。3实验步骤3.1需求根据功能描述可知,贪吃蛇游戏的系统结构图如下所示。定义数据字典如下:1)草坪(lawn):草坪是贪吃蛇游戏的场地。豆和蛇只能存 在于草坪范围之内。草坪具有大小和颜色等属性。2)蛇(snake):在贪吃蛇游戏中,蛇由若干节组成,其中第 一节是蛇头,其余是蛇身。在游戏过程中,有且仅有一条 蛇,并且蛇在不停地移动。如果蛇吃了豆,则蛇生长一节。 如果蛇头碰到蛇身或离开草坪,则蛇死亡游戏结束。蛇具 有长度
3、、颜色、运动方向、每一节的位置等属性。3)豆(bean):在贪吃蛇游戏屮,豆是蛇的食物。在游戏过 程中,有且仅有一颗豆。如果蛇吃了豆,则重新生成一颗 豆。豆具有位置、大小和颜色等属性。3.2设计根据需求分析可知,snake的每一节都有位置和大小等属性。而bean也具有这两个属性。抽象出二者的共同特征,抽象出一般类 block,用于描述一个块。block派生111 bean和snakcblock两个类,其 中snakeblock类用于描述蛇的一节。为了使游戏的运行更易于控制,定义game类用于启动、暂停和 继续游戏。根据需求分析可知,lawn仅包含大小和颜色两个属性。为了减 少类的数量,可将其大
4、小和颜色等属性添加到game类中。综上所述,在贪吃蛇游戏中,有block(块)、bean(豆)、sankeblock (节)、snake (蛇)、game (游戏)和mainform (用户接口)六个 类。贪吃蛇游戏的逻辑模型如下图所示。3.3编码3.3.1 block (块)类编码block是用来构成bean (豆)和snake (蛇)的最基本的单位,是bean和snakeblock的基类。block类的参考代码如下。using system;using system. drawing;namespace hungrysnakeclass blockprotected point origi
5、on; /block的左上顶点public const int width = 10; /block的宽度pub 1 ic const int height = 10; /block的高度protected color color; /block的颜色pub lie bl ock ()origion = new point (0, 0);color = new color();public block(int x, int y, co 1 or _color)ori gion = new point (x, y);color = _color;public point origiongetre
6、turn origion;public void display(graphics g)solidbrush brush 二 now solidbrush(color);g. fi 11 rectangle (brush, origion. x, origion. y, w1dth, height);pen pen = new pen(color. black);g. drawrectangle(pen, new rectangle(origion. x, origion. y, width - 1, height - 1);pub 1ic void clear(graphics g, co
7、1 or backgroundcolor)solidbrush brush = new solidbrush(backgroundcolor);g. fillrectangle (brush, origion. x, origion. y, width, height);3.3.2 bean (豆)类编码bean表示豆,是由block派生而来的。bean类的参考代码如下。using system;using system. drawing;namespace hungrysnakeclass bean : blockpublic bean(color _color)origion = new
8、point (0, 0);color = color;public void creat(graphics g, color backgroundcolor, int lawnwidth, int1awnheight, snake snake)cl ear (g, backgroundcolor);bool bgetaposition = false; /是否找到生成豆的位置random random = new random ();while (!bgetaposition)origion. x = random. next(0, lawnwidth - 1) / width * width
9、; origion. y = random. next (0, lawnlleight - 1) / height * height; int i;for (i = 0; i < snake. length; i+)if (origion = snake. blocksi> origion)break;if (i = snake. length)bgetaposition 二 true;display(g);3.3.3 snakeblock (节)类编码snakeblock表示蛇的一节,是由block派生而来的。snakeblock 类的参考代码如下:using system;us
10、ing system. drawing;namespace hungrysnakcclass snakeblock : blockprivate bool ishead;public bool isheadgetreturn ishcad;public snakeblock(int x, int y, color _color, bool _isllead) origion = new p()int(x, y);color = _color;ishead = _ishead;public void changeheadtobody0if (ishead)isllead = false;pub
11、1i c void di splay(graphics g, direction directi on)base. di splay (g);if (ishead)绘制蛇眼sol idbrush brush = new sol i dbrush ('olor. brown);switch (direction)case direction. up:case di reelion .down:g. fillrectangle(brush, origion. x + width / 4, origion. y +hetght / 2, 2, 2);g. fillrectangle(brus
12、h, origion. x + width / 4 * 3, origion. y + height / 2, 2, 2);break;case direction. left:case di recti on. right:g. fillrectangle(brush, origion. x + width / 2, origion. y + height / 4, 2, 2);g. fi 11 rectangle (brush, origion. x + width / 2, origion. y + height / 4 * 3, 2, 2);break;3.3.4 snake (蛇)类
13、编码snake表示蛇。snake类的参考代码如下:using system;using system. collections. generic;using system. drawing;namespace hungrysnakepublic enum directionup,dow n,left,right;class snakeprivate int length;public direction direction;private color color;publ ic li st<snakeblock> blocks;private const int tnit leng
14、th = 3;public int lengthgetreturn length;public snake(color color, direction direction)direction = direction;color = _color;blocks = new list<snakcblock>();public void creat (graphics g, color backgroundcolor, int lawnwidth, int lawnhei ght)clear(g, backgroundcolor);blocks. clear ();length = i
15、ntt_length;int x;int y;random random = new random ();x = random. next(lawnwidth / 4, lawnwidth / 4 * 3) / block. width * block. width;y = random. next(lawnheight / 4 - 1, lawnheight / 4 * 3) / block. height* blockjielglit;blocks. add(new snakeblock(x, y, color, true);switch (direction)case direction
16、. up:for (int i = 1; i < length; i+)blocks. add(new snakeblock(x, y + block. height * i, color, false);break;case direct ion .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+)bl
17、ocks. add(new snakeblock(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.
18、 origion. x;int y 二 2 * blocksblocks.count - 1. origion. y - blocksblocks.count- 2. origion. y;blocks. insert(length, new snakoblock(x, y, color, false);length+;public void move()int x = 0;int y = 0;blocks0. changeheadtobody 0;switch (direction)case direction. up:x = blocks0.origion. x;y = blocks0.
19、origion. 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.
20、y;break;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 di splay(g, directi on);public void clear (graphics g, color backgroundco1or)for (int i = 0; i < length; i+)blocksi. clear(
21、g, backgroundcolor);public bool caneatbean(bcan bean)if (blocks0. origion = bean. origion) return true;elsereturn false;public void eatbean(bean bean, graphics g, color backgroundcolor, int 1awnwidth, int 1awnheight)bean. clear (g, backgroundco1or);grow();bean. creat(g, backgroundcolor, lawnwidth, l
22、awnheight, this);publicbool isalivc(int 1awnwidth, int lawnheight)(blocks0. origion. x return false;(blocks0, origion.y return false;(blocks0. origion. x return false;(blocks0. origion. y return false;for (int i = 3; i < blocks.count; i+)ifififif0)0)block. wtdth > lawnwidth)block. height >
23、lawnheight)if (blocks0j. origion = blocksi. origion) return false;return true;3.3.5 game (游戏)类设计game控制游戏的运彳亍,负责在游戏开始时生成bean和snake,以 及负责在游戏运行中snake的移动、snake的生长、bean的重生,并随 时检测snake的生死状态。game类的参考代码如下:using system;using system collections. generic;using system. drawing;namespace hungrysnakcclass game pu
24、blic snake snake;public bean bean;public bool issnakealive;public 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. heig
25、ht * block. height; direction direction 二(direction)randomnext(1, 4);snake = new snake (co lor. yellowgreen, di rection);bean = new bean(color. pink);issnakealive = false;lawnwidth = _lawnwidth;lawnhei ght = _lawnheight;public void begin (graphics g, color backgroundcolor, int lawnwidth, int lawnhei
26、ght)issnakealive = true;snake. creat(g, backgroundcolor, lawnwidth, lawnheight);bean. creat(g, backgroundcolor, lawnwidth, lawnheight, snake);public void ontime(graphics g, color backgroundcolor, int lawnwidth, int lawnheight)if (issnakealive)snake c1 ear (g, backgroundco1 or);snake move ();snake. d
27、i splay(g);bean. display(g);if (snake. caneatbean(bcan)bean.clear(g, backgroundcolor);snake. eatbean(bean, g, backgroundcolor, lawnwidth, lawnheight);bean display(g);if (!snake.isalive(lswnwidth, lawnheight)issnakealive = false;3.3.6 mainform (主界面)类设计mainform可通过继承 system.windows.forms.form实现,通过向 mai
28、nform中添加控件及设置控件的属性系统会自动向mainform类中 添加相应的对象字段,如木程序的menustrip 1和timer 1等。33.6.1添加属性添加属性game,来控制游戏的开始和运行,参考代码如下: private game game;3.362修改构造函数在构造函数的最后对属性game进行实例化,参考代码如下:game = new gamc(lawn.width, lawn.height);3363panel容器panel容器作为游戏运彳亍的场地。snake和bean都在panel容器中绘 制。panel容器的添加步骤如下: 在mainform中添加一个panel容器 口
29、 p81161。拖动panel容器的大小才,使其充满整个窗体。 修改panel容器的属性n ame=lawn。 修改 panel 容器的属性 backcolor=green。3364timer组件timer组件用于在用户定义的时间间隔引发事件。timer组件的添 加步骤如下:在mainform 中添加一个timer 组件 timer。 修改timer组件的属性enalbed=false。 修改timer组件的属性interval 100双击® timer ,生成tick事件。tick事件的参考代码如下:if (game, issnakea1ive)graphics g;g = law
30、n. creategraphics();game. on time (g, 1 awn. backcolor, lawn. width, lawn. height); if (!game. issnakealive)messagebox. show ("game over!,z);3.3.6.5menustrip 菜单menustrip菜单用于提供用户可以使用的功能。menustrip菜单的添加步骤如下: 在mainform 中添加一个menustrip菜单 ® menustrip。添加菜单项如下图所示。游戏1开局1 修改菜单项"开局”的属性name=toolstripmenuitemstart双击菜单项“开局”,生成click事件。click事件的参考代码如下:graphics g;g = lawn. creategraphics();game begin(g, lawn. backcolor, lawn. width, lawn. height); timerl. enablcd = true;3366keydown事件在贪吃蛇游戏中,使用“忙、“厂、“和四个方向 键改变蛇的移动方向。keydown事件用于在用户按键吋,提供改变
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年图书馆信息化:智能检索系统技术创新与信息资源整合
- 2025年特色小镇文化旅游产业技术创新与旅游产品创新可行性研究
- 镰状细胞贫血课件
- 激光雷达固态化技术2025年在物流行业的应用与成本分析
- 2025年4D打印的动态材料应用
- 2025年3D打印技术在医疗器械制造中的前景
- 2025年3D打印技术在制造业的变革作用
- 镀锌材料知识培训内容课件
- 锻造模具加工知识培训课件
- 2025年3D打印技术在定制化医疗器械中的创新
- 钢板粘贴施工检验批质量验收记录
- 全屋定制厂采购管理制度
- 2025年三级筑路工职业技能鉴定考试题库(含答案)
- CJ/T 447-2014管道燃气自闭阀
- 四肢骨折护理要点及规范
- 消防经济学试题及答案
- GenAI时代趋势中的TiDB
- 《麦克风培训资料》课件
- 口腔综合治疗台水路清洗消毒技术规范
- 心理课堂-情绪ABC理论教案
- 村消防安全管理工作制度
评论
0/150
提交评论