1、添加类的行为:绘制块draw ()方法,构造函数(判断是否豆),重载构造 函数3) Snake类:定义游戏中的蛇。其属性包括蛇移动的方向以及蛇身的集合(泛型集合List );其行为成员包括蛇吃豆、吃过豆之后蛇身增长、蛇身移动 以及判断蛇是否死亡等。4) Bea ns类:定义游戏中的豆。其属性包括 3个豆组成的列表(List 0; i-) / 蛇移动的方式bodyi = bodyi - 1;switch ( this .direction) / 蛇移动的方向case Direction .up:body0.Y -= side;case Directi on .dow n:body0.Y += s
2、ide;case Direction .left:body0.X -= side;case Directi on .right:body0.X += side;if (body0.X 537 | body0.Y 346) / 蛇撞四壁挂this .1 ive = false ;for ( int i = 1; i = number - 1; i+)if (body0.X = bodyi.X & body0.Y = bodyi.Y)thisive = false ;public void Eat( Food food) / 蛇吃食物Label label2 = new Label ();body
3、 nu mber = food.Positi on;this .numbe叶+;/ 长度加score += 2;label2.Text = Con vert .ToStri ng(score);public void Draw( Graphics g) / 绘制蛇SolidBrush RedBrush = new SolidBrush (Color .Red); g.DrawEllipse( Pen s.Red, body0.X, body0.Y, side, side);g.FillEllipse(RedBrush, body0.X, body0.Y, side, side);for (in
4、t i = 1;g.DrawRecta ngle( Pen s.Red, bodyi.X, bodyi.Y, side,side);g.FillRectangle(RedBrush, bodyi.X, bodyi.Y, side,if ( thisive = false )g.DrawString( Game Over!, new Font (宋体 ”,36,FontStyle .Bold), RedBrush, new Point (100, 150);public void resetSnake() / 重新设置蛇Point node1 = new Point (100, 320);Poi
5、nt node2 = new Point (90, 320);body1 = n ode1;body0 = node2;class FoodPoint position; / 食物位置Boolean exist; /判断失误是否存在 public Food()position = this .createdFood();exist = true ;public Point Position /Position 的属性return position;position = value ;public Boolean Exist /Exist 属性return exist;exist = value
6、 ;public Point createdFood() / 随机产生食物 Point position = new Point ();Ran domra n = new Ran don();position.X = ran.Next(1,30) * 10;position.Y = ran.Next(1,30) * 10; public void Draw( Graphics g) / 绘制食物SolidBrush greenBrush = new SolidBrush (Color .Green);g.FillEllipse(greenBrush, position.X, position.Y, 10, 10);