using System.Diagnostics; using System.Text; namespace Debug.MinLong; #pragma warning disable CA1822 // 将成员标记为 static #pragma warning disable IDE0051 // 删除未使用的私有成员 #pragma warning disable IDE1006 // 命名样式 public class KongLong { public KongLong() { Console.WriteLine("MinLong"); Console.CursorVisible = false; ResetChars();//初始化 ResetLong();//初始化小龙 new Thread(PrintCharArray).Start(); KeyThread();//监听键盘 } private long time; public void PrintCharArray() { var stopwatch = new Stopwatch(); int frameCount = 0; double totalTime = 0; int i; int j; var a = Program.chars.GetLength(0); var b = Program.chars.GetLength(1); var buffer = new StringBuilder(); while (Program.IsWhile) { stopwatch.Restart(); Console.SetCursorPosition(0, 0);// 设定光标位置 Console.Clear(); buffer.Clear(); for (i = 0; i < a; i++) { for (j = 0; j < b; j++) { // Console.ForegroundColor = chars[i, j].Color; // Console.Write($" {chars[i, j].Char} "); buffer.Append(Program.chars[i, j].Char); time++; if (time > 100000) { GC.Collect(); time = 0; } } buffer.AppendLine(); } Console.ForegroundColor = ConsoleColor.White; for (i = 0; i < b; i++) { buffer.Append('Ξ'); } if (time % 1000 == 0) { new Thread(Zhangaiwu.newshitou).Start(); } Console.Write(buffer); Console.ResetColor(); // 停止计时并计算本帧的处理时间 stopwatch.Stop(); double elapsedMilliseconds = stopwatch.Elapsed.TotalMilliseconds; totalTime += elapsedMilliseconds; frameCount++; // 计算帧率 double fps = frameCount / (totalTime / 1000.0); Console.SetCursorPosition(0, 0);// 设定光标位置到第二行 Console.WriteLine($" {fps:0} FPS"); // 保持每秒约16.67ms(即大约60 FPS)的间隔 // Thread.Sleep(Math.Max(0, 60 - (int)elapsedMilliseconds)); //死方法 不知道为什么 控制台限制60 帧这样闪烁不会太严重 Thread.Sleep(100); } } public class XiaoLong { public char Char = '口';//小龙的形状 public ConsoleColor Color = ConsoleColor.Red;//小龙的颜色 public int x;//小龙X坐标 public int y;//小龙Y坐标 public bool istiao = false;//是否跳 } public XiaoLong xiaoLong = new(); int charsy = Program.chars.GetLength(0); /// /// 初始化小龙 /// public void ResetLong() { int x = Program.chars.GetLength(1); xiaoLong.x = x / 6; xiaoLong.y = charsy - 1; Program.chars[xiaoLong.y, xiaoLong.x].Char = xiaoLong.Char; } /// /// 全局键盘监听 /// public void KeyThread() { new Thread(() => { while (Program.IsWhile) { ConsoleKeyInfo consoleKeyInfo = Console.ReadKey(true); if (consoleKeyInfo.Key == ConsoleKey.Escape) { Program.IsWhile = false; Thread.Sleep(500); Console.Clear(); Environment.Exit(0); return; } // Console.WriteLine(consoleKeyInfo.Key); switch (consoleKeyInfo.Key) { //空格 case ConsoleKey.Spacebar: if (!xiaoLong.istiao) { new Thread(tiaoyue).Start(); } break; } } // 等待用户按下任意键后退出 }).Start(); } /// /// 跳跃 /// private void tiaoyue() { if (xiaoLong.istiao) return; xiaoLong.istiao = true; // Console.WriteLine("跳跃"); //设置离地高度 for (int i = 0; i < 3; i++) { Program.chars[xiaoLong.y, xiaoLong.x].Char = Program.ShowChar; xiaoLong.y -= 1; Program.chars[xiaoLong.y, xiaoLong.x].Char = xiaoLong.Char; Thread.Sleep(100); } for (int i = 0; i < 3; i++) { Program.chars[xiaoLong.y, xiaoLong.x].Char = Program.ShowChar; xiaoLong.y += 1; Program.chars[xiaoLong.y, xiaoLong.x].Char = xiaoLong.Char; Thread.Sleep(100); } xiaoLong.istiao = false; } public static void ResetChars() { for (int i = 0; i < Program.chars.GetLength(0); i++) { for (int j = 0; j < Program.chars.GetLength(1); j++) { Program.chars[i, j] = new ModelingMusic.Point(); } } } }