Einzelnen Beitrag anzeigen

Namenloser

Registriert seit: 7. Jun 2006
Ort: Karlsruhe
3.724 Beiträge
 
FreePascal / Lazarus
 
#19

Re: Space Invaders in Delphi5

  Alt 14. Feb 2010, 18:19
Ich meinte eher sowas:
Delphi-Quellcode:
type
  TSprite = class
  protected
    FImage: TImage;
    FParent: TForm;
    procedure InitImage; virtual; abstract;
  public
    constructor Create(Parent: TForm; Position: TPoint);
    destructor Destroy; override;
    procedure Move; virtual; abstract;
    function CollidesWith(AObject: TSprite): boolean;
  end;

  TInvader = class(TSprite)
  protected
    procedure InitImage; override;
  public
    procedure Move; override;
  end;

  TBullet = class(TSprite)
  protected
    procedure InitImage; override;
  public
    procedure Move; override;
  end;

implementation

constructor TSprite.Create(Parent: TForm; Position: TPoint);
begin
  inherited Create;
  FParent := Parent;
  FImage := TImage.Create(nil);
  FImage.Parent := Parent;
  InitImage;
  FImage.Left := Position.X;
  FImage.Top := Position.Y;
end;

destructor TSprite.Destroy;
begin
  FImage.Free;
  inherited;
end;

function TSprite.CollidesWith(AObject: TSprite): boolean;
begin
  Result := (FImage.Left+FImage.Width > AObject.Left) and
            (FImage.Top +FImage.Height > AObject.Top ) and
            ...;
end;

procedure TInvader.InitImage;
begin
  FImage.LoadFromFile('invader.bmp'); // z.B...
end;

procedure TInvader.Move;
begin
  FImage.Left := FImage.Left+1; // z.B...
end;


procedure TBullet.InitImage;
begin
  FImage.LoadFromFile('bullet.bmp'); // z.B...
end;

procedure TInvader.Move;
begin
  FImage.Top := FImage.Top-10; // z.B...
end;
Delphi-Quellcode:
TForm1 = class(TForm)
...
private
  FSprites: TObjectList;
...
end;

implementation

procedure TForm1.FormCreate(Sender: TObject);
var
  i,j: integer;
begin
  FSprites := TObjectList.Create(True);
  for i := 0 to 8-1 do
    for j:= 0 to 3-1 do
      FSprites.Add(TInvader.Create(self, Point(i*30, j*20)));
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  i,j: integer;
begin
  for i := 0 to FSprites.Count-1 do
    TSprite(FSprites[i]).Move;
  ...
end;
  Mit Zitat antworten Zitat