Advertisement
believe_me

Untitled

Apr 8th, 2022
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 8.59 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.Menus, Vcl.ExtCtrls, System.Math, System.Uitypes;
  8.  
  9. type
  10.   TMainForm = class(TForm)
  11.     MainImage: TImage;
  12.     AnimationTimer: TTimer;
  13.     procedure AnimationTimerTimer(Sender: TObject);
  14.     procedure FormShow(Sender: TObject);
  15.     procedure FormKeyPress(Sender: TObject; var Key: Char);
  16.   private
  17.     { Private declarations }
  18.   public
  19.     procedure drawRings();
  20.     procedure drawCircles();
  21.     procedure restart();
  22.     procedure clearCircles();
  23.     function isEnd(): boolean;
  24.     procedure writeEndLabel();
  25.     procedure writeTopListLabel();
  26.     procedure writePositionNumber(Index: integer; NumberOfCircle: integer);
  27.     procedure writeRestartLabel();
  28.     function findLastPosition(): integer;
  29.   end;
  30.  
  31. var
  32.   MainForm: TMainForm;
  33.  
  34. implementation
  35.  
  36. {$R *.dfm}
  37.  
  38.  
  39. const
  40.     XCentreCoord = 400;
  41.     YCentreCoord = 375;
  42.     NumberOfRings = 8;
  43.     BiggestRingRadius = 340;
  44.     BiggestCircleRadius = 38;
  45.     NumberOfCircles = 8;
  46.     NumberOfParams = 6;
  47.     MaxWidth = 1200;
  48.     MaxHeight = 800;
  49.     XTextOffset = 800;
  50.     VerticalOffset = 30;
  51.     HorizontalOffset = 40;
  52.  
  53. type
  54.     TArrayOfParams = array[1..NumberOfCircles] of array[1..NumberOfParams] of integer;
  55.  
  56. var
  57.     IsPlaying: boolean = false;
  58.     ArrayOfParams: TArrayOfParams;
  59.     Angle: double = 0;
  60.  
  61.  
  62. procedure TMainForm.AnimationTimerTimer(Sender: TObject);
  63.  
  64. begin
  65.     if IsPlaying then
  66.     begin
  67.         clearCircles();
  68.         Angle := Angle + 0.02;
  69.         drawRings();
  70.         writeTopListLabel();
  71.         drawCircles();
  72.         IsPlaying := not (isEnd());
  73.         writeRestartLabel()
  74.     end
  75.     else
  76.     begin
  77.         AnimationTimer.Enabled := false;
  78.         writeEndLabel();
  79.         writeRestartLabel()
  80.     end;
  81.  
  82. end;
  83.  
  84. procedure TMainForm.drawRings();
  85.  
  86. var
  87.     i, CurrentRadius: integer;
  88.  
  89. begin
  90.     CurrentRadius := BiggestRingRadius;
  91.  
  92.     for i := NumberOfRings downto 1 do
  93.     begin
  94.         with MainForm.MainImage do
  95.         begin
  96.             Canvas.Pen.Color := clBlack;
  97.             Canvas.Pen.Width := 3;
  98.             Canvas.Brush.Color := clSilver;
  99.             Canvas.Ellipse(XCentreCoord - CurrentRadius, YCentreCoord - CurrentRadius,
  100.                            XCentreCoord + CurrentRadius, YCentreCoord + CurrentRadius);
  101.         end;
  102.         CurrentRadius := trunc(CurrentRadius / 1.3) - 10;
  103.     end;
  104. end;
  105.  
  106.  
  107. procedure TMainForm.FormKeyPress(Sender: TObject; var Key: Char);
  108.  
  109. begin
  110.     if key = #13 then
  111.     begin
  112.         Angle := 0;
  113.         clearCircles();
  114.         restart();
  115.         AnimationTimer.Enabled := true;
  116.         IsPlaying := true;
  117.     end;
  118. end;
  119.  
  120. procedure TMainForm.FormShow(Sender: TObject);
  121.  
  122. begin
  123.     Angle := 0;
  124.     clearCircles();
  125.     restart();
  126.     AnimationTimer.Enabled := true;
  127.     IsPlaying := true;
  128. end;
  129.  
  130. procedure TMainForm.drawCircles();
  131.  
  132. var
  133.     i, XCoord, YCoord, Index: integer;
  134.     Coefficient, CurrentAngle: double;
  135.  
  136. begin
  137.     for i := NumberOfRings downto 1 do
  138.     begin
  139.         with MainForm.MainImage do
  140.         begin
  141.             case i of
  142.                 1: Canvas.Brush.Color := clRed;
  143.                 2: Canvas.Brush.Color := clMaroon;
  144.                 3: Canvas.Brush.Color := clYellow;
  145.                 4: Canvas.Brush.Color := clLime;
  146.                 5: Canvas.Brush.Color := clAqua;
  147.                 6: Canvas.Brush.Color := clBlue;
  148.                 7: Canvas.Brush.Color := clFuchsia;
  149.                 8: Canvas.Brush.Color := clBlack;
  150.             end;
  151.             Canvas.Pen.Color := clWhite;
  152.             Canvas.Pen.Width := 0;
  153.             Coefficient := ArrayOfParams[i][4] / 20;
  154.             CurrentAngle := Coefficient * Angle;
  155.             XCoord := trunc(XCentreCoord + ArrayOfParams[i][1] * cos(CurrentAngle));
  156.             YCoord := trunc(YCentreCoord + ArrayOfParams[i][1] * sin(CurrentAngle));
  157.             if (CurrentAngle > 2*Pi) then
  158.             begin
  159.                 if (ArrayOfParams[i][3] = 0) then
  160.                 begin
  161.                     Index := findLastPosition();
  162.                     ArrayOfParams[i][3] := Index;
  163.                 end;
  164.                 Canvas.Ellipse(XCentreCoord + ArrayOfParams[i][1] - ArrayOfParams[i][2],
  165.                                YCentreCoord - ArrayOfParams[i][2],
  166.                                XCentreCoord + ArrayOfParams[i][1] + ArrayOfParams[i][2],
  167.                                YCentreCoord + ArrayOfParams[i][2]);
  168.                 Canvas.Ellipse(XTextOffset + 60 + HorizontalOffset - ArrayOfParams[i][2],
  169.                                trunc(1.85 * ArrayOfParams[8][2] * (ArrayOfParams[i][3] + 1)) - 35 - ArrayOfParams[i][2],
  170.                                XTextOffset + 60 + HorizontalOffset + ArrayOfParams[i][2],
  171.                                trunc(1.85 * ArrayOfParams[8][2] * (ArrayOfParams[i][3] + 1)) - 35 + ArrayOfParams[i][2]);
  172.                 writePositionNumber(ArrayOfParams[i][3], i);
  173.             end
  174.             else
  175.                 Canvas.Ellipse(XCoord - ArrayOfParams[i][2], YCoord - ArrayOfParams[i][2],
  176.                                XCoord + ArrayOfParams[i][2], YCoord + ArrayOfParams[i][2]);
  177.         end;
  178.     end;
  179. end;
  180.  
  181.  
  182. procedure TMainForm.restart();
  183.  
  184. var
  185.     i, j, CurrentRingRadius, CurrentCircleRadius: integer;
  186.  
  187. begin
  188.     CurrentRingRadius := BiggestRingRadius;
  189.     CurrentCircleRadius := BiggestCircleRadius;
  190.  
  191.     for i := NumberOfCircles downto 1 do
  192.     begin
  193.         for j := 1 to NumberOfParams do
  194.         begin
  195.             case j of
  196.                 1: ArrayOfParams[i][j] := CurrentRingRadius;
  197.                 2: ArrayOfParams[i][j] := CurrentCircleRadius;
  198.                 3: ArrayOfParams[i][j] := 0;
  199.                 4:
  200.                 begin
  201.                     Randomize;
  202.                     ArrayOfParams[i][j] := Random(20) + 2;
  203.                 end;
  204.             end;
  205.         end;
  206.  
  207.         CurrentRingRadius := trunc(CurrentRingRadius / 1.3) - 10;
  208.         CurrentCircleRadius := trunc(CurrentCircleRadius / 1.2);
  209.     end;
  210. end;
  211.  
  212.  
  213. function TMainForm.isEnd(): boolean;
  214.  
  215. var
  216.     i: integer;
  217.     isEnd: boolean;
  218.  
  219. begin
  220.     IsEnd := true;
  221.  
  222.     for i := 1 to NumberOfCircles do
  223.         if ArrayOfParams[i][3] = 0 then
  224.             isEnd := false;
  225.  
  226.      Result := isEnd;
  227. end;
  228.  
  229.  
  230. procedure TMainForm.writePositionNumber(Index: integer; NumberOfCircle: integer);
  231.  
  232. begin
  233.     with MainForm.MainImage do
  234.         begin
  235.             Canvas.Font.Color := clBlack;
  236.             Canvas.Brush.Color := clSilver;
  237.             Canvas.Font.Size := 14;
  238.             Canvas.textOut(XTextOffset + 15, trunc(1.85 * ArrayOfParams[8][2] * (Index + 1)) - 50,
  239.                            intToStr(Index) + ': ');
  240.         end;
  241. end;
  242.  
  243.  
  244. function TMainForm.findLastPosition(): integer;
  245.  
  246. var
  247.     i, MaxIndex: integer;
  248.  
  249. begin
  250.     MaxIndex := 0;
  251.  
  252.     for i := NumberOfCircles downto 1 do
  253.         if ArrayOfParams[i][3] > MaxIndex then
  254.             MaxIndex := ArrayOfParams[i][3];
  255.     inc(MaxIndex);
  256.  
  257.     Result := MaxIndex;
  258. end;
  259.  
  260.  
  261.  
  262. procedure TMainForm.writeEndLabel();
  263.  
  264. begin
  265.     with MainForm.MainImage do
  266.         begin
  267.             Canvas.Font.Color := clBlue;
  268.             Canvas.Brush.Color := clSilver;
  269.             Canvas.Font.Size := 15;
  270.             Canvas.textOut(XTextOffset - 100, 650, 'Все фигуры достигли финиша!');
  271.         end;
  272. end;
  273.  
  274.  
  275. procedure TMainForm.writeTopListLabel();
  276.  
  277. begin
  278.     with MainForm.MainImage do
  279.         begin
  280.             Canvas.Font.Color := clRed;
  281.             Canvas.Brush.Color := clSilver;
  282.             Canvas.Pen.Color := clPurple;
  283.             Canvas.Pen.Width := 7;
  284.             Canvas.Font.Size := 15;
  285.             Canvas.textOut(XTextOffset, 10, 'Список прибывших:');
  286.             Canvas.Brush.Color := clBlue;
  287.             Canvas.MoveTo(XTextOffset - 30, 50);
  288.             Canvas.LineTo(XTextOffset + 250, 50);
  289.         end;
  290. end;
  291.  
  292.  
  293. procedure TMainForm.writeRestartLabel();
  294.  
  295. begin
  296.     with MainForm.MainImage do
  297.     begin
  298.         Canvas.Font.Color := clGreen;
  299.         Canvas.Brush.Color := clSilver;
  300.         Canvas.Pen.Color := clPurple;
  301.         Canvas.Pen.Width := 7;
  302.         Canvas.Font.Size := 15;
  303.         Canvas.textOut(XTextOffset, 700, '[Enter] - начать заново.');
  304.     end;
  305.  
  306. end;
  307.  
  308. procedure TMainForm.clearCircles();
  309.  
  310. begin
  311.     with MainForm.MainImage do
  312.         begin
  313.             Canvas.Pen.Color := clWhite;
  314.             Canvas.Pen.Width := 3;
  315.             Canvas.Brush.Color := clSilver;
  316.             Canvas.Rectangle(0, 0, MaxWidth, MaxHeight);
  317.         end;
  318. end;
  319.  
  320.  
  321. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement