public Game(Size boardGridSize, Size cellSize) { _boardGridSize = boardGridSize; BackgroundColor = Color.White; Snake = new SnakeBuilder().SetStartPosition(new Point(x: _boardGridSize.Width / 2, y: _boardGridSize.Height / 2)) .SetLength(3) .SetPartSize(cellSize) .SetSnakeBrush(Brushes.LightBlue) .SetSnakeHeadBrush(Brushes.CornflowerBlue) .Build(); Snake.CrashAccident += (sender, e) => CrashAccident?.Invoke(sender, e); Score = new GameScore(); Score.ScoreChanged += (sender, e) => ScoreChanged?.Invoke(sender, e); Ball = new Ball(cellSize); StartNewGame(); }
public void MoveTo(Point movementVector, Size bounds) { var newPositionOfHead = GetHead().Add(movementVector); newPositionOfHead = new Point((newPositionOfHead.X + bounds.Width) % bounds.Width, (newPositionOfHead.Y + bounds.Height) % bounds.Height); if (IsCollisionExistsOnHead(newPositionOfHead)) { CrashAccident?.Invoke(this, null); return; } // move a body ahead for (int i = _partsOfsnake.Count - 1; i > 0; --i) { _partsOfsnake[i] = _partsOfsnake[i - 1]; } // move a head SetHead(newPositionOfHead); }