Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit MainUnit;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.Imaging.pngimage,
- Vcl.Imaging.jpeg, Vcl.StdCtrls, Math, Vcl.MPlayer;
- type
- TSeaBattle = class(TForm)
- Sea: TImage;
- Gun: TImage;
- Timer: TTimer;
- Ship: TImage;
- Label1: TLabel;
- Label2: TLabel;
- Label3: TLabel;
- Label4: TLabel;
- Label5: TLabel;
- Label6: TLabel;
- Label7: TLabel;
- Label8: TLabel;
- Cannonball: TImage;
- CannonballTimer: TTimer;
- Explosion: TImage;
- Info: TLabel;
- Score: TLabel;
- MediaPlayerMusic: TMediaPlayer;
- Timer2sec: TTimer;
- procedure TimerTimer(Sender: TObject);
- procedure FormKeyPress(Sender: TObject; var Key: Char);
- procedure FormCreate(Sender: TObject);
- procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
- procedure CannonballTimerTimer(Sender: TObject);
- procedure Timer2secTimer(Sender: TObject);
- private
- { Private declarations }
- procedure OpenTheGame;
- procedure StartGame;
- procedure SetGunPosition;
- procedure SetCannonStartPosition;
- public
- { Public declarations }
- end;
- var
- SeaBattle: TSeaBattle;
- IsGameStarted : Boolean;
- Speed, CannonLeft, CannonTop : Integer;
- GunPosition, NormalGunPosition, CannonballSpeed, CannonballTraectory, ScoreCounter: Byte;
- const
- START_POINT = -120;
- FINISH_POINT = 1000;
- implementation
- {$R *.dfm}
- procedure TSeaBattle.CannonballTimerTimer(Sender: TObject);
- begin
- case CannonballTraectory of // тут надо убрать 2 абзаца
- 1:
- begin
- Cannonball.Left := Cannonball.Left - (1 * CannonballSpeed);
- Cannonball.Top := Cannonball.Top - (2 * CannonballSpeed);
- end;
- 2:
- begin
- Cannonball.Left := Cannonball.Left - (1 * CannonballSpeed);
- Cannonball.Top := Cannonball.Top - (3 * CannonballSpeed);
- end;
- 3:
- begin
- Cannonball.Left := Cannonball.Left - (1 * CannonballSpeed);
- Cannonball.Top := Cannonball.Top - (5 * CannonballSpeed);
- end;
- 4:
- begin
- Cannonball.Top := Cannonball.Top - (2 * CannonballSpeed);
- end;
- 5:
- begin
- Cannonball.Left := Cannonball.Left + (1 * CannonballSpeed);
- Cannonball.Top := Cannonball.Top - (5 * CannonballSpeed);
- end;
- 6:
- begin
- Cannonball.Left := Cannonball.Left + (1 * CannonballSpeed);
- Cannonball.Top := Cannonball.Top - (3 * CannonballSpeed);
- end;
- 7:
- begin
- Cannonball.Left := Cannonball.Left + (1 * CannonballSpeed);
- Cannonball.Top := Cannonball.Top - (2 * CannonballSpeed);
- end;
- end;
- If Cannonball.Top < 144 then
- begin //хочу придумать красивое исчезновение шара
- SetCannonStartPosition();
- CannonballTimer.Enabled := False;
- end;
- end;
- procedure TSeaBattle.FormCreate(Sender: TObject);
- begin
- IsGameStarted := False;
- GunPosition := 4;
- Gun.Left := 475;
- CannonballSpeed := 1;
- ScoreCounter := 0;
- CannonLeft := Cannonball.Left;
- CannonTop := Cannonball.Top;
- end;
- procedure TSeaBattle.FormKeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- begin
- If (IsGameStarted and (Key = VK_LEFT) and (GunPosition = 1)) or not((Key = VK_LEFT) or (Key = VK_RIGHT)) then
- exit;
- Gun.Left := 475;
- If IsGameStarted and (Key = VK_LEFT) and (GunPosition > 1) then //left
- begin
- Dec(GunPosition);
- SetGunPosition();
- end;
- If IsGameStarted and (Key = VK_Right) and (GunPosition < 7) then //right
- begin
- Inc(GunPosition);
- SetGunPosition();
- end;
- end;
- procedure TSeaBattle.FormKeyPress(Sender: TObject; var Key: Char);
- begin
- If (Key = #32) and not(IsGameStarted) then
- begin
- OpenTheGame();
- StartGame();
- end;
- If IsGameStarted and (Key in ['0'..'9']) and not(CannonballTimer.Enabled) then
- begin
- CannonballSpeed := StrToint(Key) + 1;
- Info.Caption := 'Скорость ядра: ' + IntToStr(CannonballSpeed);
- end;
- If IsGameStarted and (Key = #13) then
- begin
- If not(CannonballTimer.Enabled) then
- begin
- CannonballTraectory := GunPosition;
- CannonballTimer.Enabled := True;
- Cannonball.Visible := True;
- end;//else "reload"
- end;
- end;
- procedure TSeaBattle.SetGunPosition();
- const
- LEFT10 = 30;
- LEFT20 = 55;
- LEFT30 = 80;
- Begin
- case GunPosition of
- 1:
- begin
- Gun.Left := Gun.Left - LEFT30;
- Gun.Picture.LoadFromFile('30l.png');
- end;
- 2:
- begin
- Gun.Left := Gun.Left - LEFT20;
- Gun.Picture.LoadFromFile('20l.png');
- end;
- 3:
- begin
- Gun.Left := Gun.Left - LEFT10;
- Gun.Picture.LoadFromFile('10l.png');
- end;
- 4:
- begin
- Gun.Picture.LoadFromFile('gun.png');
- end;
- 5:
- begin
- Gun.Picture.LoadFromFile('10r.png');
- end;
- 6:
- begin
- Gun.Picture.LoadFromFile('20r.png');
- end;
- 7:
- begin
- Gun.Picture.LoadFromFile('30r.png');
- end;
- end;
- If not(CannonballTimer.Enabled) then
- begin
- SetCannonStartPosition();
- end;
- End;
- procedure TSeaBattle.SetCannonStartPosition();
- begin
- Cannonball.Visible := False;
- case GunPosition of
- 1:
- begin
- Cannonball.Left := CannonLeft - 88;
- Cannonball.Top := CannonTop + 15;
- end;
- 2:
- begin
- Cannonball.Left := CannonLeft - 60;
- Cannonball.Top := CannonTop + 15;
- end;
- 3:
- begin
- Cannonball.Left := CannonLeft - 32;
- Cannonball.Top := CannonTop + 10;
- end;
- 4:
- begin
- Cannonball.Left := CannonLeft;
- Cannonball.Top := CannonTop;
- end;
- 5:
- begin
- Cannonball.Left := CannonLeft + 30;
- Cannonball.Top := CannonTop + 10;
- end;
- 6:
- begin
- Cannonball.Left := CannonLeft + 55;
- Cannonball.Top := CannonTop + 15;
- end;
- 7:
- begin
- Cannonball.Left := CannonLeft + 78;
- Cannonball.Top := CannonTop + 15;
- end;
- end;
- end;
- procedure TSeaBattle.Timer2secTimer(Sender: TObject);
- begin
- Explosion.Visible := False;
- Timer2sec.Enabled := False;
- end;
- procedure TSeaBattle.TimerTimer(Sender: TObject);
- begin
- Ship.Left := Ship.Left + Speed;
- If (Ship.Left >= (FINISH_POINT - 2)) and (Ship.Left <= (FINISH_POINT + 2)) then
- begin
- Ship.Left := START_POINT;
- Speed := RandomRange(1, 4);
- end;
- If ((Cannonball.Left < Ship.Left + 121) and (Cannonball.Left > Ship.Left) and (Cannonball.Top <= Ship.Top + 5)) then
- begin
- Explosion.Top := Ship.Top;
- Explosion.Left := Ship.Left;
- Ship.Left := START_POINT;
- Explosion.Visible := True;
- SetCannonStartPosition();
- Speed := RandomRange(1, 4);
- Timer.Enabled := False;
- CannonballTimer.Enabled := False;
- Inc(ScoreCounter);
- Score.Caption := 'Очки: ' + IntToStr(ScoreCounter);
- Timer2sec.Enabled := True;
- end;
- end;
- procedure TSeaBattle.OpenTheGame();
- begin
- Label1.Visible := False;
- Label2.Visible := False;
- Label3.Visible := False;
- Label4.Visible := False;
- Label5.Visible := False;
- Label6.Visible := False;
- Label7.Visible := False;
- Label8.Visible := False;
- Score.Visible := True;
- Info.Visible := True;
- Ship.Visible := True;
- Sea.Visible := True;
- Gun.Visible := True;
- end;
- procedure TSeaBattle.StartGame();
- begin
- Randomize();
- //Speed := 5;
- Speed := RandomRange(1, 4);
- Timer.Enabled := True;
- IsGameStarted := True;
- MediaPlayerMusic.FileName := 'music.mp3';
- MediaPlayerMusic.Enabled := True;
- MediaPlayerMusic.Play;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement