写个小程序 喜迎.NET 5.0 ~

.NET5.0在双十一正式发布,这是.NET Core的后续版本,带来了包括高性能、C#9.0、Blazor WebAssembly等新特性。为了庆祝,本文分享了一个使用C#编写的创意控制台应用程序,该程序不仅展示了一颗跳动的心形图案,还播放了简单的音乐。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

.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.SetWindowSize(ScrW, ScrH + 1); Console.SetBufferSize(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)(880 * 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, int.MaxValue, '.'),
				(new Point[1], (fId, pId) => Enumerable.Range(0, (heartH + 1) * heartDef.Length * ((int)(Math.Sin(fId / 2.5D - Math.PI / 2) * 50) + 50) / 90)
						.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, int.MaxValue, ':'),
				(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, int.MaxValue, '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(60);
				}
			}

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

效果大概是这样的😛

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值