PROGRAM PING_PONG;
USES CRT,GRAPH;
VAR
   X,Y,COLORPEL,COLORRAQ,MODOP,MODOR,
   DX,DY,XMAX,YMAX,XI,YI,XF,YF,R:INTEGER;
   TECLA,OPCION:CHAR;
   FIN:BOOLEAN;
   PX:INTEGER;

   PROCEDURE INIGRAF;
   VAR AG,MG:INTEGER;
   BEGIN
    AG:=DETECT;
    INITGRAPH(AG,MG,'C:\TP6\BGI');
   END;

   PROCEDURE FINGRAF;
   BEGIN
    CLOSEGRAPH;
   END;

   PROCEDURE PELOTA;
   BEGIN
    SETCOLOR(BLACK);
    SETBKCOLOR(BLACK);
    SETFILLSTYLE(MODOP,COLORPEL);
    FILLELLIPSE(X,Y,R,R);
   END;

   PROCEDURE MUEVEP(NX,NY:INTEGER);
   BEGIN
    SETCOLOR(BLACK);
    SETBKCOLOR(BLACK);
    SETFILLSTYLE(MODOP,BLACK);
    DELAY(400);
    FILLELLIPSE(X,Y,R,R);
    SETFILLSTYLE(MODOP,COLORPEL);
    X:=NX;
    Y:=NY;
    FILLELLIPSE(X,Y,R,R);
   END;

   PROCEDURE RAQUETA;
   BEGIN
    SETFILLSTYLE(MODOR,COLORRAQ);
    XF:=XI + 40;
    YF:=YI+5;
    BAR(XI,YI,XF,YF);
   END;

   PROCEDURE MUEVER(NX:INTEGER);
   BEGIN
    SETFILLSTYLE(MODOR,GETBKCOLOR);
    BAR(XI,YI,XF,YF);
    SETFILLSTYLE(MODOR,COLORRAQ);
    XI:=XI + NX;
    XF:=XI + 40;
    IF (XI > (XMAX-40)) THEN XI:=XMAX-40;
    IF XI<0 THEN XI:=0;
    BAR(XI,YI,XF,YF);
   END;

BEGIN
  INIGRAF;
  XMAX:=GETMAXX;
  YMAX:=GETMAXY;
  R:=5;
  COLORPEL:=YELLOW;
  COLORRAQ:=GREEN;
  MODOP:=1;
  MODOR:=1;
  RANDOMIZE;
  REPEAT
   CLEARDEVICE;
   X:=XMAX DIV 2;
   XI:=XMAX DIV 2-20;
   YI:=YMAX*3 DIV 4;
   Y:=YI-R-1;
   PELOTA;
   RAQUETA;
   REPEAT
     TECLA:=READKEY;
   UNTIL TECLA=#13;
   DY:=-1;
   IF RANDOM(2)=0 THEN
     DX:=1
   ELSE
     DX:=-1;
   FIN:=FALSE;
   REPEAT
     MUEVEP(X+DX,Y+DY);
     IF KEYPRESSED THEN
     BEGIN
       TECLA:=READKEY;
       IF TECLA=#0 THEN TECLA:=READKEY;
       CASE TECLA OF
         #77: BEGIN
             PX:=10;
            END;
         #75: BEGIN
             PX:=-10;
            END;
         #27:
             FIN:=TRUE;
       END;
       IF TECLA <> #27 THEN MUEVER(PX);
     END;
     IF X>(XMAX-5) THEN DX:=-1;
     IF X<5 THEN DX:=1;
     IF Y<5 THEN DY:=1;
     IF Y>YMAX THEN FIN:=TRUE;
     IF ( ((Y+6)=YI) AND ( (X>=XI) AND (X<= XF)) ) THEN  DY:=-1;
   UNTIL FIN=TRUE;
   SETCOLOR(WHITE);
   OUTTEXTXY(300,450,'OTRO JUEGO (S/N)?');
   OPCION:=READKEY;
  UNTIL ((OPCION='N') OR (OPCION='n'));
  FINGRAF;
END.