Pascal полноэкранная программа

Рейтинг: 0Ответов: 1Опубликовано: 01.02.2023

Есть код программы, которая создаёт арену из символов "*", по границам которой бежит символ "#", до тех пор, пока не нажмём какую-либо клавишу. Как только нажимаем - программа закрывается.

program clrsrceen;
uses Crt, SysUtils;

const
  Width = 12;
  Height = 12;
  Delay = 100;
  GridWidth = 10;
  GridHeight = 10;
  GridBack = '*';
  GridRunner = '#';

procedure DrawGrid(x, y: Integer);
var
  i, j: Integer;
begin
  for i := 0 to GridHeight - 1 do begin
    GotoXY(x, y + i);
    for j := 0 to GridWidth - 1 do
      Write(GridBack);
  end;
end;

procedure MoveGrid(x, y: Integer);

  procedure Animate;
  var
    i: Integer;
  begin
    for i := 0 to GridWidth - 1 do begin
      GotoXY(x + i, y);
      Write(GridRunner);
      GotoXY(x + i, y);
      Sleep(Delay);
      Write(GridBack);
      if KeyPressed then Exit;
    end;
    for i := 0 to GridHeight - 1 do begin
      GotoXY(x + GridWidth - 1, y + i);
      Write(GridRunner);
      GotoXY(x + GridWidth - 1, y + i);
      Sleep(Delay);
      Write(GridBack);
      if KeyPressed then Exit;
    end;
    for i := GridWidth - 1 downto 0 do begin
      GotoXY(x + i, y + GridHeight - 1);
      Write(GridRunner);
      GotoXY(x + i, y + GridHeight - 1);
      Sleep(Delay);
      Write(GridBack);
      if KeyPressed then Exit;
    end;
    for i := GridHeight - 1 downto 0 do begin
      GotoXY(x, y + i);
      Write(GridRunner);
      GotoXY(x, y + i);
      Sleep(Delay);
      Write(GridBack);
      if KeyPressed then Exit;
    end;
  end;

begin
  while not KeyPressed do begin
    GotoXY(1, 1);
    Animate;
  end;
  if ReadKey = #0 then ReadKey;
end;

var
  x, y: Integer;
begin
  ClrScr;
  x := (ScreenWidth - GridWidth) div 2;
  y := (ScreenHeight - GridHeight) div 2;
  DrawGrid(x, y);
  MoveGrid(x, y);
  ClrScr;
end.

Я хочу изменить код так, чтобы выход из программы делался только нажатием esc. Попробовал прописать в каждом цикле Animate процедуры так

for i := 0 to GridHeight - 1 do begin
  GotoXY(x + i, y);
  Write(GridRunner);
  GotoXY(x + i, y);
  Sleep(Delay);
  Write(GridBack);
  if KeyPressed and (ReadKey = #27) then exit; // change break to exit
end;

и в вызове вот так

begin
  while not KeyPressed do begin 
    Animate;
  end;
  if ReadKey = #27 then // check for Esc key
  Halt;
end;

Но не вышло. Если совсем убрать проверку из циклов тоже не получается. Подскажите, как сделать правильно?

Функция

function IsEscPressed: Boolean;
var
  Key: Char;
begin
  if KeyPressed then
  begin
    Key := ReadKey;
    if Key = #27 then
      exit(True);
  end;
  exit(False);
end

Ответы

▲ 0Принят

Прошу прощения, что слегка ввёл в заблуждение. Давно не связывался с Crt.

var
  EscPressed: Boolean;

function IsEscPressed(): Boolean;
begin
  if ( not EscPressed ) then
    EscPressed := KeyPressed() and ( ReadKey() = #27 );

  Result := EscPressed;
end;

В начале программы на всякий случай можно прописать EscPressed := False; Также можно цикл while заменить на repeat. Тогда что-то успеет нарисоваться.

repeat
  GotoXY(1, 1);
  Animate;
until IsEscPressed();