From ac00035e31762d26c0154baa9f4ea7f62ea1b5e0 Mon Sep 17 00:00:00 2001 From: muqing <1966944300@qq.com> Date: Sat, 4 Jan 2025 19:26:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A8=A1=E6=8B=9F=E9=9F=B3=E6=B8=B81.5=20by?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ModeSwitch.cs | 109 ++++++++++++++++++++------------- ModelingMusic/Music.cs | 15 +++-- ModelingMusic/MusicDebug.cs | 9 --- ModelingMusic/MusicForm.cs | 6 -- ModelingMusic/MusicFrom.cs | 117 ++++++++++++++++++++++++++++++++++++ Program.cs | 11 ++-- README.md | 15 +++-- pyDebug.py | 5 -- 8 files changed, 207 insertions(+), 80 deletions(-) delete mode 100644 ModelingMusic/MusicForm.cs create mode 100644 ModelingMusic/MusicFrom.cs delete mode 100644 pyDebug.py diff --git a/ModeSwitch.cs b/ModeSwitch.cs index 0bf611a..8042e79 100644 --- a/ModeSwitch.cs +++ b/ModeSwitch.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection.Metadata; using System.Text; using System.Threading.Tasks; @@ -10,58 +11,80 @@ public class ModeSwitch { public event Action? Enter; private readonly string[] str = ["退出"]; - private readonly int mode = 0; - public ModeSwitch(string title, string[] a, Action Enter) + private int mode = 0; + // private bool zhuyi = false;//是否显示提醒词 + public ModeSwitch(string title, string[] a, Action Enter) : this(title, a, Enter, string.Empty) { + } + public ModeSwitch(string title, string[] a, Action Enter, string message) + { + this.message = message; this.Enter = Enter; str = a; - PrintCentered(title); - int windowWidth = Console.WindowWidth; - for (int i = 0; i < str.Length; i++) + new Thread(() => { - // string paddedString = str[i].PadLeft((windowWidth + str[i].Length) / 2).PadRight(windowWidth); - // Console.WriteLine(paddedString); - PrintCentered($"{str[i]}"); - } - do - { - var A1 = CalculateActualLength($"{mode}.{str[mode]}"); - Console.SetCursorPosition(windowWidth / 2 - A1, mode + 1); - Console.Write("<<"); - Console.SetCursorPosition(windowWidth / 2 + A1 - 2, mode + 1); - Console.Write(">>"); - var key = Console.ReadKey(true); - Console.SetCursorPosition(windowWidth / 2 - A1, mode + 1); - Console.Write(" "); - Console.SetCursorPosition(windowWidth / 2 + A1 - 2, mode + 1); - Console.Write(" "); - // Console.Write(key.Key); - if (key.Key >= ConsoleKey.D0 && key.Key <= ConsoleKey.D9) + // Thread logic here + PrintCentered(title); + int windowWidth = Console.WindowWidth; + for (int i = 0; i < str.Length; i++) { - int v1 = int.Parse(key.KeyChar.ToString()); - if (v1 >= 0 && v1 < str.Length) - mode = v1; + // string paddedString = str[i].PadLeft((windowWidth + str[i].Length) / 2).PadRight(windowWidth); + // Console.WriteLine(paddedString); + PrintCentered($"{str[i]}"); } - else - switch (key.Key) + // 判断message是不是为空 + + if (message != string.Empty) + { + for (int i = 0; i < windowWidth; i++) { - case ConsoleKey.UpArrow: - case ConsoleKey.W: - mode--; - break; - case ConsoleKey.S: - case ConsoleKey.DownArrow: - mode++; - break; - case ConsoleKey.Enter: - Enter?.Invoke(mode); - return; - default: - break; + Console.Write('-'); } - mode = int.Max(0, int.Min(str.Length - 1, mode)); - } while (true); + Console.WriteLine(); + PrintCentered("按W/S或↑/↓选择或数字键选择,按Enter确定"); + Console.Write(message); + } + do + { + var A1 = CalculateActualLength($"{mode}.{str[mode]}"); + Console.SetCursorPosition(windowWidth / 2 - A1, mode + 1); + Console.Write("<<"); + Console.SetCursorPosition(windowWidth / 2 + A1 - 2, mode + 1); + Console.Write(">>"); + var key = Console.ReadKey(true); + Console.SetCursorPosition(windowWidth / 2 - A1, mode + 1); + Console.Write(" "); + Console.SetCursorPosition(windowWidth / 2 + A1 - 2, mode + 1); + Console.Write(" "); + // Console.Write(key.Key); + if (key.Key >= ConsoleKey.D0 && key.Key <= ConsoleKey.D9) + { + int v1 = int.Parse(key.KeyChar.ToString()); + if (v1 >= 0 && v1 < str.Length) + mode = v1; + } + else + switch (key.Key) + { + case ConsoleKey.UpArrow: + case ConsoleKey.W: + mode--; + break; + case ConsoleKey.S: + case ConsoleKey.DownArrow: + mode++; + break; + case ConsoleKey.Enter: + Enter?.Invoke(mode); + return; + default: + break; + } + mode = int.Max(0, int.Min(str.Length - 1, mode)); + } while (true); + }).Start(); } + private string message = ""; private static int CalculateActualLength(string text) { Encoding encoding = Encoding.UTF8; diff --git a/ModelingMusic/Music.cs b/ModelingMusic/Music.cs index 0f4ea92..55a6549 100644 --- a/ModelingMusic/Music.cs +++ b/ModelingMusic/Music.cs @@ -1,18 +1,19 @@ using System.Diagnostics; using System.Text; +using ModelingMusic; namespace Debug.ModelingMusic; public class Music { - + public static string title = "模拟音游 1.5 by"; //选择的模式 private static int mode = 0; public Music() { Console.Clear(); - string[] str = ["退出", "启动", "测试下落", "指定字符串", "随机指定字符串下落", "扬声器节奏"]; - ModeSwitch.New("模拟音游 1.0 by ", str, (a) => + string[] str = ["退出", "启动", "测试下落", "指定字符串", "随机指定字符串下落", "**音游模式**"]; + ModeSwitch.New(title, str, (a) => { Console.Clear(); mode = a; @@ -46,7 +47,8 @@ public class Music setCharArry(); switch (mode) - {//默认模式 + { + //默认模式 case 1: new Thread(MusicDebug.Default).Start(); break; @@ -73,9 +75,10 @@ public class Music break; } case 5: - new Thread(MusicDebug.MusicRhythm).Start(); + // new Thread(MusicDebug.MusicRhythm).Start(); //MusicDebug.(); - break; + _ = new MusicFrom(); + return; default: break; } diff --git a/ModelingMusic/MusicDebug.cs b/ModelingMusic/MusicDebug.cs index 30cf48b..db1b173 100644 --- a/ModelingMusic/MusicDebug.cs +++ b/ModelingMusic/MusicDebug.cs @@ -118,15 +118,6 @@ public class MusicDebug Thread.Sleep(100); }; cap.StartRecording(); - // new Thread(() => - // { - // while (Music.IsWhile) - // { - // // Console.Clear(); - // // Console.WriteLine("RMS Volume: " + rmsVolume); - // Thread.Sleep(100); - // } - // }).Start(); } static float CalculateRMS(float[] samples) diff --git a/ModelingMusic/MusicForm.cs b/ModelingMusic/MusicForm.cs deleted file mode 100644 index 389f4b0..0000000 --- a/ModelingMusic/MusicForm.cs +++ /dev/null @@ -1,6 +0,0 @@ - -namespace Debug.ModelingMusic; - -public class MusicForm -{ -} diff --git a/ModelingMusic/MusicFrom.cs b/ModelingMusic/MusicFrom.cs new file mode 100644 index 0000000..6a39a23 --- /dev/null +++ b/ModelingMusic/MusicFrom.cs @@ -0,0 +1,117 @@ +using System; +using System.Diagnostics; +using System.Text; +using Debug; +using Debug.ModelingMusic; + +namespace ModelingMusic; + +public class MusicFrom +{ + private long time = 0; + + public MusicFrom() + { + setCharArry(); + new Thread(PrintCharArray).Start(); + } + + char[] jianpan = { 'D', 'F', 'G', ' ', 'J', 'K', 'L' }; + 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(); + ConsoleColor xiantiaoColor = Program.random.Next(0, 2) == 0 ? ConsoleColor.Red : ConsoleColor.Blue;//线条的颜色 + 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 = Program.chars[i, j].Color; + //Console.Write($" {Program.chars[i, j].Char} "); + + buffer.Append(Program.chars[i, j].Char); + time++; + if (time > 100000) + { + GC.Collect(); time = 0; + } + } + buffer.AppendLine(); + //Console.WriteLine(); + } + Console.ForegroundColor = ConsoleColor.DarkBlue; + Console.Write(buffer.ToString()); + if (time % 1000 == 0) + { + xiantiaoColor = Program.random.Next(0, 2) == 0 ? ConsoleColor.Red : ConsoleColor.Blue; + } + // Console.ForegroundColor = (ConsoleColor)random.Next(0, 16); + Console.ForegroundColor = xiantiaoColor; + buffer.Clear(); + for (i = 0; i < b; i++) + { + buffer.Append('-'); + } + // 打印ASDF HJKL + buffer.AppendLine(); + // int totalLength = 50; + int totalAs = jianpan.Length; + int spaceBetweenAs = (b - totalAs) / (totalAs - 1); + + for (i = 0; i < totalAs; i++) + { + for (j = 0; j < spaceBetweenAs / 2; j++) + { + buffer.Append(' '); + } + buffer.Append(jianpan[i]); + for (j = 0; j < spaceBetweenAs / 2; j++) + { + buffer.Append(' '); + } + } + + Console.Write(buffer); + // 停止计时并计算本帧的处理时间 + stopwatch.Stop(); + double elapsedMilliseconds = stopwatch.Elapsed.TotalMilliseconds; + totalTime += elapsedMilliseconds; + frameCount++; + // 计算帧率 + double fps = frameCount / (totalTime / 1000.0); + // Console.ResetColor(); + Console.ForegroundColor = ConsoleColor.White; + buffer.Clear(); + buffer.Append($" {fps:0} FPS "); + Console.WriteLine(buffer); + // 保持每秒约16.67ms(即大约60 FPS)的间隔 + // Thread.Sleep(Math.Max(0, 60 - (int)elapsedMilliseconds)); + //这样闪烁不会太严重 + Thread.Sleep(100); + } + } + private void setCharArry() + { + 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 Point(); // 初始化每个元素 + } + // Console.WriteLine(); + } + } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs index 2797f44..a55f415 100644 --- a/Program.cs +++ b/Program.cs @@ -5,6 +5,7 @@ using System.Net; using System.Runtime.InteropServices; using System.Text; using Debug.ModelingMusic; +using ModelingMusic; namespace Debug; /// @@ -23,7 +24,6 @@ public class Program Console.WriteLine(a); } - //开始计时 public static void Main(string[] args) { Console.Title = "Tools";//设置窗口标题 @@ -45,7 +45,7 @@ public class Program public Program() { Console.Clear(); - string[] str = ["退出", "抽卡", "搜索引擎", "下落字符", "小龙快跑", "贪吃蛇"]; + string[] str = ["退出", "抽卡", "搜索引擎", Music.title, "小龙快跑", "贪吃蛇"]; var modeSwitch = new ModeSwitch("<--- 请选择你要执行的操作 --->", str, (a) => { switch (a) @@ -60,7 +60,8 @@ public class Program _ = new Mod(); break; case 3: - _ = new Music(); + // _ = new Music(); + _ = new MusicFrom(); break; case 4: _ = new MinLong.Main(); @@ -72,9 +73,9 @@ public class Program printfLine("还没有这个功能"); break; } - }); + }, "作者:MuQing \n使用语言:C# \n使用IDE:VS Code \n使用框架:.NET 8.0 \n项目地址:https://git.coldmint.top/muqing/ModelingMusicForm.git"); + // modeSwitch.SetMessage(";"); } - public static void KeyEndThread() { new Thread(() => diff --git a/README.md b/README.md index 3cc8635..c5537e8 100644 --- a/README.md +++ b/README.md @@ -48,10 +48,13 @@ break; } }); +### 新增底部注意提醒词 +public ModeSwitch(string title, string[] a, Action Enter, string message) ### 输出 - 请选择你要执行的操作: - 0.退出 > - 1.模块 - 2.模块 - 3.模块 - 4.模块 \ No newline at end of file +<--- 请选择你要执行的操作 ---> +- << 退出 >> - +- 模块 - +- 模块 - +- 模块 - +- 模块 - +------------------------------ diff --git a/pyDebug.py b/pyDebug.py deleted file mode 100644 index dcdc201..0000000 --- a/pyDebug.py +++ /dev/null @@ -1,5 +0,0 @@ - - -print(sum(range(1, 101))) -print(sum(range(1, 101, 2))) -print(sum(range(2, 101, 2))) \ No newline at end of file