@Nterwy
Почему на аватарке я робот ??

Как сделать так, чтобы с каждым съеденым яблоком змейка росла на Паскале?

Постарался все подробно закомментировать, просто скажите, как реализовать рост змейки, возможно вы сможете немного переправить мой код, чтобы это работало, буду очень благодарен
Если лень читать, то хотя бы объясните как это должно работать, я просто совсем ничего не придумаю

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

snake: array[1..50] of CircleABC;//массив звеньев змейки
score, GameOver:TextABC;//надписи Счета и ГеймОвера

procedure KeyDown(key: integer);
begin
//определение направления движения
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;

begin
randomize;
//создание клеток для визуализации работы змейки
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);

//координаты головы змейки и ее создание
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);

//Счет в игре
Score:=TextABC.Create(0,0,20,'0', clGreen);

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


//игровой цикл, действует через каждые 150 милисекунд
while (true) do begin
onKeyDown:=KeyDown;
//движение всех звеньев начиная с последнего до первого
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;

//определение направления змейки
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;

//создание нового звена змейки
{if longintSnake<>PreviousStep then begin
Snake[longintSnake] := CircleABC.Create(snake[longintSnake+1].Position.X,snake[longintSnake+1].Position.Y, 20, clpurple)
end;
PreviousStep:=longintSnake;}

//конец игры в случае достижения этих условий
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;

//цикл яблока(появление и счет очков)
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;

//движение головы змейки
head.MoveTo(xhead-20, yhead-20);
sleep(150);
end;


end.
  • Вопрос задан
  • 317 просмотров
Пригласить эксперта
Ответы на вопрос 2
dimonchik2013
@dimonchik2013
non progredi est regredi
ну есть же факт съдания яблока? по факту меняйте координаты в сторону прироста
Ответ написан
@SergeiBSK
Можно вот так:

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

snake: array[1..50] of CircleABC;//массив звеньев змейки
score, GameOver:TextABC;//надписи Счета и ГеймОвера

procedure KeyDown(key: integer);
begin
//определение направления движения
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;

begin
randomize;
longintSnake:=2;
//создание клеток для визуализации работы змейки
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);

//координаты головы змейки и ее создание
xhead:=3*40-20;
yhead:=2*40-20;
head := CircleABC.Create(xhead, yhead, 20, clPurple);

for i:=1 to longintSnake 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);

//Счет в игре
Score:=TextABC.Create(0,0,20,'0', clGreen);

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

//игровой цикл, действует через каждые 150 милисекунд
while (true) do begin
onKeyDown:=KeyDown;
//движение всех звеньев начиная с последнего до первого
if (z<>0) then begin
for k:=longintSnake 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;

//определение направления змейки
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;

//конец игры в случае достижения этих условий
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;

//цикл яблока(появление и счет очков)
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;
Snake[longintSnake] := CircleABC.Create(xhead, yhead+40*i, 20, clpurple)
end;

//движение головы змейки
head.MoveTo(xhead-20, yhead-20);
sleep(150);
end;

end.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы