unit Unit2;
interface
uses
sysutils, Windows,Classes,ExtCtrls,Graphics,Forms,Controls, Messages,Dialogs;
type
  visu_entree = class(tpanel)
  public
    nom: string;
    ox: integer;
    oy: integer;
    procedure Panel1Mousedown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    constructor create(AOwner: TComponent);override;
  end;
  ennemi = class(tpanel)
  public
    directiony: integer;
    directionx: integer;
    decx: integer;
    decy: integer;
    function gettouch(tutu: visu_entree): boolean;
    procedure bouge(AOwner: TComponent; deca: integer);
    constructor create(AOwner: TComponent); override;

      end;
  TForm2 = class(TForm)
    Timer1: TTimer;
    procedure init;
    procedure Timer1Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  end;
var
  Form2: TForm2;
  r: visu_entree;
  e1, e2, e3 : ennemi;
  decc,temps: integer;
implementation
{$R *.dfm}
constructor ennemi.Create(AOwner: TComponent);
begin
  inherited create(aowner);
  directiony := -1;directionx := -1;
  height := 60; width := 60;
  color := clred;
end;

procedure ennemi.bouge(AOwner: TComponent; deca: integer);
begin
 if (top <= 0) or ((top + Height) >= tform(self.Owner).height - 30) then
    directiony := directiony * -1;
  top := top + deca * decy * directiony;
  if (left <= 0) or ((left + width) >= tform(self.owner).width - 30) then
    directionx := directionx * -1;
  left := left + deca * decx * directionx;

end;

constructor visu_entree.Create(AOwner: TComponent);
begin
  inherited create(aowner);
  height := 40; width := 40;
  color := clblue;
  onmousemove := Panel1MouseMove;
  onmousedown := Panel1Mousedown;
end;

procedure visu_entree.Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if shift = [ssLeft] then
    begin
      if (top + y - oy > 0) and (top + height + y < tform(self.owner).Height - 5) then
        Top := top + y - oy;
      if (left + x - ox > 0) and (left + width - ox + x < tform(self.owner).width - 5) then
        left := left + x - ox;
    end;
end;

procedure visu_entree.Panel1Mousedown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  ox := x;
  oy := y;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
  form2.DoubleBuffered := true;
  init;
end;

function ennemi.gettouch(tutu: visu_entree): boolean;
begin
result := (((top + Height >= tutu.Top) and (top < tutu.top)) or ((top <= tutu.Top + tutu.Height) and (top > tutu.Top)))
            and
            (((left + width >= tutu.left) and (left < tutu.left)) or ((left <= tutu.left + tutu.width) and (left > tutu.left)));

end;

procedure TForm2.Timer1Timer(Sender: TObject);
begin
  begin
  inc(temps, 1);

  e1.bouge(self, decc);  e2.bouge(self, decc);  e3.bouge(self, decc);

  caption := format('Score : %d',[temps]);
  if (e1.gettouch(r)) or (e2.gettouch(r)) or (e3.gettouch(r)) then begin
      timer1.Enabled := false;
      showmessage(format('Score : %d',[temps]));
      if application.messagebox('Voulez vous recommencer ?', 'Dodge', MB_OKCANCEL) = 1 then begin
          init;
          timer1.Enabled := true;
      end;
  end;

  if temps = 500 then inc(decc, 1);
  if temps = 1050 then begin
      inc(e1.decx, 1); dec(e1.decy, 1);
      inc(e2.decx, 1); dec(e3.decy, 1);
  end;
  if (temps = 2500) or (temps = 1500) then begin
      inc(decc, 1);
      e1.directiony := e1.directiony * -1;     e2.directionx := e2.directionx * -1;
      e3.directiony := e3.directiony * -1;     e3.directionx := e3.directionx * -1;
  end;
  if temps = 2000 then begin
      dec(e1.decx, 1);  inc(e1.decy, 1);
      inc(e2.decy, 1);  inc(e3.decy, 1);
  end;
         end;
end;

procedure TForm2.init;
begin
 if not assigned(r) then begin
     r := visu_entree.create(self);
     r.parent := self;
  end;

  if not assigned(e1) then begin
     e1 := ennemi.create(self);
     e1.parent := self;
  end;

  if not assigned(e2) then begin
     e2 := ennemi.create(self);
     e2.parent := self;
  end;

  if not assigned(e3) then begin
     e3 := ennemi.create(self);
     e3.parent := self;
     end;
  temps := 0; decc := 1;
  e1.top := 10; e1.left := 10;
  e1.decx := 2; e1.decy := 2;
  e2.Top := 300; e2.left := 15;
  e2.Height := 90; e2.width := 50;
  e2.directiony := 1; e2.directionx := -1;
  e2.decx := 2; e2.decy := 2;
  e3.Top := 250; e3.left := 450;
  e3.directiony := 1; e3.directionx := 1;
  e3.decx := 2; e3.decy := 2;
  r.left := form2.width div 2; r.top := form2.Height div 2;
end;
end.
