Форум «Всё о Паскале» _ Задачи _ Контроль за объектами
Автор: Sanekkk1 10.01.2015 19:37
Здравствуйте, помогите исправить программу. Сейчас программа выводит точки, но они не управляются клавишами (вверх, вниз, вправо, влево), при нажатии на кнопки они просто рандомно переставляются. Как сделать чтобы их движении соответствовало нажатой кнопке.
Uses Crt,Graph; const Path_of_driver='F:\BGI';
Type TPoint = object x,y:integer; color:word; procedure Init(InitX,InitY:integer;col:word); procedure Hide; procedure Draw; procedure SetX(NewX:integer); procedure SetY(NewY:integer); function GetX:integer; function GetY:integer; procedure Move(NewX,NewY:integer); end;
Procedure TPoint.Init(InitX,InitY:integer; col:word); begin x:=InitX; y:=InitY; color:=col; end;
Procedure TPoint.Hide; begin PutPixel(x,y,GetBkColor); end;
Procedure TPoint.Draw; begin PutPixel(x,y,color); end;
Function TPoint.GetX:integer; begin GetX:=x; end;
Function TPoint.GetY:integer; begin GetY:=y; end;
Procedure TPoint.SetX(NewX:integer); begin x:=NewX; end;
Procedure TPoint.SetY(NewY:integer); begin y:=NewY; end;
procedure TPoint.Move(NewX,NewY:integer); begin Hide; x:=NewX; y:=NewY; Draw; end;
Procedure Coords(a:char; var NewX,NewY:integer; h:integer); begin if a=#0 then begin a:=readkey; case a of #80: begin Inc(NewY,h); if NewY>GetMaxY-20 then NewY:=20; end; #75: begin dec(NewX,h); if NewX<10 then NewX:=GetMaxX-10; end; #77: begin inc(NewX,h); if NewX>GetMaxX-10 then NewX:=10; end; #72: begin dec(NewY,h); if NewY<20 then NewY:=GetMaxY-20; end; end; end end; VAR gd,gm,NewX,NewY,h,x,y,vh,i: integer; color: word; Q,a:char; str:string; tp:TPoint; mas:array[1..100] of TPoint; BEGIN Randomize; gd:=Detect; InitGraph(gd,gm,Path_of_driver); if GraphResult<>grok then begin write('error:',GraphErrorMsg(GraphResult)); halt; end;
tp.Init(GetMaxX div 2,GetMaxY div 2,10);
NewX:=GetMaxX div 2; NewY:=GetMaxY div 2; h:=2; RestoreCrtMode; Q:='y'; While Q<>'n' do begin writeln('Chtoby dvigat tochki vvedite: 1 '); writeln('Chtoby tochki dvigalis sami vvedite: 2 '); readln(str); if str='1' then begin writeln('Chtoby dvigat tochki ispolzujte strelki'); end; writeln('Nazhmite: Enter'); readln; SetGraphMode(gm); repeat a:=readkey; Coords(a,NewX,NewY,h); if str='1' then begin for vh:=1 to 100 do begin tp.SetX(Random(640)); tp.SetY(Random(480)); mas[vh].Hide; mas[vh].Init(tp.GetX,tp.GetY,10); mas[vh].Draw; mas[vh].Move(tp.GetX,tp.GetY); end; end;
procedure TPoint.Init(InitX, InitY: integer; col: word); begin x := InitX; y := InitY; color := col; end;
procedure TPoint.Hide; begin PutPixel(x, y, GetBkColor); // SetColor(GetBkColor); // Circle(x, y, 10); end;
procedure TPoint.Draw; begin PutPixel(x, y, color); // SetColor(color); // Circle(x, y, 10);
end;
function TPoint.GetX: integer; begin GetX := x; end;
function TPoint.GetY: integer; begin GetY := y; end;
procedure TPoint.SetX(NewX: integer); begin x := NewX; end;
procedure TPoint.SetY(NewY: integer); begin y := NewY; end;
procedure TPoint.Move(NewX, NewY: integer); begin Hide; x := NewX; y := NewY; Draw; end;
procedure TPoint.RMove(NewX, NewY: integer); begin Self.move(x + NewX, y + NewY); end;
procedure Coords(a: char; var NewX, NewY: integer; h: integer); begin if a = #0 then begin a := readkey; case a of #80: begin Inc(NewY, h); if NewY > GetMaxY - 20 then NewY := 20; end; #75: begin Dec(NewX, h); if NewX < 10 then NewX := GetMaxX - 10; end; #77: begin Inc(NewX, h); if NewX > GetMaxX - 10 then NewX := 10; end; #72: begin Dec(NewY, h); if NewY < 20 then NewY := GetMaxY - 20; end; end; end; end;
procedure dCoords(a: char; var dX, dY: integer; h: integer); begin dX := 0; dY := 0; if a = #0 then begin a := readkey; case a of #80: begin dY := h; end; #75: begin dX := -h; end; #77: begin dX := h; end; #72: begin dY := -h; end; end; end; end;
var gd, gm, NewX, NewY, h, i: integer; a: char; mas: array[1..100] of TPoint; grError: integer; begin Randomize; // gd := Detect; gd := VGA; gm := VGAHi; InitGraph(gd, gm, Path_of_driver); grError := GraphResult; if grError <> grok then begin Write('error:', GraphErrorMsg(grError)); halt; end;
NewX := GetMaxX div 2; NewY := GetMaxY div 2; h := 2;
for i := low(mas) to high(mas) do begin mas[i].Init(Random(GetMaxX) + 1, Random(GetMaxY) + 1, random(5) + 1); mas[i].Draw; end;
repeat repeat until KeyPressed; a := readkey; dCoords(a, NewX, NewY, h); for i := low(mas) to high(mas) do mas[i].RMove(NewX, NewY); until a = #27;
closegraph; end.
У меня нет TurboPascal - только FreePascal (FPC) - почти полный эквивалент в режиме совместимости. Поэтому, что-то я изменил "под себя" (CRT на WinCRT, что-то ещё) - надеюсь разберёшься. Что я изменил по делу: 1. Цикл обработки клавиатуры и прорисовки. 2. В объект добавил RMove - относительное перемещение. 3. Убрал переменную tp. 4. В цикле проинициализировал каждый экземпляр объекта. 5. При нажатии на клавишу все экземпляры сдвигаются на h.
Нужно добавить освобождение объекта - пусть даже и пустое.
Не переключайся во время работы между режимами (текст/графика) - там есть особенности (=чудеса, баги, фичи) - рассказывать долго, да и не нужно. Работай в графике (OutText). Что будет непонятно - спрашивай.