title: WPF Snake game following the mouse cursor
body: <p>I am trying to create a simple snake game where the snake follows the mouse. My snake body have to be a <code>polyline</code>. And my problem is that when I move the mouse too fast or too slow the body of my snake is getting longer/shorter and I know that is happening because of the fact that I am adding new point with the mouse coordinates and after that when I am connecting the lines the problem occurs.But I can't think of any smarter solutions. </p>
  
  <pre><code>public partial class MainWindow : Window
  {
      Point mousePos;
      Polyline polyline;
      public MainWindow()
      {
          InitializeComponent();
  
          polyline = new Polyline();
          polyline.Stroke = Brushes.Black;
          polyline.StrokeThickness = 4;
  
          var points = new PointCollection();
          for (int i = 0; i &lt; 50; i++)
          {
              points.Add(new Point(i, i));
          }
          polyline.Points = points;
          canvas.Children.Add(polyline);
  
      }
      protected override void OnMouseMove(MouseEventArgs e)
      {
          base.OnMouseMove(e);
          mousePos = e.GetPosition(canvas);
  
          polyline.Points.Add(mousePos);
  
          for (int i = 0; i &lt; polyline.Points.Count - 1; i++)
          {
              polyline.Points[i] = new Point(polyline.Points[i + 1].X, polyline.Points[i + 1].Y);
          }
  
          polyline.Points.RemoveAt(0);
  
      }
  }
  </code></pre>