/// <summary> /// a snake with a direction. /// </summary> /// <param name="d">initial direction</param> public Snake(Direction d) { this.Direction = d; this.Body = new LinkedList<Dot>(); for (int i = 3; i >= 0; i--) { Dot newD = new Dot(i, 0); this.Body.AddLast(newD); } }
/// <summary> /// /// </summary> /// <param name="dot"></param> /// <param name="isFood"></param> private void ShowDot(Dot dot, bool isFood) { if (isFood) { g.FillEllipse(this.pen, dot.PointReal.X, dot.PointReal.Y, dot.Size.Width, dot.Size.Height); } else { g.FillRectangle(this.pen, dot.PointReal.X, dot.PointReal.Y, dot.Size.Width, dot.Size.Height); } }
/// <summary> /// Test if the snake hits the wall. /// </summary> /// <param name="newHead"></param> /// <returns>True if it hits the wall.</returns> private bool IsWallOrBodyHit(Dot newHead) { if (newHead.Point.X < 0 || newHead.Point.X >= Consts.MAX_X || newHead.Point.Y < 0 || newHead.Point.Y >= Consts.MAX_Y) { return true; } if (this.gameData.Snake.Body.Contains(newHead)) { return true; } return false; }
/// <summary> /// Test if the snake hits the food. /// </summary> /// <param name="newHead">newHead</param> /// <returns>True if the snake head is at the same position with the food.</returns> private bool IsFoodEaten(Dot newHead) { return this.gameData.Food.Equals(newHead); }
/// <summary> /// Generates the next food instance. /// </summary> /// <returns>The instance of next food</returns> private Dot GetNextFood() { int x, y; Dot d; do { x = this.rand.Next(0, Consts.MAX_X); y = this.rand.Next(0, Consts.MAX_Y); d = new Dot(x, y); } while (this.gameData.Snake.Body.Contains(d)); return d; }
/// <summary> /// /// </summary> /// <param name="dot"></param> private void FadeDot(Dot dot) { g.FillRectangle(this.penErase, dot.PointReal.X, dot.PointReal.Y, dot.Size.Width, dot.Size.Height); }