Simple snake game in DELPHI

Hello.

I want to make a snake game using DELPHI. The basic, the snake eats the apple and it grows longer. I have the apple generating in random places. The only thing that I cant get is the delay. So i need the blocks to follow the first block in a train effect, but so far they are all stuck together and when I change direction they all move together instead of being in a 'train' effect.

Can anyone help me? I don't know if the explanation was detailed enough.
Many Thanks
54999858925__AB9E1587-E48B-4EC4-B511.MOV
lT GEEKAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert GOracle dbaCommented:
it's all about the code, isn't it ?
lT GEEKAuthor Commented:
Geert G

Yes it is. I can’t seem so get it right.
Geert GOracle dbaCommented:
https://www.youtube.com/watch?v=iWxAURPFDuY
this guy at least posted a video with an explanation of how he did it ...
without showing your code, we can only comment on your video.

compare your code to the concepts in that video
Sinisa VukSoftware architectCommented:
Without looking how others do it... I will try show my thoughts...

So, try to use dynamic array - where you will store Snake (each element of an array will be one Snake segment).
Dyn-arrays can be safely expand...

type
  TSnakeSegment = record
    XPos, YPos: Integer;
   
  end;
  TSnake = array of TSnakeSegment;

Open in new window


following code will expand Snake - better do expanding from first position - because first segment (head) will have
always new position, remain segments will take prev. segment position :
procedure ExpandSnake(var Snake: TSnake; Segment: TSnakeSegment);
var
  i: Integer;
begin
  //shift
  SetLength(Snake, Length(Snake)+1);
  for i := (Low(Snake)+1) to High(Snake) do
     Snake[i] := Snake[i-1];
 //replace first...  
  Snake[0] := Segment;
end;

Open in new window


...when expanding is not in place (just moving) - do shifting without expanding Snake>
procedure MoveSnake(var Snake: TSnake; x,y: Integer);
var
  i: Integer;
begin
  //shift
  for i := (Low(Snake)+1) to High(Snake) do
     Snake[i] := Snake[i-1];
 //replace first position...  
  Snake[0].XPos := x;
  Snake[0].YPos := y;
end;

Open in new window


... this way you can have a "Train" effect - cause you will draw each segment in a sequential way.
Drawing - just iterate from first to last element of an array.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lT GEEKAuthor Commented:
I had to use a dynamic array which is something I haven’t learnt yet. Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.