您好,欢迎访问三七文档
大连理工大学管理学院本科生实验报告信息系统开发工具实验命题:_____贪吃蛇小游戏开发______________________专业:信息管理与信息系统班级:管信1701学号:201705028学生姓名:张春立完成时间:2019年5月30基于C#窗体程序的贪吃蛇小游戏的开发1.首先展示设计界面效果已及运行效果窗体设计界面运行效果2.使用到的控件画布,按钮,textbox,定时器3.操作方法点击开始/暂停按钮开始游戏或暂停游戏。蛇可以无限长大,也就是说永远不会胜利,最终也会失败。蛇撞到墙或者自己则结束游戏,游戏将被重置。4.大概实现思路(1)在画布上画出三个连续的黑色矩形(设置圆角,让它看起来像那么回事儿)(2)在画布上随机生成10个矩形(同样设置圆角),作为蛇的食物。(3)蛇移动到与食物相交的时候食物消失,蛇的长度加一,吃掉的食物消失,再重新生成一个食物。(4)重复以上步骤,直到玩家撞到墙或者撞到自己。5.构建类构建SnackPlayer类,BodyPart类,Point类,GamePart类,FoodManager类,FoodPellet类,这些类全部都在Snack命名空间下。FoodPellet类继承GamePart类。使用窗体控件建立窗体及相关控件。6.构建的类的作用及实现SnackPlayer类:定义了玩家蛇的基本属性,如方向,大小,增加长度,并用BodyPart实例化对象列表,表示蛇的每一部分。publicenumDirection{left,right,up,down,none}privateListBodyPartm_SnakeParts=newListBodyPart();privateconstintm_CircleRadius=20;privateDirectionm_MoveDirection=Direction.none;privateintm_PendingSegments;构造函数publicSnakePlayer(SnakeFormForm)实现初始化蛇的长度以及位置m_SnakeParts.Add(newBodyPart(100,0,Direction.right));m_SnakeParts.Add(newBodyPart(80,0,Direction.right));m_SnakeParts.Add(newBodyPart(60,0,Direction.right));主要方法有publicvoidAddBodySegments(intNumber)//增加蛇的长度publicvoidMovePlayer()//控制蛇移动,通过对画布上面坐标的改变来实现publicvoidSetDirection(DirectionDir)//控制蛇移动方向BodyPart类:用来初始化蛇的方向GamePart类:用来获取蛇所在点的位置FoodManager类:属性,生成随机数,用来随机生成食物所在的点;同时FoodPellet实例化对象列表,代表所有食物对象。privateRandomr=newRandom();privateListFoodPelletm_FoodPellets=newListFoodPellet();方法publicvoidAddRandomFood(){intX=r.Next(m_GameWidth-m_CircleRadius);//Randomx/ypositionsintY=r.Next(m_GameHeight-m_CircleRadius);intix=(X/m_CircleRadius);//Usetruncatingtosnaptogrid//对齐网格intiy=Y/m_CircleRadius;X=ix*m_CircleRadius;//Gridx/ypositionsY=iy*m_CircleRadius;m_FoodPellets.Add(newFoodPellet(X,Y));//保存食物对象}//增加一个食物点publicvoidAddRandomFood(intAmount){for(inti=0;iAmount;i++){AddRandomFood();}}//增加食物7.总体逻辑实现publicSnakeForm(){InitializeComponent();Application.AddMessageFilter(this);this.FormClosed+=(s,e)=Application.RemoveMessageFilter(this);Player1=newSnakePlayer(this);FoodMngr=newFoodManager(GameCanvas.Width,GameCanvas.Height);FoodMngr.AddRandomFood(10);ScoreTxtBox.Text=score.ToString();}//初始化窗体,并获取食物与蛇的实例,随机获取十个食物点publicvoidToggleTimer(){GameTimer.Enabled=!GameTimer.Enabled;}//通过计时器设置蛇的开始与暂停privatevoidGameTimer_Tick(objectsender,EventArgse){SetPlayerMovement();//移动CheckForCollisions();//检测碰撞是需要结束游戏,还是需要对蛇的长度加一GameCanvas.Invalidate();//重新绘制画布}//通过计时器来触发蛇的移动与检验是否结束游戏privatevoidCheckForCollisions(){if(Player1.IsIntersectingRect(newRectangle(-100,0,100,GameCanvas.Height)))Player1.OnHitWall(Direction.left);if(Player1.IsIntersectingRect(newRectangle(0,-100,GameCanvas.Width,100)))Player1.OnHitWall(Direction.up);if(Player1.IsIntersectingRect(newRectangle(GameCanvas.Width,0,100,GameCanvas.Height)))Player1.OnHitWall(Direction.right);if(Player1.IsIntersectingRect(newRectangle(0,GameCanvas.Height,GameCanvas.Width,100)))Player1.OnHitWall(Direction.down);//检测是否与食物相撞ListRectangleSnakeRects=Player1.GetRects();foreach(RectanglerectinSnakeRects){if(FoodMngr.IsIntersectingRect(rect,true)){FoodMngr.AddRandomFood();Player1.AddBodySegments(1);score++;ScoreTxtBox.Text=score.ToString();}}}//检测碰撞是需要结束游戏,还是需要对蛇的长度加一privatevoidSetPlayerMovement(){if(Input.IsKeyDown(Keys.Left)){Player1.SetDirection(Direction.left);}elseif(Input.IsKeyDown(Keys.Right)){Player1.SetDirection(Direction.right);}elseif(Input.IsKeyDown(Keys.Up)){Player1.SetDirection(Direction.up);}elseif(Input.IsKeyDown(Keys.Down)){Player1.SetDirection(Direction.down);}Player1.MovePlayer();}//让蛇动起来privatevoidStart_Btn_Click(objectsender,EventArgse){ToggleTimer();}//开始与暂停按钮privatevoidGameCanvas_Paint(objectsender,PaintEventArgse){Graphicscanvas=e.Graphics;Player1.Draw(canvas);FoodMngr.Draw(canvas);}//对画布进行初始化,并绘制蛇和食物
本文标题:C#小游戏开发
链接地址:https://www.777doc.com/doc-3948472 .html