贪吃蛇说详细明.doc_第1页
贪吃蛇说详细明.doc_第2页
贪吃蛇说详细明.doc_第3页
贪吃蛇说详细明.doc_第4页
贪吃蛇说详细明.doc_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

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

文档简介

贪吃蛇目录一、游系功能介绍二、 窗体界面设计三、 Snack蛇类四、 Food食物类五、窗体代码设计六、实现流程图七、运行图解八、说明图解九、总结一.游戏功能介绍 本游戏主要实现的是贪吃蛇吃东西,吃食物加分,每次吃一个食物他会变长,当分数达到一定值会升级,所谓的升级就是速度加快,当蛇头撞到自己的身体部位或者是墙游戏就会结束,所谓的撞墙就是蛇头越界超出自己所规定的界面,在游戏未开始前可以对游戏难度进行设置,还可以设置蛇体颜色,食物颜色,蛇体。食物大小,当游戏开始的时候,出了对游戏进行暂停外,不得对游戏进行其他操作。 二窗体界面设计 2个窗体,1个主窗体Form1里面第一层是建一个菜单栏包裹游戏选项,设置,帮助。游戏选项里面包括(开始,暂停,退出)设置里面包括(难度等级(初级,中级,高级)蛇体颜色,食物颜色,蛇体大小)帮助里面显示的是Form2界面内容 菜单栏下面一个容器panel 旁边是3个lable标签,第一个显示分数字;第二个是显示运行时的实际分数;第三个显示的是游戏规则说明下面就是一个文本框用来接收收状态的,旁边就是一个游戏结束按钮便于立刻结束,克服了玩家不想玩而又退不退不出去的障碍对了在窗体里面拖放一个计时器用来计时三Snack蛇类1、定义字段:public static int SnakeBone = 10;/设置蛇体模块大小public static int Direct = 0;/设置方向public static Point Place = new Point(-1, -1), new Point(-1, /设置蛇体各模块的位置public static bool hasFood = false;/是否有食物,有食物为truepublic static bool isGame = false;/游戏是否结束,游戏结束为truepublic static int Field_with = 0; /场地的宽度public static int Field_length = 0;/场地的长度public static Control control; /记录绘制贪吃蛇的控件public static Timer timer; /记录Time组件public static SolidBrush SolidSnake = new SolidBrush(Color.Green);/设置贪吃蛇身体颜色public static SolidBrush SolidBack = new SolidBrush(Color.Green); /设置背景颜色public static Label label; /记录label控件public static ArrayList List = new ArrayList();/实例化一个ArrayList数组Graphics g;/实例化 Graphics类Food food = new Food();2、命名空间的支持using System.Drawing;支持:SolidBrush 、Graphics、Point类using System.Windows.Forms;支持:Control、Timer、Label类using System.Collections;支持:ArrayList类3 、定义一个函数用于绘制贪吃蛇的起始位置,以及对游戏场地进行初始化设置的方法 public void GamePlaceSet(Control con, int Bone) Field_with = con.Width;/获取场地的宽度 Field_length = con.Height;/获取场地的长度 SnakeBone = Bone;/记录蛇体模块大小 control = con;/记录背景控件 g = control.CreateGraphics();/创建背景控件的Graphics类 SolidBack = new SolidBrush(con.BackColor);/设置画刷颜色 for (int i = 0; i Place.Length; i+) Placei.X = (Place.Length - i - 1) * SnakeBone;/设置蛇初始位置的横坐标 Placei.Y = (Field_length / 2) - SnakeBone;/设置蛇初始位置的纵坐标 g.FillRectangle(SolidSnake, Placei.X + 1, Placei.Y + 1, SnakeBone - 1, SnakeBone - 1);/绘制蛇体 List = new ArrayList(Place);/记录每一个模块的位置 isGame = false;/停止游戏 Direct = 0;/设置方向向右 4、定义一个SnakeMove函数用于蛇体移动,根据蛇的位置,判断是否吃到食物,如果吃到食物,重新生成食物public void SnakeMove(int N, Form1 control1) Point tem_point = new Point(-1, -1);/定义坐标结构 switch (N) case 0: tem_point.X = (Point)List0).X + SnakeBone;/蛇头右移动 tem_point.Y = (Point)List0).Y; break; case 1: tem_point.X = (Point)List0).X - SnakeBone;/蛇头左移动 tem_point.Y = (Point)List0).Y; break; case 2: tem_point.X = (Point)List0).X; tem_point.Y = (Point)List0).Y - SnakeBone;/蛇头上移动 break; case 3: tem_point.X = (Point)List0).X; tem_point.Y = (Point)List0).Y + SnakeBone;/蛇头下移动 break; if (!EstimateMove(tem_point) /如果没有向相反的方向移动 Direct = N; /改变贪吃蛇的方向 if (!GameEnd(tem_point) /如果游戏没有结束 ProtractSnake(tem_point); /重新绘制蛇体 Eatefood(control1); /吃食物 /绘制食物 g.FillRectangle(Food.SolidFood, Food.FoodPoint.X + 1, Food.FoodPoint.Y + 1, SnakeBone - 1, SnakeBone - 1); 5、吃东西 public void Eatefood(Form1 control1) int m, n; /记录分数 if (Point)List0) = Food.FoodPoint)/如果蛇头吃到了食物 List.Add(ListList.Count - 1);/在蛇的尾部添加蛇身 hasFood = false;/没有食物 food.Buildfood(hasFood, control);/生成食物 m = Convert.ToInt32(label.Text); if (m = 10)/积分为10时加速 n = control1.speed - 50; timer.Interval = n; label.Text = Convert.ToString(m + 5);/显示当前分数 g.DrawString(一级加速中, new Font(宋体, 15, FontStyle.Bold), new SolidBrush(Color.Green), new PointF(180, 30); else if (m = 20)/积分为20时加速 g.FillRectangle(Snake.SolidBack, 0, 0, control.Width, control.Height); control1.ProtractTable(g); g.DrawString(二级加速中, new Font(宋体, 15, FontStyle.Bold), new SolidBrush(Color.Blue), new PointF(180, 30); n = control1.speed - 100; timer.Interval = n; label.Text = Convert.ToString(m + 5);/显示当前分数 else if (m = 30)/积分为30时加速 g.FillRectangle(Snake.SolidBack, 0, 0, control.Width, control.Height); control1.ProtractTable(g); g.DrawString(三级加速中, new Font(宋体, 15, FontStyle.Bold), new SolidBrush(Color.Red), new PointF(180, 30); n = control1.speed - 150; timer.Interval = n; label.Text = Convert.ToString(m + 5);/显示当前分数 else label.Text = Convert.ToString(Convert.ToInt32(label.Text) + 5);/显示当前分数 6、用于判断当前游戏是否结束public bool GameEnd(Point GE) bool tem_e = false;/用于判断游戏是否结束 bool tem_body = false;/用于判断蛇身是否重叠 for (int i = 1; i List.Count; i+) if (Point)List0) = (Point)Listi)/如果重叠则返回ture,游戏结束 tem_body = true; /判断蛇头是否超出游戏场地 if (GE.X = control.Width - 1 | GE.Y = control.Height - 1 | tem_body) /游戏结束提示 g.DrawString(游戏结束!, new Font(宋体, 50, FontStyle.Bold), new SolidBrush(Color.Yellow), new PointF(100, 150); isGame = true;/游戏结束 timer.Stop();/停止计时器 tem_e = true; return tem_e; 7、用于判断贪吃蛇的移动方向是否向相反的方向移动,如果方向移动则返回false public bool EstimateMove(Point ES) bool tem_d = false;/记录蛇头是否向相反的方向移动 if (ES.X = (Point)List0).X & ES.Y = (Point)List0).Y)/如果蛇头向相反的方向移动,则返回true tem_d = true; return tem_d; 8、重新画蛇public void ProtractSnake(Point Pr) bool tem_bool = false;/用于确定是否清除移动后的蛇体 List.Insert(0, Pr);/根据蛇头的移动方向设置蛇头的位置 Point tem_point = (Point)ListList.Count - 1);/记录蛇头的位置 List.RemoveAt(List.Count - 1);/移除蛇的尾部 for (int i = 0; i List.Count - 1; i+)/使蛇体模块向前移动一位 if (tem_point = (Point)Listi) tem_bool = true; if (!tem_bool) /清除贪吃蛇移动前的尾部 g.FillRectangle(SolidBack, tem_point.X + 1, tem_point.Y + 1, SnakeBone - 1, SnakeBone - 1); for (int i = 0; i List.Count; i+)/重新绘制蛇体 g.FillRectangle(SolidSnake, (Point)Listi).X + 1, (Point)Listi).Y + 1, SnakeBone - 1, SnakeBone - 1); 四、Food食物类1、定义public static SolidBrush SolidFood = new SolidBrush(Color.Orange);/设置食物颜色public static Point FoodPoint = new Point(-1, -1);/设置蛇所在的位置2、生成食物函数public void Buildfood(bool hasFood, Control con) if (hasFood = false)/如果没有食物 Point tem_p = new Point(-1, -1);/定义坐标结构 bool tem_bool = false;/计算出食物的位置为true,否则为false while (!tem_bool)/计算显示食物的位置 bool tem_b = false;/用于确定生成的食物 是否和蛇体重叠 tem_p = RectPoint(con);/随机生成食物的位置 for (int i = 0; i Snake.List.Count; i+) if (Point)Snake.Listi = tem_p) /如果随机生成的食物和蛇体重叠 tem_b = true; /记录重叠 break; /重叠则停止for循环 if (tem_b = false)/如果食物和蛇体没有重叠 tem_bool = true;/计算出食物的位置位true,否则为false break; FoodPoint = tem_p;/记录食物的显示位置 hasFood = true;/有食物 3、用Random类随机生成随机的纵坐标和横坐标,用返回Point结构 public Point RectPoint(Control pon) int tem_R = pon.Width / Snake.SnakeBone;/获取场地的行数 int tem_C = pon.Height / Snake.SnakeBone;/获取场地的列数 Random RandomA = new Random();/实例化Random类 tem_R = RandomA.Next(tem_R);/生成横坐标 tem_C = RandomA.Next(tem_C);/生成纵坐标 Point tem_RC = new Point(tem_R * Snake.SnakeBone, tem_C * Snake.SnakeBone);/生成随机点的显示位置 return tem_RC; 五、窗体的代码设计Form1 类1、 定义字段public int speed = 350; /用于设置贪吃蛇的速度单位为毫秒public static bool ifStart = false; /用于游戏是否开始public static bool pause = true; /用于游戏是否暂停,游戏暂停为trueSnake snake = new Snake();/实例化Snake类Food food = new Food();2、绘制游戏场景的网格public void ProtractTable(Graphics G)for (int i = 0; i = panel1.Width / Snake.SnakeBone; i+)/绘制单元格的纵向线G.DrawLine(new Pen(Color.LightGreen, 1), new Point(i * Snake.SnakeBone, 0), new Point(i * Snake.SnakeBone, panel1.Height);for (int i = 0; i = panel1.Height / Snake.SnakeBone; i+)/绘制单元格的横向线G.DrawLine(new Pen(Color.LightGreen, 1), new Point(0, i * Snake.SnakeBone), new Point(panel1.Width, i * Snake.SnakeBone);3、游戏开始和为开始的2种情况private void panel1_Paint(object sender, PaintEventArgs e) Graphics G= panel1.CreateGraphics();/创建panel1控件的Graphics类 ProtractTable(G); /绘制游戏场景 if (!ifStart) /如果没有开始游戏 Snake.timer = timer1; Snake.label = label2; snake.GamePlaceSet(panel1, Snake.SnakeBone);/初始化场地及贪吃蛇信息; else for (int i = 0; i Snake.List.Count;i+)/绘制蛇体 e.Graphics.FillRectangle(Snake.SolidSnake, (Point)Snake.Listi).X +1, (Point)Snake.Listi).Y +1,Snake.SnakeBone-1,Snake.SnakeBone-1); /绘制食物 e.Graphics.FillRectangle(Food.SolidFood, Food.FoodPoint.X+1, Food.FoodPoint.Y+1, Snake.SnakeBone-1, Snake.SnakeBone-1); if(Snake.isGame)/如果游戏结束 e.Graphics.DrawString(游戏结束!, new Font(宋体, 50, FontStyle.Bold), new SolidBrush(Color.Yellow), new PointF(100, 150); 4、游戏操作说明 private void 游戏操作说明F3ToolStripMenuItem_Click(object sender, EventArgs e)if (ifStart = false) | (Snake.isGame)/如果游戏正在运行Form2 temp = new Form2();/初始 一个 help实例if (temp.ShowDialog() = DialogResult.OK) /把Form2的对话说明框给显示出来temp.Dispose();/释放资源5、初始大小(设置初始大小为选中项) private void 初始大小ToolStripMenuItem_Click(object sender, EventArgs e) if (ifStart = false) | (Snake.isGame) 初始大小ToolStripMenuItem.Checked = false; 放大2倍ToolStripMenuItem.Checked = false; 放大3倍ToolStripMenuItem.Checked = false; (ToolStripMenuItem)sender).Checked = true;/设置当前项选中 Snake.SnakeBone = 10;/设置蛇块的初始大小为10 6、放大2倍private void 放大2倍ToolStripMenuItem_Click(object sender, EventArgs e)if (ifStart = false) | (Snake.isGame)初始大小ToolStripMenuItem.Checked = false;放大2倍ToolStripMenuItem.Checked = false;放大3倍ToolStripMenuItem.Checked = false;(ToolStripMenuItem)sender).Checked = true;/设置当前项选中Snake.SnakeBone = 20;7、放大3倍private void 放大3倍ToolStripMenuItem_Click(object sender, EventArgs e)if (ifStart = false) | (Snake.isGame)初始大小ToolStripMenuItem.Checked = false;放大2倍ToolStripMenuItem.Checked = false;放大3倍ToolStripMenuItem.Checked = false;(ToolStripMenuItem)sender).Checked = true;/设置当前项选中Snake.SnakeBone = 30;/初始大小为308、游戏等级为初级private void 初级ToolStripMenuItem_Click(object sender, EventArgs e)if (ifStart = false) | Snake.isGame)/在游戏没有开始和游戏结束时才可以选择设置初级ToolStripMenuItem.Checked = false;/设置初级项被选中中级ToolStripMenuItem.Checked = false;/设置中级项被选中高级ToolStripMenuItem.Checked = false;/设置高级项目被选中(ToolStripMenuItem)sender).Checked = true;/设置当前项选中speed = 350;/初始速度为350毫秒textBox1.Focus();/获得焦点9、游戏等级为中级private void 中级ToolStripMenuItem_Click(object sender, EventArgs e)if (ifStart = false) | Snake.isGame)/在游戏没有开始和游戏结束时才可以选择设置初级ToolStripMenuItem.Checked = false;/设置初级项被选中中级ToolStripMenuItem.Checked = false;/设置中级项被选中高级ToolStripMenuItem.Checked = false;/设置高级项目被选中(ToolStripMenuItem)sender).Checked = true;/设置当前项选中speed = 250;/中级初始速度为250毫秒textBox1.Focus();/获得焦点10、游戏等级为高级高级private void 高级ToolStripMenuItem_Click(object sender, EventArgs e)if (ifStart = false) | (Snake.isGame)/在游戏没有开始和游戏结束时才可以选择设置初级ToolStripMenuItem.Checked = false;/设置初级项被选中中级ToolStripMenuItem.Checked = false;/设置中级项被选中高级ToolStripMenuItem.Checked = false;/设置高级项目被选中(ToolStripMenuItem)sender).Checked = true;/设置当前项选中speed = 200;/高级 初始速度为200毫秒textBox1.Focus();/获得焦点蛇体颜色(绿红蓝黑)11、绿色、private void 绿色ToolStripMenuItem_Click(object sender, EventArgs e)if (ifStart = false) | (Snake.isGame)绿色ToolStripMenuItem.Checked = false;红色ToolStripMenuItem.Checked = false;蓝色ToolStripMenuItem.Checked = false;黑色ToolStripMenuItem.Checked = false;(ToolStripMenuItem)sender).Checked = true;/设置当前项选中Snake.SolidSnake = new SolidBrush(Color.Green);12、红色private void 红色ToolStripMenuItem_Click(object sender, EventArgs e)if (ifStart = false) | (Snake.isGame)绿色ToolStripMenuItem.Checked = false;红色ToolStripMenuItem.Checked = false;蓝色ToolStripMenuItem.Checked = false;黑色ToolStripMenuItem.Checked = false;(ToolStripMenuItem)sender).Checked = true;/设置当前项选中Snake.SolidSnake = new SolidBrush(Color.Red);13、蓝色 private void 蓝色ToolStripMenuItem_Click(object sender, EventArgs e) if (ifStart = false) | (Snake.isGame) 绿色ToolStripMenuItem.Checked = false; 红色ToolStripMenuItem.Checked = false; 蓝色ToolStripMenuItem.Checked = false; 黑色ToolStripMenuItem.Checked = false; (ToolStripMenuItem)sender).Checked = true;/设置当前项选中 Snake.SolidSnake = new SolidBrush(Color.Blue); 14、黑色 private void 黑色ToolStripMenuItem_Click(object sender, EventArgs e) if (ifStart = false) | (Snake.isGame) 绿色ToolStripMenuItem.Checked = false; 红色ToolStripMenuItem.Checked = false; 蓝色ToolStripMenuItem.Checked = false; 黑色ToolStripMenuItem.Checked = false; (ToolStripMenuItem)sender).Checked = true;/设置当前项选中 Snake.SolidSnake = new SolidBrush(Color.Black); 食物颜色(橙黄紫)15、橙色private void 橙色ToolStripMenuItem_Click(object sender, EventArgs e)if (ifStart = false) | (Snake.isGame)橙色ToolStripMenuItem.Checked = false;黄色ToolStripMenuItem.Checked = false;紫色ToolStripMenuItem.Checked = false;(ToolStripMenuItem)sender).Checked = true;/设置当前项选中Food.SolidFood = new SolidBrush(Color.Orange);16、黄色 private void 黄色ToolStripMenuItem_Click(object sender, EventArgs e) if (ifStart = false) | (Snake.isGame) 橙色ToolStripMenuItem.Checked = false; 黄色ToolStripMenuItem.Checked = false; 紫色ToolStripMenuItem.Checked = false; (ToolStripMenuItem)sender).Checked = true;/设置当前项选中 Food.SolidFood = new SolidBrush(Color.Yellow); 17、紫色 private void 紫色ToolStripMenuItem_Click(object sender, EventArgs e) if (ifStart = false) | (Snake.isGame) 橙色ToolStripMenuItem.Checked = false; 黄色ToolStripMenuItem.Checked = false; 紫色ToolStripMenuItem.Checked = false; (ToolStripMenuItem)sender).Checked = true;/设置当前项选中 Food.SolidFood = new SolidBrush(Color.Purple); 18、开始private void 开始F2ToolStripMenuItem_Click(object sender, EventArgs e)/控制游戏的开始、暂停、和退出ifStart = false;Graphics G = panel1.CreateGraphics();/创建panel1控件的Graphics类/刷新游戏场地G.FillRectangle(Snake.SolidBack, 0, 0, panel1.Width, panel1.Height);ProtractTable(G);/绘制游戏场地ifStart = true;/开始游戏snake.GamePla

温馨提示

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

评论

0/150

提交评论