program pingpong;
uses crt,graph,mouse;
 var u_bat_x,u_bat_y,c_bat_x,c_bat_y : integer;
     o_u_bat_x,o_c_bat_x : integer;
     o_ball_x,o_ball_y : integer;
     ball_x,ball_y : integer;
     leven,punten : integer;
     mx,my,mknop : integer;
     gd,gm : integer;
     ch    : char;
     r_x,r_y : integer;
     uit,hit     : boolean;
     tel : byte;
     ball_to_c : boolean;
     h_ball_x,h_ball_y:integer; { uitrekenen waar bal terecht komt }
     h_r_x,h_r_y : integer;
     o_schuin,sch,schuin : integer;



 procedure initscreen;
  begin
   gd:=detect;
   initgraph(gd,gm,'');
 end;


 procedure setvars;
  begin
   schuin:=0;
   o_schuin:=0;
   ball_to_c:=false;
   tel:=0;
   setmouse(300,450);
   ball_x:=200;
   ball_y:=300;
   r_x:=1;
   r_y:=1;
   punten:=0;
   leven:=3;
   uit:=false;
   c_bat_x:=300;
   c_bat_y:=0;
   o_c_bat_x:=c_bat_x;
   setfillstyle(1,green);
    bar(c_bat_x,0,c_bat_x+60,10);

  end;

 procedure updatevars;
  begin
   o_u_bat_x:=mx;
   getmouse(mx,my,mknop);
   if ball_y>450 then uit:=True
     else uit:=false;
   if r_y=1 then ball_to_c:=False;

   if keypressed then ch:=readkey;
   o_schuin:=schuin;
   if mknop=1 then inc(schuin,1);
   if mknop=2 then dec(schuin,1);
   if mknop=3 then schuin:=0;


  end;


 procedure af;
  begin
   sound(40);delay(100);nosound;
   ball_y:=random(300);
   ball_x:=random(600);
  end;

 procedure update_u_bat;
   var t: byte;
  begin
    setcolor(red);
    if schuin>0 then
     for t:=0 to 10 do line(mx,450+t,mx+60,450+t+schuin);

    if schuin<=0 then begin
                        sch:=-(schuin);
                        for t:=0 to 10 do line(mx,450+t+sch,mx+60,450+t);
                       end;

   if (o_u_bat_x<>mx) then
                        begin
                         setcolor(black);
                          if schuin>=0 then
                            for t:=0 to 10 do line(o_u_bat_x,450+t,o_u_bat_x+60,450+t+schuin);
                          if schuin<0 then
                            for t:=0 to 10 do line(o_u_bat_x,450+t+sch,o_u_bat_x+60,450+t);
                        end;
   if (o_schuin<>schuin) then
                          begin
                           delay(2);
                           setcolor(black);
                           if schuin>=0 then
                             for t:=0 to 10 do line(o_u_bat_x,450+t,o_u_bat_x+60,450+t+o_schuin);
                           if schuin<=0 then
                             for t:=0 to 10 do line(o_u_bat_x,450+t-o_schuin,o_u_bat_x+60,450+t);
                          end;


  end;

 procedure update_c_bat;
  var t : integer;
  begin
   if (ball_to_c=true)and(ball_y>8) then
    begin
     h_ball_x:=ball_x;
     h_ball_y:=ball_y;
     h_r_x:=r_x;
     h_r_y:=r_y;

     repeat
      if h_ball_x>630 then h_r_x:=-1;
      if h_ball_x<10 then h_r_x:=1;

      inc(h_ball_x,h_r_x);
      inc(h_ball_y,h_r_y);
     until h_ball_y=0;

     setcolor(blue);
     circle(h_ball_x,h_ball_y,10);

     o_c_bat_x:=c_bat_x;

     if h_ball_x-20 < c_bat_x then dec(c_bat_x);
     if h_ball_x-20 > c_bat_x then inc(c_bat_x);
{     if h_ball_y < c_bat_y then dec(c_bat_y);
     if h_ball_y > c_bat_y then inc(c_bat_y);}

     setfillstyle(1,green);
     bar(c_bat_x,0,c_bat_x+60,10);
     delay(1);
     if o_c_bat_x<>c_bat_x then
                          begin
                           setfillstyle(1,black);
                           bar(o_c_bat_x,0,o_c_bat_x+60,10);
                          end;

    end
   else;
  end;

 procedure score;
  begin
   if hit then punten:=punten+100;
   if not hit then punten:=punten-100;
  end;

 procedure levens;
  begin
   if hit then
          begin
           inc(tel);
           if tel<> 10 then sound(1000);delay(10);nosound;
          end;

   if tel=10 then
          begin
           sound(2000);delay(20);nosound;
           inc(leven);
           tel:=0;
          end;
   if not hit then dec(leven);
  end;



 procedure look_u_bat;
   var t : integer;
  begin

{   for t:=-1 to schuin do if (ball_x>mx)and(ball_x<mx+60) and (ball_y=450+t) then
                       begin
                        sound(100);delay(100);nosound;
                        r_y:=-1;
                        hit:=True;
                        ball_to_c:=True;
                       end
    else hit:=false;
    levens;
    score;
{    if not hit then af;}

   if (ball_x>mx)and(ball_x<mx+60)then
      begin
       if schuin>=0 then for t:=0 to schuin do
          begin
           if ball_y=450+t then halt;
          end;

       if schuin<0 then for t:=0 downto schuin do
          begin
           if ball_y=450+t then halt;
          end;


      end;

  end;

 procedure update_ball;
  begin

   setcolor(black);
{   circle(o_ball_x,o_ball_y,10);}

   inc(ball_x,r_x);
   inc(ball_y,r_y);

   if ball_x>630 then r_x:=-1;
   if ball_x<10 then r_x:=1;
   if ball_y<10 then r_y:=1;

   if ball_y>446 then look_u_bat;
   if ball_y>474 then r_y:=-1{ look_u_bat;};


   o_ball_x:=ball_x;
   o_ball_y:=ball_y;

   setcolor(white);
{   circle(ball_x,ball_y,10);}
   putpixel(ball_x,ball_y,yellow);
   delay(5);
  end;


 procedure main;
  begin
   repeat
    ch:=#0;

    updatevars;
    update_u_bat;
    update_c_bat;

    update_ball;

   until ch=#27;
  end;

 begin
  clrscr;
  initscreen;
  setvars;
  main;
 end.