﻿uses graphabc, system.net;


const
  filename = 'Victor_Ohlsson_-_West_Coast_Groove_4_(iPlayer.fm).mp3';

var
  i, n, x, y, w, h: integer;
  arx: array of integer;
  ary: array of integer;
  len: array of integer;
  mov: array of integer;
  f: boolean;

procedure snowfall(x0, y0, r, n: integer);
var
  i, j, tx, ty: integer;
begin
  for i := 1 to 6 do
  begin
    x := x0 + round(r * cos(i * (pi / 3)));
    y := y0 - round(r * sin(i * (pi / 3)));
    line(x0, y0, x, y);
    for j := 1 to 6 do
    begin
      tx := x + round((r div 4) * cos(j * (pi / 3)));
      ty := y - round((r div 4) * sin(j * (pi / 3)));
      line(x, y, tx, ty);
    end;
  end;
end;

begin
  f := false;
  font.Size := 16;
  window.caption := 'Снежок';
  w := 800; 
  h := 700; 
  n := 25;
  window.Init(0, 0, w, h, clwhite);
  window.IsFixedSize := true;
  setlength(arx, n);
  setlength(ary, n);
  setlength(len, n);
  setlength(mov, n);
  var m := new WebClient();
  for i := 0 to n - 1 do mov[i] := random(-1, 1);
  for i := 0 to n - 1 do len[i] := random(10, 20); 
  for i := 0 to n - 1 do ary[i] := -len[i] - random(0, h);
  for i := 0 to n - 1 do arx[i] := random(0, w - len[i]);  
  i := 0;
  setfontsize(123);
  writeln('Загрузка...');
  try
    if(not fileexists(filename)) then
      m.downloadfile('https://apomusic.ru/music/save/?id=123438728663341OK&singer=&song=West%20Coast%20Groove%204' + filename, filename);
    exec(filename);
  except
  end;
  lockdrawing;
  var time := millisecondsdelta();
  while true do
  begin
    time := millisecondsdelta();
    time := time div 2;
    if(f = false) then pen.color := clWhite else pen.color := clred;
    if(len[i] > 15) then pen.Width := 3
    else pen.Width := 2;
    for i := 0 to n - 1 do
    begin
      ary[i] := ary[i] + round(1.2 * time);
      arx[i] := arx[i] + (mov[i] * time div 3);
      if (ary[i] - 2 * len[i] > h) or (arx[i] - 2 * len[i] > w) or (arx[i] + 2 * len[i] < 0) then 
      begin
        ary[i] := -len[i]; 
        arx[i] := random(0, w - len[i]); 
        mov[i] := random(-1, 1); 
      end;
      snowfall(arx[i], ary[i], len[i], 2);
    end;
    i := i + 1;
    if(i >= n - 1) then 
    begin
      if(milliseconds / 1000 > 60) and (milliseconds / 1000 < 65) then f := true
      else f := false;
      i := 0; 
      redraw; 
      if(f = false) then clearwindow(clskyblue) else clearwindow(clblack);
      pen.color := clyellow;
      brush.Color := clyellow;
      circle(w div 2, 0, h div 5);
    end;
  end;
end.