this slowpoke moves

Draw Quaders & Animate Flicker-Free

type
  TMyObject = record
    x: Integer;
    y: Integer;
    xr: ShortInt;
    yr: ShortInt;
    maxl: Word;
    maxt: Word;
    Bmp: TBitmap;
  end;

var
  MyObject: array[0..500] of TMyObject;
  TmpBmp: TBitmap;

//

procedure TForm1.FormCreate(Sender: TObject);
const
  speed = 6;
var
  i: Integer;
begin
  Randomize;
  DoubleBuffered := True;
  TmpBmp := TBitmap.Create;
  TmpBmp.Width := ClientWidth;
  TmpBmp.Height := ClientHeight;

  for i := Low(MyObject) to High(MyObject) do
    with MyObject[i] do
    begin
      Bmp := TBitmap.Create;
      with Bmp do
      begin
        Canvas.Pen.Color := TColor(Random($FFFFFF));
        Canvas.Brush.Color := TColor(Random($FFFFFF));
        Width := Random(100);
        Height := Width;
        Canvas.Rectangle(Canvas.ClipRect);
        //with Canvas, ClipRect do Rectangle(Left, Top, Right, Bottom)
      end;

      maxl := Self.ClientWidth - Bmp.Width;
      maxt := Self.ClientHeight - Bmp.Height;

      x := Random(maxl);
      y := Random(maxt);
      repeat xr := (speed div 2) - Random(speed) until xr <> 0;
      repeat yr := (speed div 2) - Random(speed) until yr <> 0;
    end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var
  i: Integer;
begin
  TmpBmp.Free;

  for i := Low(MyObject) to High(MyObject) do
    with MyObject[i] do
    begin
      Bmp.Free;
    end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  i: Integer;
begin
  TmpBmp.Canvas.Brush.Color := clWhite;
  TmpBmp.Canvas.FillRect(TmpBmp.Canvas.ClipRect);

  with TmpBmp.Canvas do //Ersatz für Hintergrundbild
  begin
    Pen.Color := clRed;
    Pen.Width := 20;
    Brush.Color := clYellow;
    Ellipse(TmpBmp.Canvas.ClipRect);
    //with Canvas, ClipRect do Ellipse(Left, Top, Right, Bottom)
  end;

  for i := Low(MyObject) to High(MyObject) do
  begin
    with MyObject[i] do
    begin
      x := x + xr;
      if x > maxl then
      begin
        x := maxl;
        xr := xr  * -1;
      end
        else if x < 0 then
        begin
          x := 0;
          xr := xr  * -1;
        end;

      y := y + yr;
      if y > maxt then
      begin
        y := maxt;
        yr := yr  * -1;
      end
        else if y < 0 then
        begin
          y := 0;
          yr := yr  * -1;
        end;

      TmpBmp.Canvas.Draw(x, y, Bmp);
    end;
  end;

  Canvas.Draw(0, 0, TmpBmp);
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate