using System.Diagnostics; using System.Text; namespace Debug.MinLong; #pragma warning disable CA1822 // 将成员标记为 static #pragma warning disable IDE0051 // 删除未使用的私有成员 #pragma warning disable IDE1006 // 命名样式 public class Main { public Main() { // Console.WriteLine("MinLong"); ResetChars();//初始化 渲染界面 ResetLong();//初始化小龙 new Thread(PrintCharArray).Start(); var timer = new System.Timers.Timer(); // 设置计时器事件处理方法 timer.Elapsed += (sender, e) => { new Thread(() => Zhangaiwu.Newshitou(xiaoLong)).Start(); int interval = random.Next(3000, 6000); // 1000ms 到 5000ms timer.Interval = interval; }; // 启动计时器 timer.Start(); KeyThread();//监听键盘 } private long time; //随机数 Random random = new(); public void PrintCharArray() { var stopwatch = new Stopwatch(); int frameCount = 0; double totalTime = 0; int i; int j; int p = 0; 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(); xiaoLong.Char = xiaoLong.Char == '웃' ? '유' : '웃'; Program.chars[xiaoLong.y, xiaoLong.x].Char = xiaoLong.Char; 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++) { p = p == i % 2 ? 1 : 0; if (p == 0) { buffer.Append(' '); } else { buffer.Append('-'); } } 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 得分{xiaoLong.score}"); // 保持每秒约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 bool islong = false;//是否长按跳 //分数 public int score = 0; } 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); if (Program.IsWhile == false) { if (consoleKeyInfo.Key == ConsoleKey.Enter) { Console.Clear(); Program.IsWhile = true; _ = new MinLong.Main(); } return; } switch (consoleKeyInfo.Key) { //空格 case ConsoleKey.Spacebar: case ConsoleKey.PageUp: if (!xiaoLong.istiao) { new Thread(() => tiaoyue(xiaoLong)).Start(); break; } xiaoLong.islong = true; break; } } // 等待用户按下任意键后退出 }).Start(); } /// /// 跳跃 /// public static void tiaoyue(XiaoLong xiaoLong) { 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++) { if (xiaoLong.islong) { Thread.Sleep(50); } 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.islong = false; 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(); } } } }