/// <summary> /// snake是否吃到自己 /// </summary> public void HitSelfTest() { SnakeItem headItem = m_snakeBody.First(); for (int i = 1; i < m_snakeBody.Count; i++) { if (m_snakeBody.ElementAt(i).ItemPositon == headItem.ItemPositon) { this.IsAlive = false; return; } } }
/// <summary> /// snakebody移动 /// </summary> public void Move() { SnakeItem newItem; Point newItemPos = new Point(); SnakeItem headItem = m_snakeBody.First(); SnakeItem tailItem = m_snakeBody.Last(); int tailIndex = m_snakeBody.IndexOf(tailItem); int headIndex = m_snakeBody.IndexOf(headItem); // 计算新的头部的位置 switch (SnakeBodyDirec) { case Direction.SOUTH: newItemPos.X = headItem.ItemPositon.X; newItemPos.Y = headItem.ItemPositon.Y + m_snakeMoveDis; break; case Direction.NORTH: newItemPos.X = headItem.ItemPositon.X; newItemPos.Y = headItem.ItemPositon.Y - m_snakeMoveDis; break; case Direction.WEST: newItemPos.X = headItem.ItemPositon.X - m_snakeMoveDis; newItemPos.Y = headItem.ItemPositon.Y; break; case Direction.EAST: newItemPos.X = headItem.ItemPositon.X + m_snakeMoveDis; newItemPos.Y = headItem.ItemPositon.Y; break; case Direction.PAUSE: return; default: break; } // 新的头部 // 将头部的颜色设置为身体的颜色 headItem.ItemColor = this.SnakeBodyColor; newItem = new SnakeItem(newItemPos); newItem.ItemColor = this.SnakeHeadColor; // 移除尾部item RemoveSnakeItem(tailIndex); m_snakeBody.Insert(headIndex, newItem); }
public void CreateSnakeBody(Point beginPos, Graphics snakeGrap) { RemoveAllSnakeItem(); this.TotalScore = 0; this.m_snakeGrap = snakeGrap; this.SnakeBodyDirec = Direction.WEST; // 初始化方向为west SnakeItem headItem = new SnakeItem(beginPos); headItem.ItemColor = this.SnakeHeadColor; m_snakeBody.Add(headItem); // 初始化snake for (int i = 0; i < 6; i++) { AddSnakeItem(); } IsAlive = true; }