V
V
Viktor2020-12-05 21:43:05
Pascal
Viktor, 2020-12-05 21:43:05

How to make a snake grow on Pascal with every apple eaten?

I tried to comment everything in detail, just tell me how to implement the growth of the snake, maybe you can change my code a little to make it work, I will be very
grateful

.
uses GraphABC, ABCObjects;
var i, k, xhead, yhead, z, appleX,appleY, longintSnake, PreviousStep: integer;
apple, head: CircleABC;

snake: array[1..50] of CircleABC;//an array of snake links
score, GameOver:TextABC;//Score and GameOver labels

procedure KeyDown(key: integer);
begin
//determining the direction of movement
if(key=vk_Right) then z:=1; xhead:=xhead+40;
if(key=vk_Left) then z:=2; xhead:=xhead-40;
if(key=vk_Up) then z:=3; yhead:=yhead-40;
if(key=vk_Down) then z:=4; yhead:=yhead+40;
end;

start
randomize;
//create cells to visualize the work of the snake
for i:=0 to 12 do
line(0, 40*i, windowWidth, 40*i);
for i:=0 to 16 do
line(40*i, 0, 40*i, windowHeight);

//coordinates of the snake's head and its creation
xhead:=3*40-20;
yhead:=2*40-20;
head := CircleABC.Create(xhead, yhead, 20, clPurple);

for i:=1 to 4 do begin
Snake[i] := CircleABC.Create(xhead, yhead+40*i, 20, clpurple)
end;

appleX:=6*40-20;
appleY:=3*40-20;

apple:=circleABC.Create(appleX, appleY, 20, clRed);

//Game
score Score:=TextABC.Create(0,0,20,'0', clGreen);

{longintSnake:=4;
PreviousStep:=4;}


//game loop, runs every 150 milliseconds
while (true) do begin
onKeyDown:=KeyDown;
// move all links from last to first
if (z<>0) then begin
for k:=4 downto 2 do begin
Snake[k].MoveTo(snake[k-1].position.X, snake[k- 1].Position.Y);
end;
snake[1].MoveTo(xhead-20, yhead-20);
end;

//determining the direction of the snake
if(z=1) then xhead:=xhead+40
else if(z=2) then xhead:=xhead-40
else if(z=3) then yhead:=yhead-40
else if(z=4) then yhead:=yhead+40;

//create a new snake link
{if longintSnake<>PreviousStep then begin
Snake[longintSnake] := CircleABC.Create(snake[longintSnake+1].Position.X,snake[longintSnake+1].Position.Y, 20, clpurple)
end;
PreviousStep:=longintSnake;}

// game over if these conditions
are met if xhead>windowWidth then begin GameOver:=TextABC.Create(80, 200, 60, 'GAME OVER' ,clBlack); exit; end;
if xhead<0 then begin GameOver:=TextABC.Create(80, 200, 60, 'GAME OVER' ,clBlack); exit; end;
if yhead>windowHeight then begin GameOver:=TextABC.Create(80, 200, 60, 'GAME OVER' ,clBlack); exit; end;
if yhead<0 then begin GameOver:=TextABC.Create(80, 200, 60, 'GAME OVER' ,clBlack); exit; end;

//apple loop(spawn and score)
if ((xhead=applex) and(yhead = appley)) then begin
applex:=random(1, 16)*40-20;
appley:=random(1, 12)*40-20;
apple.MoveTo(appleX-20, appleY-20);
score.Text:=((score.Text).ToInteger + 1).ToString();
longintSnake:=longintSnake + 1;
end;

//move the snake's head
head.MoveTo(xhead-20, yhead-20);
sleep(150);
end;


end.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2020-12-05
@dimonchik2013

Well, is there a fact of eating an apple? actually change the coordinates in the direction of growth

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question