//PINGPNG.CPP
#include <iostream.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
#include <stdlib.h>

int dx,dy,px;
int xmax,ymax;
int fin;

class objeto {
 protected:
  int x;
  int y;
  int color;
 public:
  void asigna(int nx,int ny,int clr);
};

class pelota: public objeto {
 protected:
  int radio;
 public:
  void leerad(int rad);
  void dibuja(void);
  void movpel(void);
};

class raqueta: public objeto {
 protected:
  int tamx;
  int tamy;
 public:
  void leeraq(int tx,int ty);
  void dibuja(void);
  void movraq(void);
};

void objeto::asigna(int nx,int ny,int clr)
{
 x=nx;
 y=ny;
 color=clr;
}

void pelota::leerad(int rad)
{
 radio=rad;
}

void pelota::dibuja(void)
{
 setbkcolor(BLACK);
 setcolor(BLACK);
 setfillstyle(1,color);
 fillellipse(x,y,radio,radio);
}

void pelota::movpel(void)
{
 setcolor(BLACK);
 setcolor(BLACK);
 setfillstyle(1,getbkcolor());
 delay(5);
 fillellipse(x,y,radio,radio);
 x=x+dx;
 y=y+dy;
 setfillstyle(1,color);
 fillellipse(x,y,radio,radio);
 if(x<5) dx=1;
 if(x>(xmax-5)) dx=-1;
 if(y<5) dy=1;
 if(y>ymax) fin=1;
 if( (y>=(ymax*3/4-6)) && (getpixel(x,y+6)==GREEN) ) dy=-1;
}

void raqueta::leeraq(int tx,int ty)
{
 tamx=tx;
 tamy=ty;
}

void raqueta::dibuja(void)
{
 setbkcolor(BLACK);
 setcolor(BLACK);
 setfillstyle(1,color);
 bar(x,y,x+tamx,y+tamy);
}

void raqueta::movraq(void)
{
 setbkcolor(BLACK);
 setcolor(BLACK);
 setfillstyle(1,getbkcolor());
 bar(x,y,x+tamx,y+tamy);
 x=x+px;
 if(x>(xmax-40)) x=xmax-40;
 if(x<0) x=0;
 setfillstyle(1,color);
 bar(x,y,x+tamx,y+tamy);
}

void inigraf(void)
{
 int ag=DETECT,mg;
 initgraph(&ag,&mg,"c:\\borlandc\\bgi");
}

void fingraf(void)
{
 closegraph();
}

void main()
{
 char opcion,tecla;
 pelota pel;
 raqueta raq;
 inigraf();
 xmax=getmaxx();
 ymax=getmaxy();
 pel.leerad(5);
 raq.leeraq(40,5);
 randomize();
 do {
  cleardevice();
  pel.asigna(xmax/2,ymax*3/4-6,YELLOW);
  raq.asigna(xmax/2-20,ymax*3/4,GREEN);
  pel.dibuja();
  raq.dibuja();
  do {
   tecla=getch();
  }while(tecla!=13);
  dy=-1;
  if(random(2)==0) dx=1; else dx=-1;
  fin=0;
  do {
   pel.movpel();
   if(kbhit())
   {
    tecla=getch();
    if(tecla==0) tecla=getch();
    switch(tecla)
    {
     case 77:
      px=10;
      break;
     case 75:
      px=-10;
      break;
     case 27:
      fin=1;
      break;
    }
    if(tecla!=27) raq.movraq();
   }
  }while(fin!=1);
  setcolor(WHITE);
  outtextxy(300,440,"Otro juego (s/n) ?");
  opcion=getch();
 }while((opcion!='n') && (opcion!='N'));
 fingraf();
}