Uses Crt, Graph;
Type Location=object
x,y:integer;
procedure Init(x1,y1:integer);
function Getx:integer;
function Gety:integer;
end;
Square=object(Location)
A:integer;
procedure Init(x1,y1,A1:integer);
function GetA:integer;
procedure Show;
procedure Hide;
procedure Shift(x1,y1:integer);
end;
Procedure Location.Init(x1,y1:integer);
Begin
x:=x1; y:=y1;
end;
Function Location.Getx:integer;
Begin
Getx:=x;
end;
Function Location.Gety:integer;
Begin
Gety:=y;
end;
Procedure Square.Init(x1,y1,A1:integer);
Begin
Location.Init(x1,y1);
A:=A1;
end;
Function Square.GetA:integer;
Begin
GetA:=A;
end;
Procedure Square.Show;
Begin
Rectangle(x- A div 2,y- A div 2,x+ A div 2,y + A div 2);
end;
Procedure Square.Hide;
Var color:word;
Begin
color:=GetColor;
SetColor(GetBkColor);
Rectangle(x- A div 2,y- A div 2,x+ A div 2,y + A div 2);
SetColor(color);
end;
Procedure Square.Shift(x1,y1:integer);
Begin
Hide;
Init(x1,y1,A);
Show;
end;
Var Gd, Gm : Integer;
A,h : integer;
mySquare:Square;
x,y:integer;
c:char;
Begin
h:=100;
Gd:=Detect;
InitGraph(Gd, Gm, '');
Randomize;
A:=50;
x:=Random(640-A)+(A div 2);
y:=480-h-(A div 2)-1;
SetColor(4);
mySquare.Init(x,y,A);
mySquare.Show;
Repeat
if KeyPressed then begin
c:=ReadKey;
if c=#75 then begin x:=x-3; mySquare.Shift(x,y); end;
if c=#77 then begin x:=x+3; mySquare.Shift(x,y); end;
if c=#72 then begin y:=y-3; mySquare.Shift(x,y); end;
if c=#80 then begin y:=y+3; mySquare.Shift(x,y); end;
end;
if (x-(A div 2)<0)or(x+(A div 2)>640)or(y-(A div 2)<0)or(y+(A div 2)>480) then
Begin
SetColor(Random(14)+1);
Sound(400);
Delay(100);
NoSound;
end else SetColor(4);
Until c=#32;
CloseGraph;
End.