gguuppyy

61vl

Apr 18th, 2024
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. Unit Unit1;
  2.  
  3. Interface
  4.  
  5. Uses
  6. Winapi.Windows,
  7. Winapi.Messages,
  8. System.SysUtils,
  9. System.Variants,
  10. System.Classes,
  11. Vcl.Graphics,
  12. Vcl.Controls,
  13. Vcl.Forms,
  14. Vcl.Dialogs,
  15. Vcl.ExtCtrls,
  16. Vcl.StdCtrls,
  17. Math,
  18. Vcl.Imaging.GIFImg,
  19. Vcl.Imaging.PngImage;
  20.  
  21. Type
  22. TMainForm = Class(TForm)
  23. Cannon: TImage;
  24. Torpedo: TImage;
  25. Explosion: TImage;
  26. Ship: TImage;
  27. Timer1: TTimer;
  28. Timer2: TTimer;
  29. ExplosionTimer: TTimer;
  30. InfoLabel: TLabel;
  31. SpeedLabel: TLabel;
  32. Water: TImage;
  33. Procedure FormCreate(Sender: TObject);
  34. Procedure FormKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
  35. Procedure Timer1Timer(Sender: TObject);
  36. Procedure Timer2Timer(Sender: TObject);
  37. Procedure FireTorpedo(Sender: TObject);
  38. Procedure ExplodeShip(Sender: TObject);
  39. Procedure ExplosionTimerTimer(Sender: TObject);
  40. Private
  41. { Private declarations }
  42. Public
  43. { Public declarations }
  44. End;
  45.  
  46. Var
  47. MainForm: TMainForm;
  48. ShipSpeed: Integer;
  49. TorpedoSpeed: Integer;
  50. TorpedoFired: Boolean;
  51.  
  52. Implementation
  53.  
  54. {$R *.dfm}
  55.  
  56. Procedure TMainForm.FormCreate(Sender: TObject);
  57. Begin
  58. Randomize;
  59. ShipSpeed := RandomRange(4, 15);
  60. TorpedoSpeed := 5;
  61. TorpedoFired := False;
  62. Ship.Left := 0;
  63. Ship.Top := 0;
  64. Cannon.Left := (ClientWidth - Cannon.Width) Div 2;
  65. Cannon.Top := ClientHeight - Cannon.Height;
  66. Water.Picture.LoadFromFile('water.gif');
  67. Water.Stretch := True;
  68. Water.Top := Ship.Top + Ship.Height;
  69. Water.Width := ClientWidth + 100;
  70. Water.SendToBack;
  71. (Water.Picture.Graphic As TGIFImage).Animate := True;
  72. End;
  73.  
  74. Procedure TMainForm.FormKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
  75. Begin
  76. Case Key Of
  77. VK_LEFT:
  78. Cannon.Left := Math.Max(Cannon.Left - 10, 0);
  79. VK_RIGHT:
  80. Cannon.Left := Math.Min(Cannon.Left + 10, ClientWidth - Cannon.Width);
  81. VK_RETURN:
  82. If Not TorpedoFired Then
  83. FireTorpedo(Sender);
  84. Ord('1') .. Ord('9'):
  85. If Not TorpedoFired Then
  86. Begin
  87. TorpedoSpeed := (Key - Ord('0')) * 2;
  88. SpeedLabel.Caption := IntToStr(TorpedoSpeed Div 2);
  89. End;
  90. End;
  91. End;
  92.  
  93. Procedure TMainForm.Timer1Timer(Sender: TObject);
  94. Begin
  95. Ship.Left := Ship.Left + ShipSpeed;
  96.  
  97. If Ship.Left > ClientWidth Then
  98. Begin
  99. ShipSpeed := RandomRange(4, 15);
  100. Ship.Left := -Ship.Width;
  101. End;
  102. End;
  103.  
  104. Procedure TMainForm.Timer2Timer(Sender: TObject);
  105. Begin
  106. If TorpedoFired Then
  107. Begin
  108. Torpedo.Top := Torpedo.Top - TorpedoSpeed;
  109. If Torpedo.Top + Torpedo.Height < 0 Then
  110. Begin
  111. TorpedoFired := False;
  112. Torpedo.Visible := False;
  113. End
  114. Else
  115. If Torpedo.BoundsRect.IntersectsWith(Ship.BoundsRect) Then
  116. ExplodeShip(Sender);
  117. End;
  118. End;
  119.  
  120. Procedure TMainForm.ExplosionTimerTimer(Sender: TObject);
  121. Begin
  122. Explosion.Visible := False;
  123. ExplosionTimer.Enabled := False;
  124. (Explosion.Picture.Graphic As TGIFImage).Animate := False;
  125. TorpedoFired := False;
  126. Torpedo.Visible := False;
  127. ShipSpeed := RandomRange(4, 15);
  128. Ship.Left := -Ship.Width;
  129. Timer1.Enabled := True;
  130. End;
  131.  
  132. Procedure TMainForm.FireTorpedo(Sender: TObject);
  133. Begin
  134. TorpedoFired := True;
  135. Torpedo.Left := Cannon.Left + (Cannon.Width - Torpedo.Width) Div 2;
  136. Torpedo.Top := Cannon.Top - Torpedo.Height;
  137. Torpedo.Visible := True;
  138. Timer2.Enabled := True;
  139. End;
  140.  
  141. Procedure TMainForm.ExplodeShip(Sender: TObject);
  142. Begin
  143. Explosion.Left := Ship.Left + (Ship.Width - Explosion.Width) Div 2;
  144. Explosion.Top := Ship.Top + (Ship.Height - Explosion.Height) Div 2;
  145. Explosion.Visible := True;
  146. (Explosion.Picture.Graphic As TGIFImage).Animate := True;
  147. ExplosionTimer.Enabled := True;
  148. Timer2.Enabled := False;
  149. Timer1.Enabled := False;
  150. End;
  151.  
  152. End.
Add Comment
Please, Sign In to add comment