/***********************************************************

      PONG.C
      Z-World, 1999

      Demonstration of output to STDIO.
      Appearance will vary with different character sets.
	   For best results the font should be set to "Terminal" for
	   the Stdio window

************************************************************/
int	px,py;                        // Current Position     
int   dx,dy;                        // Current Direction    
int   nx,ny;                        // New Position         

int	xl, xh,	yl, yh;

void gotoxy (int x, int y);

main ()
{
   cls ();                         	// Clear Screen        
	xl =  1;									// box coordinates 		
	xh = 24;
	yl =  4;  
	yh = 11;
   px = xl + 1; py = yl +1;        	// Position Ball        
   dx = 2; dy = 2;                 	// Give Direction       
   gotoxy ( (xh-xl)/2+xl, yl-1 );  	// Position Cursor      
   printf ( "Pong" );              	// Title                
	box(xl, yl, xh - xl, yh - yl);
	pong();
}

/********* Clear Screen ******/
cls ()
{       
   printf ( " \x1Bt" );            	// Space Opens Window   
}

box(int x,int y, int w, int h)
{

	int i;
	char hor_line[100];
	
	//define horizontal border
	for( i = 0 ; i < w-1 ; i++)
	{

		hor_line[i] = 0xC4; 	
		
	}
	hor_line[i] = 0;

	//upper left corner
	gotoxy(x,y);

   printf ( "\xda" );

	//print top
	gotoxy(x + 1, y);
 	printf ( "%s", hor_line);

	//upper right corner
  	gotoxy(x + w, y);
   printf ( "\xbf" );

	//print sides
	for( i = 1 ; i < h ; i++)
	{
		gotoxy(x, y + i);
	   printf ( "\xB3" );
		gotoxy(x + w, y + i);
	   printf ( "\xB3" );
	}

	//lower left corner
	gotoxy(x,y + h);
	   printf ( "\xC0" );

	//bottom
	gotoxy(x + 1, y + h);
 	printf ( "%s", hor_line );

	//lower right corner
	gotoxy(x + w, y+h);
   printf ( "\xd9" );
}

nodebug pong(){

   while (1) 
   {
		hitwd();
      gotoxy ( px,py );    
      printf ( "\x01" );

      nx = px + dx;               	// Try New Position     
      ny = py + dy;

      if (nx <= xl || nx >= xh)     // Avoid Collision      
         dx = -dx;
      if (ny <= yl || ny >= yh)
         dy = -dy;

      nx = px + dx;               	// Next Position        
      ny = py + dy;

      gotoxy ( px,py );           
      printf ( " " );

      px = nx; py = ny;           	// Move Ball            
   }
}

/********* Position Cursor **********/
nodebug void gotoxy (int x, int y)
{
	char xl,xh,yl,yh;
   x += 0x20;
   y += 0x20;
   printf ("\x1B=%c%c",x,y );
}