unit uLabda;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls;

const Sor = 3;
      Oszlop = 8;

type
  TfLabda = class(TForm)
    Labda: TShape;
    Uto: TShape;
    Idozito: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure IdozitoTimer(Sender: TObject);
    function Osszeer(Sh1,Sh2:TShape):Boolean;
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
    Teglak : Array[1..Sor,1..Oszlop] Of TShape;
    dx, dy : Integer;
  public
    { Public declarations }
  end;

var
  fLabda: TfLabda;

implementation

{$R *.DFM}

procedure TfLabda.FormCreate(Sender: TObject);
Var szelesseg, magassag: Integer;
    i,j:Integer;
begin
     Randomize;

     szelesseg := ClientWidth Div oszlop;
     magassag := szelesseg Div 3;

     Labda.Pen.Color := clRed;
     Labda.Pen.Style := psSolid;
     Labda.Brush.Color := clRed;
     Labda.Brush.Style := bsSolid;
     Labda.Shape := stCircle;
     Labda.Width := magassag;
     Labda.Height := magassag;
     Labda.Left := 0;
     Labda.Top := Sor * magassag;

     Uto.Pen.Color := clBlue;
     Uto.Pen.Style := psSolid;
     Uto.Brush.Color := clBlue;
     Uto.Brush.Style := bsSolid;
     Uto.Shape := stRectangle;
     Uto.Width := szelesseg;
     Uto.Height := magassag;
     Uto.Left := 0;
     Uto.Top := ClientHeight - magassag;

     For i:=1 to Sor Do
         For j:=1 To Oszlop Do
            Begin
            Teglak[i,j] := TShape.Create(Self);
            With Teglak[i,j] Do
              Begin
              Pen.Color := clBlack;
              Pen.Style := psSolid;
              Brush.Color := clYellow;
              Brush.Style := bsSolid;
              Shape := stRectangle;
              Width := szelesseg;
              Height := magassag;
              Left := (j-1) * szelesseg;
              Top := (i-1)* magassag;
              Parent := Self;
              Visible := True;
              End;
           End;
     dx := 5 + random(6);
     dy := 5 + random(6);
end;

function TfLabda.Osszeer(Sh1,Sh2:TShape):Boolean;
Var p:TPoint;
    t:TRect;
Begin
     Osszeer := false;
     p.x := Sh1.Left;
     p.y := Sh1.Top;
     t.Left := Sh2.Left;
     t.Top := Sh2.Top;
     t.Right := Sh2.Left + Sh2.Width;
     t.Bottom := Sh2.Top + Sh2.Height;
     If PtInRect(t,p) Then
       Osszeer := true;
     p.x := Sh1.Left;
     p.y := Sh1.Top + Sh1.Height;
     If PtInRect(t,p) Then
       Osszeer := true;
     p.x := Sh1.Left + Sh1.Width;
     p.y := Sh1.Top;
     If PtInRect(t,p) Then
       Osszeer := true;
     p.x := Sh1.Left + Sh1.Width;
     p.y := Sh1.Top + Sh1.Height;
     If PtInRect(t,p) Then
       Osszeer := true;

End;

procedure TfLabda.IdozitoTimer(Sender: TObject);
Var i,j:Integer;
    nv:Boolean;
begin
     If (Labda.Left + dx < 0) Or
        (Labda.Left + Labda.Width + dx > ClientWidth) Then
        dx := -dx;
     If Labda.Top + Labda.Height + dy < 0 Then
        dy := -dy;
     If Labda.Top + Labda.Height + dy > ClientHeight Then
        Begin
        Idozito.Enabled := false;
        MessageDlg('Sajnos vesztettél!',mtCustom,[mbOK],0);
        Close;
        Exit;
        End;
     If Osszeer(Labda,Uto) Then
        dy := -dy;
     For i:=1 to Sor Do
         For j:=1 To Oszlop Do
            If Teglak[i,j].Visible Then
               Begin
               If Osszeer(Labda,Teglak[i,j]) Then
                  Begin
                  Teglak[i,j].Visible := false;
                  dy := -dy;
                  Break;
                  End;
                End;

     nv := false;
     For i:=1 to Sor Do
         For j:=1 To Oszlop Do
            nv := nv Or Teglak[i,j].Visible;
     if not nv then
     Begin
        Idozito.Enabled := false;
        MessageDlg('Gratulálok, nyertél!',mtCustom,[mbOK],0);
        Close;
        Exit;
     End;

     Labda.Left := Labda.Left + dx;
     Labda.Top := Labda.Top + dy;

end;

procedure TfLabda.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
     If (Key = Vk_Left) And (Uto.Left >0) Then
        Uto.Left := Uto.Left - 2 * abs(dx);
     If (Key = Vk_Right) And (Uto.Left + Uto.Width + 2*abs(dx) < ClientWidth) Then
        Uto.Left := Uto.Left + 2 * abs(dx);
end;

end.
