Delphi Object Oriented Snake Game using TImage : <p>I was bored, and I thought to myself I should do something productive, like try and deepen my knowledge with object oriented programming , so I set forth to create a small snake game using nothing more then the TImage Component . And I did this , for understanding what I did and maybe why I get the error the full code is on pastebin .</p>

<p>My problem manifests itself in that the Food if it is eaten by the snake sometimes (very rarely) the new food spawn on the snakes tail, which should not be possible ... </p>

<pre><code>  if (fPosX = Fruit.PosX) and (fPosY = Fruit.PosY) then
    begin
      Player.AddPart;

      inc(PlayerScore,10);

      // make sure the position is not inside a wall or the snake itself
      ValidPos:=false;

      while ValidPos = false do
        begin
          randomX:=random(Main.TileXCount);
          randomY:=random(Main.TileYCount);

          if Main.Level[randomY,RandomX] &lt;&gt; 1 then
            if (fPosX &lt;&gt; randomX) and (fPosY &lt;&gt; randomY) then
              begin
                for i := 0 to Length(fPlayerParts)-1 do
                  begin
                    if (fPlayerParts[i].X &lt;&gt; randomX) and (fPlayerParts[i].Y &lt;&gt; randomY) then
                      ValidPos:=true;
                  end;
              end;
        end;

      Fruit.PosX:=randomX;
      Fruit.PosY:=randomY;

    end;
</code></pre>

<p><a href="https://pastebin.com/ywt5M1ai" rel="nofollow noreferrer">https://pastebin.com/ywt5M1ai</a></p>

<p>I would be really grateful if someone would take a look at this, and tell me what I did wrong so that I might learn from my mistakes . I guess you can make a Snake Game a lot more simple, but I wanted to complicate it with a Class, Object , Array inside Object for the sake of trying out new waters.</p>

<p>Thank you very much for your kind help!</p>
