Advertisement
gguuppyy

61

Apr 18th, 2024 (edited)
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6. Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  7. Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls, Math,
  8. Vcl.Imaging.GIFImg, Vcl.Imaging.PngImage;
  9.  
  10. type
  11. TMainForm = class(TForm)
  12. Cannon: TImage;
  13. Target: TImage;
  14. Torpedo: TImage;
  15. Wall: TImage;
  16. Explosion: TImage;
  17. Timer1: TTimer;
  18. Timer2: TTimer;
  19. ExplosionTimer: TTimer;
  20. InfoLabel: TLabel;
  21. SpeedLabel: TLabel;
  22. procedure FormCreate(Sender: TObject);
  23. procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  24. procedure Timer1Timer(Sender: TObject);
  25. procedure Timer2Timer(Sender: TObject);
  26. procedure FireTorpedo(Sender: TObject);
  27. procedure ExplodeTarget(Sender: TObject);
  28. procedure ExplosionTimerTimer(Sender: TObject);
  29. private
  30. { Private declarations }
  31. WallHeight: Integer;
  32. TargetOriginalLeft: Integer; // Добавлено
  33. TargetOriginalTop: Integer; // Добавлено
  34. public
  35. { Public declarations }
  36. end;
  37.  
  38. var
  39. MainForm: TMainForm;
  40. TorpedoSpeed: Integer;
  41. TorpedoFired: Boolean;
  42.  
  43. implementation
  44.  
  45. {$R *.dfm}
  46.  
  47. procedure TMainForm.FormCreate(Sender: TObject);
  48. begin
  49. Randomize;
  50. TorpedoSpeed := 5;
  51. TorpedoFired := False;
  52.  
  53. Cannon.Left := 10;
  54. Cannon.Top := (ClientHeight - Cannon.Height) div 2;
  55.  
  56. WallHeight := RandomRange(Round(ClientHeight * 0.25), Round(ClientHeight * 0.5));
  57. Wall.Left := ClientWidth - Wall.Width;
  58. Wall.Top := ClientHeight - WallHeight;
  59. Wall.Height := WallHeight;
  60.  
  61. TargetOriginalLeft := 10;
  62. TargetOriginalTop := RandomRange(0, ClientHeight - Target.Height);
  63. Target.Left := ClientWidth - Target.Width - 10; // Изменено
  64. Target.Top := TargetOriginalTop;
  65.  
  66. Explosion.Visible := False;
  67. Explosion.Left := Target.Left + (Target.Width - Explosion.Width) div 2;
  68. Explosion.Top := Target.Top + (Target.Height - Explosion.Height) div 2;
  69. (Explosion.Picture.Graphic as TGIFImage).Animate := False;
  70.  
  71. Timer1.Enabled := True;
  72. end;
  73.  
  74. procedure TMainForm.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  75. begin
  76. case Key of
  77. VK_RETURN:
  78. if not TorpedoFired then
  79. FireTorpedo(Sender);
  80. Ord('1') .. Ord('9'):
  81. if not TorpedoFired then
  82. begin
  83. TorpedoSpeed := (Key - Ord('0')) * 2;
  84. SpeedLabel.Caption := IntToStr(TorpedoSpeed div 2);
  85. end;
  86. end;
  87. end;
  88.  
  89. procedure TMainForm.Timer1Timer(Sender: TObject);
  90. begin
  91. Target.Visible := not Target.Visible;
  92. if Target.Visible then
  93. begin
  94. Target.Left := TargetOriginalLeft; // Добавлено
  95. Target.Top := TargetOriginalTop; // Добавлено
  96. Timer1.Interval := RandomRange(500, 1000);
  97. end
  98. else
  99. begin
  100. Timer1.Interval := RandomRange(500, 1000);
  101. end;
  102. end;
  103.  
  104. procedure TMainForm.Timer2Timer(Sender: TObject);
  105. begin
  106. if TorpedoFired then
  107. begin
  108. Torpedo.Left := Torpedo.Left + TorpedoSpeed;
  109. if Torpedo.Left > Wall.Left then
  110. begin
  111. if (Torpedo.Top + Torpedo.Height >= Wall.Top) and (Torpedo.Top <= Wall.Top + Wall.Height) then
  112. ExplodeTarget(Sender)
  113. else
  114. begin
  115. TorpedoFired := False;
  116. Torpedo.Visible := False;
  117. end;
  118. end
  119. else if Torpedo.Left > ClientWidth then
  120. begin
  121. TorpedoFired := False;
  122. Torpedo.Visible := False;
  123. end;
  124. end;
  125. end;
  126.  
  127. procedure TMainForm.ExplosionTimerTimer(Sender:TObject);
  128. begin
  129. Explosion.Visible := False;
  130. ExplosionTimer.Enabled := False;
  131. (Explosion.Picture.Graphic as TGIFImage).Animate := False;
  132. TorpedoFired := False;
  133. Torpedo.Visible := False;
  134. WallHeight := RandomRange(Round(ClientHeight * 0.25), Round(ClientHeight * 0.5));
  135. Wall.Top := ClientHeight - WallHeight;
  136. Wall.Height := WallHeight;
  137. Timer1.Enabled := True;
  138. end;
  139.  
  140. procedure TMainForm.FireTorpedo(Sender: TObject);
  141. begin
  142. TorpedoFired := True;
  143. Torpedo.Left := Cannon.Left + Cannon.Width;
  144. Torpedo.Top := Cannon.Top + (Cannon.Height - Torpedo.Height) div 2;
  145. Torpedo.Visible := True;
  146. Timer2.Enabled := True;
  147. end;
  148.  
  149. procedure TMainForm.ExplodeTarget(Sender: TObject);
  150. begin
  151. Explosion.Visible := True;
  152. (Explosion.Picture.Graphic as TGIFImage).Animate := True;
  153. ExplosionTimer.Enabled := True;
  154. Timer2.Enabled := False;
  155. end;
  156.  
  157. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement