public void AddOnePart() { if (this.mLenght < mSnake.Length) { SnakePart newPart = new SnakePart(); mSnake[this.mLenght] = newPart; Canvas.SetLeft(newPart.mSnakePart, 0 - Constants.CellWidth * 10); Canvas.SetTop(newPart.mSnakePart, 0); mCanvas.Children.Add(newPart.mSnakePart); this.mLenght++; } }
public bool CheckRules() { SnakePart headSection = mSnake[0]; Rectangle headSectionRectangle = headSection.mSnakePart; int curHeadPosX = (int)Canvas.GetLeft(headSectionRectangle); int curHeadPosY = (int)Canvas.GetTop(headSectionRectangle); if (curHeadPosX + Constants.CellWidth > mCanvas.Width || curHeadPosX < 0) { return(false); } else if (curHeadPosY + Constants.CellHeight > mCanvas.Height || curHeadPosY < 0) { return(false); } Rect headRect = new Rect(Canvas.GetLeft(headSectionRectangle), Canvas.GetTop(headSectionRectangle), headSectionRectangle.Width, headSectionRectangle.Height); for (int i = 2; i < this.mLenght; ++i) { SnakePart tailSection = mSnake[i]; if (tailSection == null) { break; } Rectangle tailSectionRectangle = tailSection.mSnakePart; Rect tailRect = new Rect(Canvas.GetLeft(tailSectionRectangle), Canvas.GetTop(tailSectionRectangle), tailSectionRectangle.Width, tailSectionRectangle.Height); tailRect.Intersect(headRect); if (tailRect.Width > 0 && tailRect.Height > 0) { return(false); } } return(true); }