#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void dot();

int left=150,right=550,top=50,bottom=300;
int x2,y2,i,j,k,x1,y1,sp;
int spd[6]={0,50,40,30,20,10};
int bats[30],bat,score=0;
char dir;

void main()
{
 clrscr();
 int gd,gm;
 detectgraph(&gd,&gm);
 initgraph(&gd,&gm,"C:\\TC\\bgi");

 cout<<"\n\n\n\n\n          Instructions for playing the game :";
 cout<<"\n\n           Press 'i' to move stick up";
 cout<<"\n           Press 'k' to move stick down";
 cout<<"\n           Press 'x' to quit";
 cout<<"\n\n\n           Press Enter when you are ready to play";
 getch();

 clrscr();
 cout<<"               Enter Speed(1 2 3 4 5) = ";
 cin>>sp;
 x1=left+5;
 y1=240;
 x2=0;
 y2=242;
 i=1;
 j=1;
 for(k=0;k<30;k++)
  bats[k]=200+k;
 dot();
}

void dot()
{
 clrscr();

 setcolor(1);
 line(left-2,top-2,left-2,bottom+2);
 line(right+2,top-2,right+2,bottom+2);
 line(left-2,top-2,right+2,top-2);
 line(left-2,bottom+2,right+2,bottom+2);

 for(k=0;k<30;k++)
 {
  putpixel(left+1,bats[k],3);
  putpixel(left+2,bats[k],3);
  putpixel(right-1,bats[k],3);
  putpixel(right-2,bats[k],3);
 }//for

 while(1)
 {
  delay(spd[sp]);

  putpixel(x2-1,y2-1,7);
  putpixel(x2-1,y2,7);
  putpixel(x2-1,y2+1,7);
  putpixel(x2,y2-1,7);
  putpixel(x2,y2,7);
  putpixel(x2,y2+1,7);
  putpixel(x2+1,y2-1,7);
  putpixel(x2+1,y2,7);
  putpixel(x2+1,y2+1,7);

  putpixel(x1-1,y1-1,10);
  putpixel(x1-1,y1,0);
  putpixel(x1-1,y1+1,0);
  putpixel(x1,y1-1,0);
  putpixel(x1,y1,0);
  putpixel(x1,y1+1,0);
  putpixel(x1+1,y1-1,0);
  putpixel(x1+1,y1,0);
  putpixel(x1+1,y1+1,0);

  x2=x1;
  y2=y1;
  x1=x1+i;
  y1=y1+j;

  bat=getpixel(x1-2,y1);
  if(bat==4) { i=1; score=score+sp; }
  bat=getpixel(x1+2,y1);
  if(bat==4) { i=-1; score=score+sp; }

  if(x1-1==left || x1+1==right)
  {
   cout<<"\n\n\n               Game Over";
   cout<<"\n\n\n\n\n\n             Your Score = "<<score;
   delay(2500);
   break;
  }

  if(y1==bottom) j=-1;
  else if(y1==top) j=1;

  if(kbhit())
  {
   dir=getch();

   if(dir=='x')
   {
    cout<<"\n\n\n\n           Your Score = "<<score;
    break;
   }

   if(dir=='i' && bats[0]>top)
   {
    for(k=0;k<30;k++)
     bats[k]=bats[k]-1;
    putpixel(left+1,bats[29]+1,7);
    putpixel(left+2,bats[29]+1,7);
    putpixel(right-1,bats[29]+1,7);
    putpixel(right-2,bats[29]+1,7);
    for(k=0;k<30;k++)
    {
     putpixel(left+1,bats[k],4);
     putpixel(left+2,bats[k],4);
     putpixel(right-1,bats[k],4);
     putpixel(right-2,bats[k],4);
    }//for
   }//if

    if(dir=='k' && bats[29]<bottom)
   {
    for(k=0;k<30;k++)
     bats[k]=bats[k]+1;
    putpixel(left+1,bats[0]-1,7);
    putpixel(left+2,bats[0]-1,7);
    putpixel(right-1,bats[0]-1,7);
    putpixel(right-2,bats[0]-1,7);
    for(k=0;k<30;k++)
    {
     putpixel(left+1,bats[k],4);
     putpixel(left+2,bats[k],4);
     putpixel(right-1,bats[k],4);
     putpixel(right-2,bats[k],4);
    }//for
   }//if


  }//if
 }//while
}