Advertisement
Vladislav8653

Untitled

Jul 18th, 2023 (edited)
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 8.33 KB | None | 0 0
  1. unit MainUnit;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.Imaging.pngimage,
  8.   Vcl.Imaging.jpeg, Vcl.StdCtrls, Math, Vcl.MPlayer;
  9.  
  10. type
  11.   TSeaBattle = class(TForm)
  12.     Sea: TImage;
  13.     Gun: TImage;
  14.     Timer: TTimer;
  15.     Ship: TImage;
  16.     Label1: TLabel;
  17.     Label2: TLabel;
  18.     Label3: TLabel;
  19.     Label4: TLabel;
  20.     Label5: TLabel;
  21.     Label6: TLabel;
  22.     Label7: TLabel;
  23.     Label8: TLabel;
  24.     Cannonball: TImage;
  25.     CannonballTimer: TTimer;
  26.     Explosion: TImage;
  27.     Info: TLabel;
  28.     Score: TLabel;
  29.     MediaPlayerMusic: TMediaPlayer;
  30.     Timer2sec: TTimer;
  31.     procedure TimerTimer(Sender: TObject);
  32.     procedure FormKeyPress(Sender: TObject; var Key: Char);
  33.     procedure FormCreate(Sender: TObject);
  34.     procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  35.     procedure CannonballTimerTimer(Sender: TObject);
  36.     procedure Timer2secTimer(Sender: TObject);
  37.   private
  38.     { Private declarations }
  39.     procedure OpenTheGame;
  40.     procedure StartGame;
  41.     procedure SetGunPosition;
  42.     procedure SetCannonStartPosition;
  43.   public
  44.     { Public declarations }
  45.   end;
  46.  
  47. var
  48.     SeaBattle: TSeaBattle;
  49.     IsGameStarted : Boolean;
  50.     Speed, CannonLeft, CannonTop : Integer;
  51.     GunPosition, NormalGunPosition, CannonballSpeed, CannonballTraectory, ScoreCounter: Byte;
  52.  
  53. const
  54.     START_POINT = -120;
  55.     FINISH_POINT = 1000;
  56.  
  57. implementation
  58.  
  59. {$R *.dfm}
  60.  
  61. procedure TSeaBattle.CannonballTimerTimer(Sender: TObject);
  62. begin
  63.     case CannonballTraectory of   // тут надо убрать 2 абзаца
  64.         1:
  65.         begin
  66.             Cannonball.Left := Cannonball.Left - (1 * CannonballSpeed);
  67.             Cannonball.Top := Cannonball.Top - (2 * CannonballSpeed);
  68.         end;
  69.         2:
  70.         begin
  71.             Cannonball.Left := Cannonball.Left - (1 * CannonballSpeed);
  72.             Cannonball.Top := Cannonball.Top - (3 * CannonballSpeed);
  73.         end;
  74.         3:
  75.         begin
  76.             Cannonball.Left := Cannonball.Left - (1 * CannonballSpeed);
  77.             Cannonball.Top := Cannonball.Top - (5 * CannonballSpeed);
  78.         end;
  79.         4:
  80.         begin
  81.             Cannonball.Top := Cannonball.Top - (2 * CannonballSpeed);
  82.         end;
  83.         5:
  84.         begin
  85.             Cannonball.Left := Cannonball.Left + (1 * CannonballSpeed);
  86.             Cannonball.Top := Cannonball.Top - (5 * CannonballSpeed);
  87.         end;
  88.         6:
  89.         begin
  90.             Cannonball.Left := Cannonball.Left + (1 * CannonballSpeed);
  91.             Cannonball.Top := Cannonball.Top - (3 * CannonballSpeed);
  92.         end;
  93.         7:
  94.         begin
  95.             Cannonball.Left := Cannonball.Left + (1 * CannonballSpeed);
  96.             Cannonball.Top := Cannonball.Top - (2 * CannonballSpeed);
  97.         end;
  98.     end;
  99.     If Cannonball.Top < 144 then
  100.     begin     //хочу придумать красивое исчезновение шара
  101.         SetCannonStartPosition();
  102.         CannonballTimer.Enabled := False;
  103.     end;
  104. end;
  105.  
  106. procedure TSeaBattle.FormCreate(Sender: TObject);
  107. begin
  108.     IsGameStarted := False;
  109.     GunPosition := 4;
  110.     Gun.Left := 475;
  111.     CannonballSpeed := 1;
  112.     ScoreCounter := 0;
  113.     CannonLeft := Cannonball.Left;
  114.     CannonTop := Cannonball.Top;
  115. end;
  116.  
  117. procedure TSeaBattle.FormKeyDown(Sender: TObject; var Key: Word;
  118.   Shift: TShiftState);
  119. begin
  120.     If (IsGameStarted and (Key = VK_LEFT) and (GunPosition = 1)) or not((Key = VK_LEFT) or (Key = VK_RIGHT))  then
  121.         exit;
  122.     Gun.Left := 475;
  123.     If IsGameStarted and (Key = VK_LEFT) and (GunPosition > 1) then //left
  124.     begin
  125.         Dec(GunPosition);
  126.         SetGunPosition();
  127.     end;
  128.     If IsGameStarted and (Key = VK_Right) and (GunPosition < 7) then //right
  129.     begin
  130.         Inc(GunPosition);
  131.         SetGunPosition();
  132.     end;
  133. end;
  134.  
  135. procedure TSeaBattle.FormKeyPress(Sender: TObject; var Key: Char);
  136. begin
  137.     If (Key = #32) and not(IsGameStarted) then
  138.     begin
  139.         OpenTheGame();
  140.         StartGame();
  141.     end;
  142.     If IsGameStarted and (Key in ['0'..'9']) and not(CannonballTimer.Enabled) then
  143.     begin
  144.         CannonballSpeed := StrToint(Key) + 1;
  145.         Info.Caption := 'Скорость ядра: ' + IntToStr(CannonballSpeed);
  146.     end;
  147.     If IsGameStarted and (Key = #13) then
  148.     begin
  149.         If not(CannonballTimer.Enabled) then
  150.         begin
  151.             CannonballTraectory := GunPosition;
  152.             CannonballTimer.Enabled := True;
  153.             Cannonball.Visible := True;
  154.         end;//else "reload"
  155.     end;
  156. end;
  157.  
  158. procedure TSeaBattle.SetGunPosition();
  159. const
  160.      LEFT10 = 30;
  161.      LEFT20 = 55;
  162.      LEFT30 = 80;
  163. Begin
  164.     case GunPosition of
  165.         1:
  166.         begin
  167.             Gun.Left := Gun.Left - LEFT30;
  168.             Gun.Picture.LoadFromFile('30l.png');
  169.         end;
  170.         2:
  171.         begin
  172.             Gun.Left := Gun.Left - LEFT20;
  173.             Gun.Picture.LoadFromFile('20l.png');
  174.         end;
  175.         3:
  176.         begin
  177.             Gun.Left := Gun.Left - LEFT10;
  178.             Gun.Picture.LoadFromFile('10l.png');
  179.         end;
  180.         4:
  181.         begin
  182.             Gun.Picture.LoadFromFile('gun.png');
  183.         end;
  184.         5:
  185.         begin
  186.             Gun.Picture.LoadFromFile('10r.png');
  187.         end;
  188.         6:
  189.         begin
  190.             Gun.Picture.LoadFromFile('20r.png');
  191.         end;
  192.         7:
  193.         begin
  194.             Gun.Picture.LoadFromFile('30r.png');
  195.         end;
  196.     end;
  197.     If not(CannonballTimer.Enabled) then
  198.     begin
  199.         SetCannonStartPosition();
  200.     end;
  201. End;
  202.  
  203. procedure TSeaBattle.SetCannonStartPosition();
  204. begin
  205.     Cannonball.Visible := False;
  206.     case GunPosition of
  207.         1:
  208.         begin
  209.             Cannonball.Left := CannonLeft - 88;
  210.             Cannonball.Top := CannonTop + 15;
  211.         end;
  212.         2:
  213.         begin
  214.             Cannonball.Left := CannonLeft - 60;
  215.             Cannonball.Top := CannonTop + 15;
  216.         end;
  217.         3:
  218.         begin
  219.             Cannonball.Left := CannonLeft - 32;
  220.             Cannonball.Top := CannonTop + 10;
  221.         end;
  222.         4:
  223.         begin
  224.             Cannonball.Left := CannonLeft;
  225.             Cannonball.Top := CannonTop;
  226.         end;
  227.         5:
  228.         begin
  229.             Cannonball.Left := CannonLeft + 30;
  230.             Cannonball.Top := CannonTop + 10;
  231.         end;
  232.         6:
  233.         begin
  234.             Cannonball.Left := CannonLeft + 55;
  235.             Cannonball.Top := CannonTop + 15;
  236.         end;
  237.         7:
  238.         begin
  239.             Cannonball.Left := CannonLeft + 78;
  240.             Cannonball.Top := CannonTop + 15;
  241.         end;
  242.     end;
  243. end;
  244.  
  245. procedure TSeaBattle.Timer2secTimer(Sender: TObject);
  246. begin
  247.     Explosion.Visible := False;
  248.     Timer2sec.Enabled := False;
  249. end;
  250.  
  251. procedure TSeaBattle.TimerTimer(Sender: TObject);
  252. begin
  253.     Ship.Left := Ship.Left + Speed;
  254.     If (Ship.Left >= (FINISH_POINT - 2)) and (Ship.Left <= (FINISH_POINT + 2)) then
  255.     begin
  256.         Ship.Left := START_POINT;
  257.         Speed := RandomRange(1, 4);
  258.     end;
  259.     If ((Cannonball.Left < Ship.Left + 121) and (Cannonball.Left > Ship.Left) and (Cannonball.Top <= Ship.Top + 5)) then
  260.     begin
  261.         Explosion.Top := Ship.Top;
  262.         Explosion.Left := Ship.Left;
  263.         Ship.Left := START_POINT;
  264.         Explosion.Visible := True;
  265.         SetCannonStartPosition();
  266.         Speed := RandomRange(1, 4);
  267.         Timer.Enabled := False;
  268.         CannonballTimer.Enabled := False;
  269.         Inc(ScoreCounter);
  270.         Score.Caption := 'Очки: ' + IntToStr(ScoreCounter);
  271.         Timer2sec.Enabled := True;
  272.     end;
  273. end;
  274.  
  275. procedure TSeaBattle.OpenTheGame();
  276. begin
  277.     Label1.Visible := False;
  278.     Label2.Visible := False;
  279.     Label3.Visible := False;
  280.     Label4.Visible := False;
  281.     Label5.Visible := False;
  282.     Label6.Visible := False;
  283.     Label7.Visible := False;
  284.     Label8.Visible := False;
  285.     Score.Visible := True;
  286.     Info.Visible := True;
  287.     Ship.Visible := True;
  288.     Sea.Visible := True;
  289.     Gun.Visible := True;
  290. end;
  291.  
  292. procedure TSeaBattle.StartGame();
  293. begin
  294.     Randomize();
  295.     //Speed := 5;
  296.     Speed := RandomRange(1, 4);
  297.     Timer.Enabled := True;
  298.     IsGameStarted := True;
  299.     MediaPlayerMusic.FileName := 'music.mp3';
  300.     MediaPlayerMusic.Enabled := True;
  301.     MediaPlayerMusic.Play;
  302. end;
  303.  
  304. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement