To add a moving background with sparkling stars and comets in a space-themed top-down shooter game in Delphi 2010, you can utilize a TImage component and manipulate its properties to create the desired effect. Here's an example implementation:
-
Add a TImage component to the form and set its Align property to alClient. Load an image of a space background as the Picture property of the image.
-
Add a TTimer component to the form and set its Interval property to control the speed of the background animation.
-
Declare the following variables in the form class to control the movement of the background:
var
BackgroundY: Integer;
StarList: TList<TImage>;
CometList: TList<TImage>;
- Initialize the background-related variables in the FormCreate event:
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
begin
// Existing code...
BackgroundY := 0;
// Initialize lists
StarList := TList<TImage>.Create;
CometList := TList<TImage>.Create;
// Create stars
for i := 0 to 100 do
begin
CreateStar(i);
end;
// Create comets
for i := 0 to 10 do
begin
CreateComet(i);
end;
// Existing code...
end;
- Add the CreateStar procedure to create a sparkling star image and add it to the StarList:
procedure TForm1.CreateStar(Index: Integer);
var
Star: TImage;
begin
Star := TImage.Create(Self);
Star.Picture.LoadFromFile('star.png'); // Replace with your star image
Star.Transparent := True;
Star.Left := Random(ClientWidth);
Star.Top := Random(ClientHeight);
Star.Width := 5;
Star.Height := 5;
StarList.Add(Star);
Star.Parent := Image1;
end;
- Similarly, add the CreateComet procedure to create a moving comet image and add it to the CometList:
procedure TForm1.CreateComet(Index: Integer);
var
Comet: TImage;
begin
Comet := TImage.Create(Self);
Comet.Picture.LoadFromFile('comet.png'); // Replace with your comet image
Comet.Transparent := True;
Comet.Left := Random(ClientWidth);
Comet.Top := Random(ClientHeight);
Comet.Width := 50;
Comet.Height := 50;
CometList.Add(Comet);
Comet.Parent := Image1;
end;
- Implement the background animation in the Timer1Timer event:
procedure TForm1.Timer1Timer(Sender: TObject);
var
i: Integer;
Star: TImage;
Comet: TImage;
begin
// Move the background
BackgroundY := BackgroundY + 1;
Image1.Canvas.Draw(0, 0, Image1.Picture.Graphic);
Image1.Canvas.Draw(0, -BackgroundY, Image1.Picture.Graphic);
// Move and update stars
for i := 0 to StarList.Count - 1 do
begin
Star := StarList[i];
Star.Top := Star.Top + 1;
if Star.Top > ClientHeight then
begin
Star.Top := 0;
Star.Left := Random(ClientWidth);
end;
end;
// Move and update comets
for i := 0 to CometList.Count - 1 do
begin
Comet := CometList[i];
Comet.Left := Comet.Left - 2;
if Comet.Left + Comet.Width < 0 then
begin
Comet.Left := ClientWidth;
Comet.Top := Random(ClientHeight);
end;
end;
end;
- In the FormDestroy event, ensure to free memory by clearing and destroying the StarList and CometList:
procedure TForm1.FormDestroy(Sender: TObject);
var
i: Integer;
begin
// Free the star and comet images
for i := 0 to StarList.Count - 1 do
begin
StarList[i].Free;
end;
StarList.Clear;
StarList.Free;
for i := 0 to CometList.Count - 1 do
begin
CometList[i].Free;
end;
CometList.Clear;
CometList.Free;
end;
Ensure that you have star.png and comet.png files available, incorporating suitable images for the stars and comets.
With these changes, you will have a moving background with