Помощь - Поиск - Пользователи - Календарь
Полная версия: Змейка
Форум «Всё о Паскале» > Pascal, Object Pascal > Задачи
Bokul
Проверьте пожалуйста код, вроде должно быть
9 звено у змейки, а выходит только 5.
Где ошибка unsure.gif

Код

uses crt;
const xmax=8;
      ymax=8;
var ch:char;
    i,j,first,last:byte;
    n:integer;
    x:array[0..xmax] of byte;
    y:array[0..ymax] of byte;
    begin
clrscr;
i:=1;
j:=1;
n:=0;
while ch<>#27 do
begin
inc(n);
gotoxy(i,j);
write('*');
ch:=readkey;
first:=n mod (xmax+1);
if first<xmax then last:=first+1
else last:=0;
    case first of
  0: begin x[0]:=i; y[0]:=j; end;
  1: begin x[1]:=i; y[1]:=j; end;
  2: begin x[2]:=i; y[2]:=j; end;
  3: begin x[3]:=i; y[3]:=j; end;
  4: begin x[4]:=i; y[4]:=j; end;
  5: begin x[5]:=i; y[5]:=j; end;
  6: begin x[6]:=i; y[6]:=j; end;
  7: begin x[7]:=i; y[7]:=j; end;
  8: begin x[8]:=i; y[8]:=j; end;
  end;  
  if n>=(xmax+1) then
    begin
    gotoxy(x[last],y[last]);
    write(' ');
    end;  
  if (ch=#77) and (i<80) then  inc(i); {right}
  if (ch=#75) and (i>1)  then  dec(i); {left}
  if (ch=#80) and (j<25) then  inc(j); {up}
  if (ch=#72) and (j>1)  then  dec(j); {down}
end;
end.
volvo
uses crt;
const xmax=8;
      ymax=8;
var ch:char;
    i,j,first,last:byte;
    n:integer;
    x:array[0..xmax] of byte;
    y:array[0..ymax] of byte;
begin
  clrscr;
  i:=1; j:=1;

  n:=0;
  while ch<>#27 do begin
    inc(n);
    gotoxy(i, j); write('*');
    ch := readkey;
  
    first := n mod (xmax+1);
    if first < xmax then last:=first + 1
    else last := 0;
  
    x[first] := i; y[first] := j; { А это - просто, чтобы меньше набирать... }
  
    if n>=(xmax+1) then begin
      gotoxy(x[last],y[last]); write(' ');
    end;
  
    if ch = #0 then begin { <--- Вот тут была ошибка !!! }
      ch := readkey;
      if (ch=#77) and (i<80) then  inc(i); {right}
      if (ch=#75) and (i>1)  then  dec(i); {left}
      if (ch=#80) and (j<25) then  inc(j); {up}
      if (ch=#72) and (j>1)  then  dec(j); {down}
    end;
  end;
end.
мисс_граффити
все змеек пишут...
вот у меня тоже такое задание (курсовик).
правда, в Делфи.

а в этой программке смутило:
Код
if (ch=#77) and (i<80) then  inc(i); {right}
           if (ch=#75) and (i>1)  then  dec(i); {left}

то есть змейка сможет сразу поменять направление на 180 градусов?
Bokul
Спасибо Volvo. !thanks.gif
Только один вопрос - чтo такое? unsure.gif
Цитата

ch = #0
volvo
Символ с нулевым кодом... Дело-то все в том, что клавиши управления курсором, ну и некоторые другие, возвращают не обычный код, а расширенный: первый символ - нулевой (#0), а второй - именно то, что ты написал...

Вот и ловить нажатие этих клавиш нужно также (сначала - проверка на нулевой символ, а потом - анализ следующего за ним обычного)
Это текстовая версия — только основной контент. Для просмотра полной версии этой страницы, пожалуйста, нажмите сюда.