Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
- Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls, Math,
- Vcl.Imaging.GIFImg, Vcl.Imaging.PngImage;
- type
- TMainForm = class(TForm)
- Cannon: TImage;
- Target: TImage;
- Torpedo: TImage;
- Wall: TImage;
- Explosion: TImage;
- Timer1: TTimer;
- Timer2: TTimer;
- ExplosionTimer: TTimer;
- InfoLabel: TLabel;
- SpeedLabel: TLabel;
- procedure FormCreate(Sender: TObject);
- procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
- procedure Timer1Timer(Sender: TObject);
- procedure Timer2Timer(Sender: TObject);
- procedure FireTorpedo(Sender: TObject);
- procedure ExplodeTarget(Sender: TObject);
- procedure ExplosionTimerTimer(Sender: TObject);
- private
- { Private declarations }
- WallHeight: Integer;
- TargetOriginalLeft: Integer; // Добавлено
- TargetOriginalTop: Integer; // Добавлено
- public
- { Public declarations }
- end;
- var
- MainForm: TMainForm;
- TorpedoSpeed: Integer;
- TorpedoFired: Boolean;
- implementation
- {$R *.dfm}
- procedure TMainForm.FormCreate(Sender: TObject);
- begin
- Randomize;
- TorpedoSpeed := 5;
- TorpedoFired := False;
- Cannon.Left := 10;
- Cannon.Top := (ClientHeight - Cannon.Height) div 2;
- WallHeight := RandomRange(Round(ClientHeight * 0.25), Round(ClientHeight * 0.5));
- Wall.Left := ClientWidth - Wall.Width;
- Wall.Top := ClientHeight - WallHeight;
- Wall.Height := WallHeight;
- TargetOriginalLeft := 10;
- TargetOriginalTop := RandomRange(0, ClientHeight - Target.Height);
- Target.Left := ClientWidth - Target.Width - 10; // Изменено
- Target.Top := TargetOriginalTop;
- Explosion.Visible := False;
- Explosion.Left := Target.Left + (Target.Width - Explosion.Width) div 2;
- Explosion.Top := Target.Top + (Target.Height - Explosion.Height) div 2;
- (Explosion.Picture.Graphic as TGIFImage).Animate := False;
- Timer1.Enabled := True;
- end;
- procedure TMainForm.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
- begin
- case Key of
- VK_RETURN:
- if not TorpedoFired then
- FireTorpedo(Sender);
- Ord('1') .. Ord('9'):
- if not TorpedoFired then
- begin
- TorpedoSpeed := (Key - Ord('0')) * 2;
- SpeedLabel.Caption := IntToStr(TorpedoSpeed div 2);
- end;
- end;
- end;
- procedure TMainForm.Timer1Timer(Sender: TObject);
- begin
- Target.Visible := not Target.Visible;
- if Target.Visible then
- begin
- Target.Left := TargetOriginalLeft; // Добавлено
- Target.Top := TargetOriginalTop; // Добавлено
- Timer1.Interval := RandomRange(500, 1000);
- end
- else
- begin
- Timer1.Interval := RandomRange(500, 1000);
- end;
- end;
- procedure TMainForm.Timer2Timer(Sender: TObject);
- begin
- if TorpedoFired then
- begin
- Torpedo.Left := Torpedo.Left + TorpedoSpeed;
- if Torpedo.Left > Wall.Left then
- begin
- if (Torpedo.Top + Torpedo.Height >= Wall.Top) and (Torpedo.Top <= Wall.Top + Wall.Height) then
- ExplodeTarget(Sender)
- else
- begin
- TorpedoFired := False;
- Torpedo.Visible := False;
- end;
- end
- else if Torpedo.Left > ClientWidth then
- begin
- TorpedoFired := False;
- Torpedo.Visible := False;
- end;
- end;
- end;
- procedure TMainForm.ExplosionTimerTimer(Sender:TObject);
- begin
- Explosion.Visible := False;
- ExplosionTimer.Enabled := False;
- (Explosion.Picture.Graphic as TGIFImage).Animate := False;
- TorpedoFired := False;
- Torpedo.Visible := False;
- WallHeight := RandomRange(Round(ClientHeight * 0.25), Round(ClientHeight * 0.5));
- Wall.Top := ClientHeight - WallHeight;
- Wall.Height := WallHeight;
- Timer1.Enabled := True;
- end;
- procedure TMainForm.FireTorpedo(Sender: TObject);
- begin
- TorpedoFired := True;
- Torpedo.Left := Cannon.Left + Cannon.Width;
- Torpedo.Top := Cannon.Top + (Cannon.Height - Torpedo.Height) div 2;
- Torpedo.Visible := True;
- Timer2.Enabled := True;
- end;
- procedure TMainForm.ExplodeTarget(Sender: TObject);
- begin
- Explosion.Visible := True;
- (Explosion.Picture.Graphic as TGIFImage).Animate := True;
- ExplosionTimer.Enabled := True;
- Timer2.Enabled := False;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement