private void MoveSnake() { while (snakeParts.Count >= snakeLength) { GameArea.Children.Remove(snakeParts[0].UiElement); snakeParts.RemoveAt(0); } foreach (SnakePart snakePart in snakeParts) { (snakePart.UiElement as Rectangle).Fill = snakeBodyBrush; snakePart.IsHead = false; } SnakePart snakeHead = snakeParts[snakeParts.Count - 1]; double nextX = snakeHead.Position.X; double nextY = snakeHead.Position.Y; switch (snakeDirection) { case SnakeDirection.Left: if (snakeHead.Position.X < 20) { nextX = GameArea.ActualWidth - 20; break; } nextX -= SnakeSquareSize; break; case SnakeDirection.Right: if (snakeHead.Position.X >= GameArea.ActualWidth - 20) { nextX = 0; break; } nextX += SnakeSquareSize; break; case SnakeDirection.Up: if (snakeHead.Position.Y < 20) { nextY = GameArea.ActualHeight - 20; break; } nextY -= SnakeSquareSize; break; case SnakeDirection.Down: if (snakeHead.Position.Y >= GameArea.ActualHeight - 20) { nextY = 0; break; } nextY += SnakeSquareSize; break; } snakeParts.Add(new SnakePart() { Position = new Point(nextX, nextY), IsHead = true }); DrawSnake(); DoCollisionCheck(); }