




已阅读5页,还剩16页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
面向对象编程技术实验指导教程郑州轻工业学院计算机与通信工程学院网络工程系目录1实验目的32实验题目33实验步骤43.1需求43.2设计53.3编码63.4测试183.5系统升级194实验报告205实验验收206实验成绩20附录1 实验报告格式21网络编程基础实训教程1 实验目的通过开发一款贪吃蛇游戏程序,熟练掌握C#编程语言、和面向对象程序设计方法,独立完成一个游戏程序的开发。2 实验题目使用C#编程语言,开发一款贪吃蛇游戏,如下图所示。游戏基本功能描述如下:1) 游戏场地是一片矩形区域的草坪。2) 一条蛇由蛇头和蛇身组成。3) 当游戏开始之后,草坪中出现一颗豆和一条蛇,并且蛇不停地移动,蛇移动的方向与蛇头的方向一致。4) 当蛇移动时,玩家使用“”、“”、“”和“”四个键控制蛇的方向。5) 当蛇头与豆的位置重合时,豆被蛇吃掉,同时在草坪中再生成一颗新的豆,蛇身增加一节。6) 当蛇头碰到草坪四周或蛇身时,蛇立即毙命,游戏结束。3 实验步骤3.1 需求根据功能描述可知,贪吃蛇游戏的系统结构图如下所示。定义数据字典如下:1) 草坪(Lawn):草坪是贪吃蛇游戏的场地。豆和蛇只能存在于草坪范围之内。草坪具有大小和颜色等属性。2) 蛇(Snake):在贪吃蛇游戏中,蛇由若干节组成,其中第一节是蛇头,其余是蛇身。在游戏过程中,有且仅有一条蛇,并且蛇在不停地移动。如果蛇吃了豆,则蛇生长一节。如果蛇头碰到蛇身或离开草坪,则蛇死亡游戏结束。蛇具有长度、颜色、运动方向、每一节的位置等属性。3) 豆(Bean):在贪吃蛇游戏中,豆是蛇的食物。在游戏过程中,有且仅有一颗豆。如果蛇吃了豆,则重新生成一颗豆。豆具有位置、大小和颜色等属性。3.2 设计根据需求分析可知,Snake的每一节都有位置和大小等属性。而Bean也具有这两个属性。抽象出二者的共同特征,抽象出一般类Block,用于描述一个块。Block派生出Bean和SnakeBlock两个类,其中SnakeBlock类用于描述蛇的一节。为了使游戏的运行更易于控制,定义Game类用于启动、暂停和继续游戏。根据需求分析可知,Lawn仅包含大小和颜色两个属性。为了减少类的数量,可将其大小和颜色等属性添加到Game类中。综上所述,在贪吃蛇游戏中,有Block(块)、Bean(豆)、SankeBlock(节)、Snake(蛇)、Game(游戏)和MainForm(用户接口)六个类。贪吃蛇游戏的逻辑模型如下图所示。MainFormGameBlockBeanSnakeBlockSnake3.3 编码3.3.1 Block(块)类编码Block是用来构成Bean(豆)和Snake(蛇)的最基本的单位,是Bean和SnakeBlock的基类。Block类的参考代码如下。using System;using System.Drawing;namespace HungrySnake class Block protected 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 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(pen, 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); 3.3.2 Bean(豆)类编码Bean表示豆,是由Block派生而来的。Bean类的参考代码如下。using System;using System.Drawing;namespace HungrySnake class Bean : Block public Bean(Color _color) origion = new Point(0, 0); color = _color; public void Creat(Graphics g, Color backGroundColor, int lawnWidth, int lawnHeight, Snake snake) Clear(g, backGroundColor); bool bGetAPosition = false; /是否找到生成豆的位置 Random random = new Random(); while (!bGetAPosition) origion.X = random.Next(0, lawnWidth - 1) / WIDTH * WIDTH; origion.Y = random.Next(0, lawnHeight - 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;using System.Drawing;namespace HungrySnake class SnakeBlock : Block private bool isHead; public bool IsHead get return isHead; public SnakeBlock(int x, int y, Color _color, bool _isHead) origion = new Point(x, y); color = _color; isHead = _isHead; public void ChangeHeadToBody() if (isHead) isHead = false; public void Display(Graphics g, Direction direction) base.Display(g); if (isHead) /绘制蛇眼 SolidBrush brush = new SolidBrush(Color.Brown); switch (direction) case Direction.Up: case Direction.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; case Direction.Left: case Direction.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; 3.3.4 Snake(蛇)类编码Snake表示蛇。Snake类的参考代码如下:using System;using System.Collections.Generic;using System.Drawing;namespace HungrySnake public enum Direction Up, Down, Left, Right ; class Snake private int length; public Direction direction; private Color color; public List blocks; private const int INIT_LENGTH = 3; public int Length get return length; public Snake(Color _color, Direction _direction) direction = _direction; color = _color; blocks = new List(); public void Creat(Graphics g, Color backGroundColor, int lawnWidth, int lawnHeight) Clear(g, backGroundColor); blocks.Clear(); length = INIT_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 * Block.HEIGHT; blocks.Add(new SnakeBlock(x, y, color, true); switch (direction) case Direction.Up: for (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 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.Origion.X; int 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 = blocks0.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.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.Display(g, direction); public void Clear(Graphics g, Color backGroundColor) for (int i = 0; i length; i+) blocksi.Clear(g, backGroundColor); public bool CanEatBean(Bean bean) if (blocks0.Origion = bean.Origion) return true; else return false; public void EatBean(Bean bean, Graphics g, Color backGroundColor, int lawnWidth, int lawnHeight) bean.Clear(g, backGroundColor); Grow(); bean.Creat(g, backGroundColor, lawnWidth, lawnHeight, this); public bool IsAlive(int lawnWidth, int lawnHeight) if (blocks0.Origion.X 0) return false; if (blocks0.Origion.Y lawnWidth) return false; if (blocks0.Origion.Y + Block.HEIGHT lawnHeight) return false; for (int i = 3; i blocks.Count; i+) if (blocks0.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 HungrySnake class Game public 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.HEIGHT * Block.HEIGHT; Direction direction = (Direction)random.Next(1, 4); snake = new Snake(Color.YellowGreen, direction); bean = new Bean(Color.Pink); isSnakeAlive = false; lawnWidth = _lawnWidth; lawnHeight = _lawnHeight; public void Begin(Graphics g, Color backGroundColor, int lawnWidth, int lawnHeight) 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.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); if (!snake.IsAlive(lawnWidth, lawnHeight) isSnakeAlive = false; 3.3.6 MainForm(主界面)类设计MainForm可通过继承 System.Windows.Forms.Form实现,通过向MainForm中添加控件及设置控件的属性系统会自动向MainForm类中添加相应的对象字段,如本程序的menuStrip1和timer1等。3.3.6.1 添加属性添加属性game,来控制游戏的开始和运行,参考代码如下:private Game game;3.3.6.2 修改构造函数在构造函数的最后对属性game 进行实例化,参考代码如下:game = new Game(lawn.Width, lawn.Height);3.3.6.3Panel 容器Panel容器作为游戏运行的场地。Snake和Bean都在Panel容器中绘制。Panel容器的添加步骤如下:l 在MainForm中添加一个Panel容器。l 拖动Panel容器的大小才,使其充满整个窗体。l 修改Panel容器的属性Name=lawn。l 修改Panel容器的属性BackColor=Green。3.3.6.4Timer组件Timer组件用于在用户定义的时间间隔引发事件。Timer组件的添加步骤如下:l 在MainForm 中添加一个Timer 组件。l 修改Timer 组件的属性Enalbed=false。l 修改Timer 组件的属性Interval=100。l 双击,生成Tick 事件。Tick 事件的参考代码如下: if (game.isSnakeAlive) Graphics g; g = lawn.CreateGraphics(); game.OnTime(g, lawn.BackColor, lawn.Width, lawn.Height); if (!game.isSnakeAlive) MessageBox.Show(Game Over!); 3.3.6.5MenuStrip 菜单MenuStrip菜单用于提供用户可以使用的功能。MenuStrip菜单的添加步骤如下:l 在MainForm 中添加一个MenuStrip菜单。l 添加菜单项如下图所示。l 修改菜单项“开局”的属性Name=ToolStripMenuItemStart。l 双击菜单项“开局”,生成Click 事件。Click 事件的参考代码如下: Graphics g; g = lawn.CreateGraphics(); game.Begin(g, lawn.BackColor, lawn.Width, lawn.Height); timer1.Enabled = true;3.3.6.6KeyDown 事件在贪吃蛇游戏中,使用“”、“”、“”和“”四个方向键改变蛇的移动方向。KeyDown 事件用于在用户按键时,提供改变蛇移动方向的功能。KeyDown 事件的添加步骤如
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中学生心理健康教育途径
- 2025年房地产经纪人资格考试试题及答案
- 2025年地方治理与管理硕士入学考试试题及答案
- 2025年大气科学与环境监测考试卷及答案
- 平安保险早会标准化流程
- 保洁部清洁工具标准化细则
- 小猫和鱼儿的故事童话类作文(6篇)
- 技术咨询服务条款及合同
- 分析在线教育的发展趋势及其对教育行业的影响
- 小区绿化养护及生态改造工程协议
- 2025年新疆中考数学试卷真题
- 国内在线教育的发展状况研究论文3000字
- DL-T5153-2014火力发电厂厂用电设计技术规程
- 每天100道语法填空题过高考英语高频词汇12
- 配电室巡检记录表
- 数字程控交换机系统技术规范书
- 卓越绩效评价准则概述(专业性权威性实用性)
- GB 1886.20-2016食品安全国家标准食品添加剂氢氧化钠
- 国资进场交易工作流程讲座
- 当代法律英语翻译全
- 下承式钢桁梁桥结构设计及优化 (跨度64m)
评论
0/150
提交评论