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;
- Torpedo: TImage;
- Explosion: TImage;
- Ship: TImage;
- Timer1: TTimer;
- Timer2: TTimer;
- ExplosionTimer: TTimer;
- InfoLabel: TLabel;
- SpeedLabel: TLabel;
- Water: TImage;
- 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 ExplodeShip(Sender: TObject);
- Procedure ExplosionTimerTimer(Sender: TObject);
- Private
- { Private declarations }
- Public
- { Public declarations }
- End;
- Var
- MainForm: TMainForm;
- ShipSpeed: Integer;
- TorpedoSpeed: Integer;
- TorpedoFired: Boolean;
- Implementation
- {$R *.dfm}
- Procedure TMainForm.FormCreate(Sender: TObject);
- Begin
- Randomize;
- ShipSpeed := RandomRange(4, 15);
- TorpedoSpeed := 5;
- TorpedoFired := False;
- Ship.Left := 0;
- Ship.Top := 0;
- Cannon.Left := (ClientWidth - Cannon.Width) Div 2;
- Cannon.Top := ClientHeight - Cannon.Height;
- Water.Picture.LoadFromFile('water.gif');
- Water.Stretch := True;
- Water.Top := Ship.Top + Ship.Height;
- Water.Width := ClientWidth + 100;
- Water.SendToBack;
- (Water.Picture.Graphic As TGIFImage).Animate := True;
- End;
- Procedure TMainForm.FormKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
- Begin
- Case Key Of
- VK_LEFT:
- Cannon.Left := Math.Max(Cannon.Left - 10, 0);
- VK_RIGHT:
- Cannon.Left := Math.Min(Cannon.Left + 10, ClientWidth - Cannon.Width);
- 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
- Ship.Left := Ship.Left + ShipSpeed;
- If Ship.Left > ClientWidth Then
- Begin
- ShipSpeed := RandomRange(4, 15);
- Ship.Left := -Ship.Width;
- End;
- End;
- Procedure TMainForm.Timer2Timer(Sender: TObject);
- Begin
- If TorpedoFired Then
- Begin
- Torpedo.Top := Torpedo.Top - TorpedoSpeed;
- If Torpedo.Top + Torpedo.Height < 0 Then
- Begin
- TorpedoFired := False;
- Torpedo.Visible := False;
- End
- Else
- If Torpedo.BoundsRect.IntersectsWith(Ship.BoundsRect) Then
- ExplodeShip(Sender);
- 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;
- ShipSpeed := RandomRange(4, 15);
- Ship.Left := -Ship.Width;
- Timer1.Enabled := True;
- End;
- Procedure TMainForm.FireTorpedo(Sender: TObject);
- Begin
- TorpedoFired := True;
- Torpedo.Left := Cannon.Left + (Cannon.Width - Torpedo.Width) Div 2;
- Torpedo.Top := Cannon.Top - Torpedo.Height;
- Torpedo.Visible := True;
- Timer2.Enabled := True;
- End;
- Procedure TMainForm.ExplodeShip(Sender: TObject);
- Begin
- Explosion.Left := Ship.Left + (Ship.Width - Explosion.Width) Div 2;
- Explosion.Top := Ship.Top + (Ship.Height - Explosion.Height) Div 2;
- Explosion.Visible := True;
- (Explosion.Picture.Graphic As TGIFImage).Animate := True;
- ExplosionTimer.Enabled := True;
- Timer2.Enabled := False;
- Timer1.Enabled := False;
- End;
- End.
Add Comment
Please, Sign In to add comment