寫個小程式 喜迎.NET 5.0 ~

淡藍色2發表於2020-11-12

.net5.0正式釋出了,而且是在一個很瘋狂的日子:雙十一!?

這是一個全新的革命性的新平臺,免費、開源、跨平臺、全技術棧,它是.net Core的後續版本。

.NET 5帶來了諸多絕佳的新特性:

  • 高吞吐量和高效的生產力
  • 快速啟動,低記憶體佔用
  • 增強的C#9.0和F#5.0程式語言
  • 效能顯著提升的Blazor WebAssembly !
  • 增強的Entity Framework Core 5.0
  • ……

更多的新特性不再贅述,興奮之餘,寫個小程式慶祝一下✨?

新建一個.net Core Consol專案,把下面程式碼複製貼上到專案中執行。開啟電腦的揚聲器,有音樂的哦?

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace HelloNet5
{
	class Program
	{
		static void Main(string[] args)
		{
			const int ScrW = 120, ScrH = 40, PixelN = ScrH * ScrW;
			Console.SetBufferSize(ScrW, ScrH + 1); Console.SetWindowSize(ScrW, ScrH + 1);
			Console.SetCursorPosition((ScrW - 20) / 2, ScrH / 2 - 1); Console.Write("按任意鍵開始 ...");
			Console.SetCursorPosition((ScrW - 30) / 2, ScrH / 2); Console.Write("Press any key to start ...");
			Console.ReadKey();

			var bitmap = Convert.FromBase64String("//8ICAgI//8AAHh41JSUlJSYGAAAgIH//4CAAACAgf//gIAAAHh4h" +
									"ISEhHh4YGBgAAB/fwYMGDB/fwAAf39JSUlJSQAAAQEBf38BAQEAAACAj4+PicnZeXkx");
			var heart = Convert.FromBase64String("BSQzQ1JiYnFxgYGRkJCgoKCxsbHBwcHC0tLT0+Q=");
			var musicScore = Convert.FromBase64String("EBAiICVEEBAiICdFEBAsKSUkIhoaKSUnhQ==");
			Task.Run(() => musicScore.ToList().ForEach(it =>
				Console.Beep((int)(440 * Math.Pow(2, (it & 0xF) / 12.0)), 230 * ((it & 0xF0) >> 4))));


			var heartDef = heart.Concat(heart.Reverse()).Select(it => new Size(it & 0xF, ((it & 0xF0) >> 4) + 9)).ToArray();
			int heartH = heartDef.Max(it => it.Height) - heartDef.Min(it => it.Width), heartT = (ScrH - heartH) / 2;
			int heartL = (ScrW - heartDef.Length) / 2, heartR = heartL + heartDef.Length - 1;

			var effPoints = Enumerable.Range(0, bitmap.Length)
				.SelectMany(X => Enumerable.Range(0, 8).Select(Y => new Point(X, Y)))
				.Where(pt => ((bitmap[pt.X] >> pt.Y) & 1) == 1);

			Point[] pts1 = effPoints.Where(p => p.X < 45).ToArray(), pts2 = effPoints.Where(p => p.X >= 45).ToArray();
			var rnd = new Random();
			var rnd2 = pts2.Select(it => rnd.NextDouble()).ToArray();

			static Point Trans(Point inP, double offX, double offY) => new Point(inP.X + (int)offX, inP.Y + (int)offY);
			static Point Rota(Point p0, double ag, double r) => Trans(p0, Math.Cos(ag) * r, Math.Sin(ag) * r);
			static int Cube(int x, double xScale, double yScale) => (int)(Math.Pow(x * xScale, 3) * yScale);

			Task.Run(() => Render(
				(new Point[1], (fId, pId) =>
				{
					var rnd3 = new Random(fId / 5);
					return Enumerable.Range(0, Math.Min(fId / 2 + 2, ScrW * ScrH / 80))
						.Select(it => new Point(rnd3.Next(0, ScrW), rnd3.Next(0, ScrH)));
				}, 1, 300, '.'),
				(new Point[1], (fId, pId) => Enumerable.Range(0, (heartH + 1) * heartDef.Length / 90 * Math.Min((int)Math.Pow(fId * 0.6, 3), 99))
						.Select(it => new Point(rnd.Next(heartL, heartR + 1), rnd.Next(heartT, heartT + heartH + 1)))
						.Where(it => it.Y - heartT >= heartDef[it.X - heartL].Width && it.Y - heartT <= heartDef[it.X - heartL].Height)
					, 105, 300, ':'),
				(pts1, (fId, pId) => new[] { Trans(pts1[pId], Cube(fId - 25, 14, 2e-6) + (ScrW - 45) / 2, ScrH / 3) }, 5, 55, 'O'),
				(pts2, (fId, pId) => new[] { Trans(pts2[pId], Cube(25 - fId, 14, 2e-6)-9, ScrH / 2 + 2) }, 5, 55, 'E'),
				(pts2, (fId, pId) => new[] { Trans(Rota(pts2[pId], rnd2[pId] * 2 * Math.PI + (60 - fId) / 8.0,
					(rnd2[pId] * 0.5 + 0.1) * Math.Pow((60 - (fId >60 ? 60 : fId)) / 5.0, 2.5)), -8, ScrH / 3 + 2) }, 55, 300, 'N')
				));

			void Render(params (Point[] ps, Func<int, int, IEnumerable<Point>> Opt, int frmId1, int frmId2, char ch)[] anims)
			{
				for (int frameId = 0; anims.Where(o => frameId <= o.frmId2).Any(); frameId++)
				{
					var displayBuf = new string((char)32, PixelN).ToCharArray();
					anims.Where(it => frameId >= it.frmId1 && frameId <= it.frmId2).ToList()
						.ForEach(ani => ani.ps.SelectMany((p, id) => ani.Opt(frameId - ani.frmId1, id))
							.Where(pt => pt.X >= 0 && pt.X < ScrW && pt.Y >= 0 && pt.Y < ScrH).ToList()
							.ForEach(pt => displayBuf[pt.Y * ScrW + pt.X] = ani.ch));

					Console.SetCursorPosition(0, 0); Console.WriteLine(displayBuf);
					Thread.Sleep(70);
				}
			}

			Console.SetCursorPosition(0, ScrH);
			Console.Write("  Press any key to exit .."); Console.ReadKey();
		}
	}
}

效果大概是這樣的?

 

相關文章