Uses crt,dos,graph;
Const l=50;      { 0.5 ñòîpîíû pàêåòêè }
      per=5;      { ïåpåìåùåíèå çà åäèíèöó âpåìåíè }
      xk1=20;
      xk2=620;
      yk1=60;
      yk2=440;
      k=1;       { ê-ò îòêëîíåíèÿ }
      o=5;      { pàññòîÿíèå îò pàêåòêè íà êîò. åùå ìîæíî îòáèòü ìÿ÷ }
      vBegin=5;
      vMinX=1;
      vMinY=1;
      vmax=8;
      t=10;       { 0.5 ñòîpîíû ìÿ÷à }
      yl1=yk2;
      yl2=yk1;
Var n,i,gm,gd,xl1,xl2,x,y:integer;
    r:registers;
    s:string;
    c:char;
    vx,vy:real;
    a:array[0..9] of byte;

    Function f(a:real):real;
    Begin
      If a>=0 then F:=a else f:=-a;
    End;


    Procedure sss;
    Begin
      sound(5000);
{      delay(0);
 }     NoSound;
    End;

    Procedure vvv1;
    Begin
{       If vy>=0 then
         If vx>=0 then vy:=vx
                  else vy:=-vx;
       If vy<0 then
         If vx>=0 then vy:=-vx
                  else vy:=vx;
}
       If (vx>=vmax) then
         if vx>=0 then vx:=vmax
                  else vx:=-vmax;
       If (vx<=vMinX) then
         if vx>0 then vx:=vMinX
                  else vx:=-vMinX;
      sound(30);
      Delay(0);
      NoSound;
      If vx>0 then
       begin
         If x>=xl1 then vx:=vx+(x-xl1)/l*k;
         If x< xl1 then vx:=vx-(xl1-x)/l*k;
       End;
      If vx<=0 then
       begin
         If x>=xl1 then vx:=vx+(x-xl1)/l*k;
         If x< xl1 then vx:=vx-(xl1-x)/l*k;
       End;
    End;

    Procedure vvv2;
    Begin
{       If vy>=0 then
         If vx>=0 then vy:=vx
                  else vy:=-vx;
       If vy<0 then
         If vx>=0 then vy:=-vx
                  else vy:=vx;
}
       If (vx>=vmax) then
         if vx>=0 then vx:=vmax
                  else vx:=-vmax;
       If (vx<=vMinX) then
         if vx>0 then vx:=vMinX
                  else vx:=-vMinX;
      sound(30);
      Delay(0);
      NoSound;
      If vx>0 then
       begin
         If x>=xl2 then vx:=vx+(x-xl2)/l*k;
         If x< xl2 then vx:=vx-(xl2-x)/l*k;
       End;
      If vx<=0 then
       begin
         If x>=xl2 then vx:=vx+(x-xl2)/l*k;
         If x< xl2 then vx:=vx-(xl2-x)/l*k;
       End;
    End;


Procedure boll1;
const f=3;
Begin
    SetColor(0);
    Rectangle(x-t+f,y-t+f,x+t-f,y+t-f);

    x:=round(x+vx);
    y:=round(y+vy);

    If y+t+vy*1>yk2 then begin vy:=-vy; vvv1 end;
    If x+t+vx*1>xk2 then begin vx:=-vx; sss end;
    If y-t+vy*1<yk1 then begin vy:=-vy; vvv2 end;
    If x-t+vx*1<xk1 then begin vx:=-vx; sss end;

    { x1+60*n,y1+10,x1+60*(n+1),y+20 }
{    If (a[x-x1 div 60]=1)and(y+t+vy>y1+20) then begin vy:=-vy; sss end;
    If x+t+vx*1>x2 then begin vx:=-vx; sss end;
 }
    SetColor(14);
    Rectangle(x-t+f,y-t+f,x+t-f,y+t-f);
End;


Procedure Lin01;
const f=3;
var k,k1,x,x1:integer;
Begin
   SetColor(0);
   Line(xl1-l,yl1-f,xl1+l,yl1-f);
    k :=mem[$0000:$0417];
    k1:=mem[$0000:$0418];
    x:=k and 4;
    x1:=k1 and 1;
    if (x=4)and(x1=1) then xl1:=xl1-per;
    x:=k and 8;
    x1:=k1 and 2;
    if (x=8)and(x1=2) then xl1:=xl1+per;
   If xl1-l<xk1 then xl1:=xk1+l;
   If xl1+l>xk2 then xl1:=xk2-l;
   SetColor(14);
   Line(xl1-l,yl1-f,xl1+l,yl1-f);
End;


Procedure Lin02;
const f=3;
var x,x1,k,k1:integer;
Begin
   SetColor(0);
   Line(xl2-l,yl2-f,xl2+l,yl2-f);
    k :=mem[$0000:$0417];
    k1:=mem[$0000:$0418];
    x:=k and 4;
    x1:=k1 and 1;
    if (x=4)and(x1=0{1}) then xl2:=xl2-per;
    x:=k and 8;
    x1:=k1 and 2;
    if (x=8)and(x1=0{2}) then xl2:=xl2+per;
   If xl2-l<xk1 then xl2:=xk1-l;
   If xl2+l>xk2 then xl2:=xk2+l;
   SetColor(10);
   Line(xl2-l,yl2-f,xl2+l,yl2-f);
End;

Procedure Lin11;
const f=3;
var k,k1,x,x1:integer;
Begin
   SetColor(0);
   Line(xl1-l,yl1-f,xl1+l,yl1-f);
   r.ax:=3;
   Intr($33,r);
   xl1:=r.cx;
   If xl1-l<xk1 then xl1:=xk1+l;
   If xl1+l>xk2 then xl1:=xk2-l;
   SetColor(14);
   Line(xl1-l,yl1-f,xl1+l,yl1-f);
End;


Procedure Lin12;
const f=3;
var x,x1,k,k1:integer;
Begin
   SetColor(0);
   Line(xl2-l,yl2-f,xl2+l,yl2-f);
   r.ax:=3;
   Intr($33,r);
   xl2:=r.cx;
   If xl2-l<xk1 then xl2:=xk1+l;
   If xl2+l>xk2 then xl2:=xk2-l;
   SetColor(10);
   Line(xl2-l,yl2-f,xl2+l,yl2-f);
End;


Procedure Lin1;
Begin
   SetColor(0);
   Line(xl2-l,yl2,xl2+l,yl2);
   If x>xl2 then xl2:=xl2+per;
   If x<xl2 then xl2:=xl2-per;
   If xl2+l>xk2-5 then xl2:=xk2-l-5;
   If xl2-l<xk1+5 then xl2:=xk1+l+5;
   SetColor(10);
   Line(xl2-l,yl2,xl2+l,yl2);
End;


Procedure wr(n:integer);
Begin
  setcolor(15);
  Rectangle(xk1+60*n,yk1+10,xk1+60*(n+1),y+20);
End;


Begin
  gd:=9;
  gm:=2;
  InitGraph(gd,gm,'c:\tp\bgi');

  For i:=0 to 9 do a[i]:=1;
  SetLineStyle(0,0,3);
  n:=1;
  SetTextStyle(7,0,4);
  randomize;
  r.ax:=7;
  r.cx:=xk1+l+3;
  r.dx:=xk2-l-3;
  INTR($33,r);

  REPEAT

    randomize;
    vx:=vBegin;
    vy:=vBegin;
    xl1:=300;
    xl2:=300;
    x:=random(xk2-xk1-t*2)+xk1+2;
    y:=yk1+49;
    c:=#0;
    Str(n,s);
    SetColor(13);
    OutTextXY(220,(yk2-yk1) div 2 +yk1,'Ðàóíä '+s);
    r.ax:=3; Repeat Intr($33,r); Until (keypressed) or (r.bx=1);
    ClearDevice;
    SetColor(12);
    Rectangle(xk1-4,yk1-30,xk2+4,yk2+30);

     Repeat
       boll1;
       lin1;
       lin11;
{       y:=300;
 }      If keypressed then c:=readkey;
       If ((y+t>yk2-vmax)and((xl1-l>x+t+o)or(xl1+l<x-t-o))) then c:=#2;
       If ((y-t<yk1+vmax)and((xl2-l>x+t+o)or(xl2+l<x-t-o))) then c:=#2;
       delay(450);
     Until (c=#27)or(c=#2);

     n:=n+1;

  UNTIL  (c=#27)or(n=30);

    SetColor(13);
  If c=#2 then
   Begin
    OutTextXY(220,(yk2-yk1) div 2 +yk1,'Game Over');
    r.ax:=3;
    Repeat
      Intr($33,r);
    Until (keypressed) or (r.bx=1);
   End;
  CloseGraph;

End.