this slowpoke moves

Draw Starfield Side Scroller

Unit StarCls.pas
unit StarCls;

interface

uses
  Types, Graphics;

type
  TStar = class(TObject)
  private
    FPosition: TPoint;
    FColor: TColor;
    FSpeed: Integer;
    FSize: Integer;
    procedure SetPos(Position: TPoint);
  public
    constructor Create(Position: TPoint; Color: TColor; Speed: Integer);
    procedure DrawStar(Canvas: TCanvas);
    property Position: TPoint read FPosition write SetPos;
    property Speed: Integer read FSpeed write FSpeed;
    property Size: Integer read FSize write FSize;
  end;

implementation

constructor TStar.Create(Position: TPoint; Color: TColor; Speed: Integer);
begin
  FPosition := Position;
  FColor := Color;
  FSpeed := Speed;
  FSize := 2;
end;

procedure TStar.DrawStar(Canvas: TCanvas);
var
  rect: TRect;
  OldColor: TColor;
begin
  with canvas do
  begin
    OldColor := Brush.Color;
    rect.Left := FPosition.X;
    rect.Top := FPosition.Y;
    rect.Right := FPosition.X + FSize;
    rect.Bottom := FPosition.Y + FSize;
    Pen.Color := FColor;
    Brush.Color := FColor;
    Brush.Style := bsSolid;
    Ellipse(rect);
    Brush.Color := OldColor;
  end;
end;

procedure TStar.SetPos(Position: TPoint);
begin
  FPosition := Position;
end;

end.
Unit1 :
uses StarCls

private
    { Private declarations }
    Stars: array of TStar;
    MemBMP: TBitmap;
    
const
  NUMSTARS = 500;
  
//

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
  pt: TPoint;
  StarSpeed: Integer;
  StarColor: TColor;
begin
  StarColor := clWhite;
  Randomize;
  DoubleBuffered := True;
  SetLength(Stars, NUMSTARS);
  for i := 0 to NUMSTARS - 1 do
  begin
    pt.x := Random(Screen.Width);
    pt.y := Random(Screen.Height);
    StarSpeed := Random(3) + 1;
    case StarSpeed of
      1: StarColor := clGray;
      2: StarColor := clSilver;
      3: StarColor := clWhite;
    end;
    Stars[i] := TStar.Create(pt, StarColor, StarSpeed);
    //Stars[i].Size := StarSpeed;
  end;
  MemBMP := TBitmap.Create;
  MemBMP.Canvas.Brush.Color := clBlack;
  MemBMP.Width := Screen.Width;
  MemBMP.Height := Screen.Height;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to length(Stars) - 1 do
    Stars[i].Free;

  MemBMP.Free;
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
  BitBlt(Canvas.Handle, 0, 0, Width, Height, MemBMP.Canvas.Handle, 0, 0,
    SRCCOPY);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  i: Integer;
  pt: TPoint;
begin
  MemBMP.Canvas.FillRect(ClientRect);
  for i := 0 to length(Stars) - 1 do
  begin
    pt := Stars[i].Position;
    pt.X := pt.X + Stars[i].Speed;
    if pt.X > Width then
      pt.X := 0;
    Stars[i].Position := pt;
    Stars[i].DrawStar(MemBMP.Canvas);
  end;
  Form1.Repaint;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form1.Repaint;
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_ESCAPE then Close;
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate