郑州轻工业学院实训报告实训名称:贪吃蛇游戏指导教师:姓名:学号:班级:提交日期:11.实训目的通过开发一款贪吃蛇游戏程序,熟练掌握C#编程语言和面向对象程序设计方法,独立完成一个游戏程序的开发。2.实训题目使用C#编程语言,开发一款贪吃蛇游戏。3.功能描述1)游戏场地是一片矩形区域的草坪。2)一条蛇由蛇头和蛇身组成。3)当游戏开始之后,草坪中出现一颗豆和一条蛇,并且蛇不停地移动,蛇移动的方向与蛇头的方向一致。4)当游戏暂停之后,蛇停止移动。25)当蛇移动时,玩家可以使用“↑”、“↓”、“←”和“→”四个方向键改变蛇的移动方向。6)当蛇头与豆的位置重合时,豆被蛇吃掉,同时在草坪中再生成一颗新的豆,蛇身增加一节。7)当蛇头碰到蛇身时,则咬断蛇身,后半部分的蛇身消失。8)点击加速或者减速,蛇会加快或者减少移动速度。9)游戏有计分功能。10)当蛇头碰到草坪四周时,蛇立即毙命,游戏结束。4.需求分析根据功能描述可知,贪吃蛇游戏的系统结构图如下所示。5.设计说明根据需求分析可知,Snake的每一节都有位置和大小等属性。而Bean也具有这两个属性。抽象出二者的共同特征,抽象出一般类Block,用于描述一个块。Block派生出Bean和SnakeBlock两个类,其中SnakeBlock类用于描述蛇的一节。为了使游戏的运行更易于控制,定义Game类用于启动、暂停和继续游戏。根据需求分析可知,Lawn仅包含大小和颜色两个属性。为了减少类的数量,可将其大小和颜色等属性添加到Game类中。综上所述,在贪吃蛇游戏中,有Block(块)、Bean(豆)、SankeBlock(节)、Snake(蛇)、Game(游戏)和MainForm(用户接口)六个类。贪吃蛇游戏的逻辑模型如下图所示。36.源代码Form1源码:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceWindowsFormsApplication3{publicpartialclassForm1:Form{privateGamegame;publicForm1(){InitializeComponent();game=newGame(lawn.Width,lawn.Height);}privatevoidtimer1_Tick(objectsender,EventArgse)4{if(game.isSnakeAlive){Graphicsg;g=lawn.CreateGraphics();game.OnTime(g,lawn.BackColor,lawn.Width,lawn.Height);if(!game.isSnakeAlive){MessageBox.Show(您的得分为:+score.Text.ToString(),你已经死了!,MessageBoxButtons.OK,MessageBoxIcon.Information);}}score.Text=(game.snake.Length-3).ToString();}privatevoidToolStripMenuItemStart_Click(objectsender,EventArgse){Graphicsg;g=lawn.CreateGraphics();game.Begin(g,lawn.BackColor,lawn.Width,lawn.Height);timer1.Enabled=true;Stop.Enabled=true;Continue.Enabled=false;}privatevoidToolStripMenuItemPause_Click(objectsender,EventArgse){timer1.Enabled=false;Stop.Enabled=false;Continue.Enabled=true;}privatevoidToolStripMenuItemContinue_Click(objectsender,EventArgse){timer1.Enabled=true;Stop.Enabled=true;Continue.Enabled=false;}privatevoidForm1_KeyDown(objectsender,KeyEventArgse){5if(game.snake.direction==Direction.Down||game.snake.direction==Direction.Up){switch(e.KeyCode){caseKeys.Left:game.snake.direction=Direction.Left;break;caseKeys.Right:game.snake.direction=Direction.Right;break;}}else{switch(e.KeyCode){caseKeys.Up:game.snake.direction=Direction.Up;break;caseKeys.Down:game.snake.direction=Direction.Down;break;}}}privatevoidmenuStrip1_ItemClicked(objectsender,ToolStripItemClickedEventArgse){}privatevoid游戏ToolStripMenuItem_Click(objectsender,EventArgse){}privatevoidlawn_Paint(objectsender,PaintEventArgse){}privatevoid菜单ToolStripMenuItem_Click(objectsender,EventArgse){}privatevoidStart_Click(objectsender,EventArgse){6Graphicsg;g=lawn.CreateGraphics();game.Begin(g,lawn.BackColor,lawn.Width,lawn.Height);timer1.Enabled=true;Stop.Enabled=true;Continue.Enabled=false;}privatevoidStop_Click(objectsender,EventArgse){timer1.Enabled=false;Stop.Enabled=false;Continue.Enabled=true;Add.Enabled=false;jian.Enabled=false;}privatevoidContinue_Click(objectsender,EventArgse){timer1.Enabled=true;Stop.Enabled=true;Continue.Enabled=false;}privatevoid帮助ToolStripMenuItem_Click(objectsender,EventArgse){MessageBox.Show(1.点击菜单上的“开始”、“暂停”、“\n\n继续”或者按对应快捷键F1、F2、F3控\n\n制游戏的开始、暂停或者继续\n\n2.上下左右键控制蛇的行动\n\n3.蛇撞到墙壁游戏结束\n\n4.蛇如果吃到自己的身体那么身体会断开\n\n5.地图会随着窗口变大而变大,帮助,MessageBoxButtons.OK,MessageBoxIcon.Information);}privatevoidAdd_Click(objectsender,EventArgse){Graphicsg;g=lawn.CreateGraphics();game.Begin(g,lawn.BackColor,lawn.Width,lawn.Height);timer1.Enabled=true;Stop.Enabled=true;Continue.Enabled=false;timer1.Interval-=20;}7privatevoid减速ToolStripMenuItem_Click(objectsender,EventArgse){Graphicsg;g=lawn.CreateGraphics();game.Begin(g,lawn.BackColor,lawn.Width,lawn.Height);timer1.Enabled=true;Stop.Enabled=true;Continue.Enabled=false;timer1.Interval+=20;}privatevoidscore_Click(objectsender,EventArgse){}}}Bean.cs源码:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Drawing;namespaceWindowsFormsApplication3{classBean:Block{publicBean(Color_color){origion=newPoint(0,0);color=_color;}publicvoidCreat(Graphicsg,ColorbackGroundColor,intlawnWidth,intlawnHeight,Snakesnake){Clear(g,backGroundColor);boolbGetAPosition=false;//是否找到生成豆的位置Randomrandom=newRandom();while(!bGetAPosition){8origion.X=random.Next(0,lawnWidth-1)/WIDTH*WIDTH;origion.Y=random.Next(0,lawnHeight-1)/HEIGHT*HEIGHT;inti;for(i=0;isnake.Length;i++){if(origion==snake.blocks[i].Origion)break;}if(i==snake.Length)bGetAPosition=true;}Display(g);}}}Block.cs源码:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Drawing;namespaceWindowsFormsApplication3{classBean:Block{publicBean(Color_color){origion=newPoint(0,0);color=_color;}publicvoidCreat(Graphicsg,ColorbackGroundColor,intlawnWidth,intlawnHeight,Snakesnake){Clear(g,backGroundColor);boolbGetAPosition=false;//是否找到生成豆的位置Randomrandom=newRandom();while(!bGetAPosition){origion.X=random.Next(0,lawnWidth-1)/WIDTH*WIDTH;origion.Y=random.Next(0,lawnHeight-1)/HEIGHT*HEIGHT;inti;9for(i=0;isnake.Length;i++){if(origion==snake.blocks[i].Origion)break;}if(i==snake.Le