清理代码,移除不必要的注释和未使用的变量,优化控制台菜单选项

This commit is contained in:
muqing 2025-01-12 18:31:53 +08:00
parent b855a18a43
commit 76d31e8bf2
4 changed files with 140 additions and 150 deletions

View File

@ -12,79 +12,105 @@ public class ModeSwitch
public event Action<int>? Enter;
private readonly string[] str = ["退出"];
private int mode = 0;
// private bool zhuyi = false;//是否显示提醒词
private string title;
private string message;
public ModeSwitch(string title, string[] a, Action<int> Enter) : this(title, a, Enter, string.Empty)
{
}
public ModeSwitch(string title, string[] a, Action<int> Enter, string message)
{
this.message = message;
this.Enter = Enter;
str = a;
new Thread(() =>
{
// Thread logic here
PrintCentered(title);
int windowWidth = Console.WindowWidth;
for (int i = 0; i < str.Length; i++)
{
// string paddedString = str[i].PadLeft((windowWidth + str[i].Length) / 2).PadRight(windowWidth);
// Console.WriteLine(paddedString);
PrintCentered($"{str[i]}");
}
// 判断message是不是为空
if (message != string.Empty)
{
for (int i = 0; i < windowWidth; i++)
{
Console.Write('-');
}
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();
this.title = title;
this.message = message;
Run();
}
private void Run()
{
Console.Clear();
PrintCentered(title);
int windowWidth = Console.WindowWidth;
for (int i = 0; i < str.Length; i++)
{
// string paddedString = str[i].PadLeft((windowWidth + str[i].Length) / 2).PadRight(windowWidth);
// Console.WriteLine(paddedString);
PrintCentered($"{str[i]}");
}
// 判断message是不是为空
for (int i = 0; i < windowWidth; i++)
{
Console.Write('-');
}
Console.WriteLine();
PrintCentered("按W/S或↑/↓选择或数字键选择按Enter确定,按Esc退出");
if (message != string.Empty)
{
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.D1 && key.Key <= ConsoleKey.D9)
{
int v1 = int.Parse(key.KeyChar.ToString());
if (v1 >= 1 && v1 <= str.Length)
mode = v1 - 1;
}
else if (key.Key == ConsoleKey.Escape)
{
IsEnd();
return;
}
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);
}
/// <summary>
/// 确实是否退出
/// </summary>
private void IsEnd()
{
Console.Clear();
Console.WriteLine("再次按ESC退出,按回车键继续");
var key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Escape)
{
Console.Clear();
Environment.Exit(0);
}
else if (key.Key == ConsoleKey.Enter)
{
Run();
}
}
private string message = "";
private static int CalculateActualLength(string text)
{
Encoding encoding = Encoding.UTF8;
@ -100,6 +126,10 @@ public class ModeSwitch
return length;
}
public static void New(string title, string[] a, Action<int> Enter, string message)
{
_ = new ModeSwitch(title, a, Enter, message);
}
public static void New(string title, string[] a, Action<int> Enter)
{
_ = new ModeSwitch(title, a, Enter);

View File

@ -11,79 +11,54 @@ public class Music
public Music()
{
Console.Clear();
string[] str = ["退出", "启动", "测试下落", "指定字符串", "随机指定字符串下落", "**音游模式**"];
string[] str = ["返回首页", "启动", "手动测试", "自动测试", "字符串测试", "字符串随机测试"];
ModeSwitch.New(title, str, (a) =>
{
Console.Clear();
mode = a;
if (a == 0)
Console.CursorVisible = false; // 隐藏光标
Console.Clear();
setCharArry();
switch (mode)
{
return;
}
Init().GetAwaiter().GetResult(); // 启动
});
}
private async Task Init()
{
Console.CursorVisible = false; // 隐藏光标
Console.Clear();
Console.Write("加载资源中");
// Console.SetWindowSize(chars.GetLength(0) * 5, chars.GetLength(1) * 5); // 设置窗口大小
// Console.SetBufferSize(chars.GetLength(0) * 5, chars.GetLength(1) * 5); // 设置缓冲区大小
// 获取控制台的大小
// consoleWidth = Console.WindowWidth;
// consoleHeight = Console.WindowHeight - 1;
for (int i = 0; i < 3; i++)
{
Console.Write("·");
await Task.Delay(300);
}
Console.WriteLine("加载完成");
await Task.Delay(500);
Console.Clear();
setCharArry();
switch (mode)
{
//默认模式
case 1:
new Thread(MusicDebug.Default).Start();
break;
case 2:
new Thread(MusicDebug.DebugDefault).Start();
break;
case 3:
case 4:
{
case 0:
_ = new Program();
return;
//默认模式
case 1:
_ = new MusicFrom();
return;
case 2:
new Thread(MusicDebug.Default).Start();
break;
case 3:
new Thread(MusicDebug.DebugDefault).Start();
break;
case 4:
case 5:
Console.WriteLine("请输入字符串");
string? v = Console.ReadLine();
//string转成char[]
char[] ch = v!.ToCharArray();
if (mode == 3)
if (mode == 4)
{
//指定字符串模式
new Thread(() => MusicDebug.DesignateDown(ch)).Start();
}
else
else if (mode == 5)
{
//随机指定字符串下落
new Thread(() => MusicDebug.DesignateRandmoDown(ch)).Start();
}
break;
}
case 5:
// new Thread(MusicDebug.MusicRhythm).Start();
//MusicDebug.();
_ = new MusicFrom();
return;
default:
break;
}
new Thread(PrintCharArray).Start();
if (mode == 1) { return; }
new Thread(Program.KeyEndThread).Start();
// 等待用户按下任意键后退出
default:
break;
}
new Thread(PrintCharArray).Start();
if (mode == 1) { return; }
new Thread(Program.KeyEndThread).Start();
// 等待用户按下任意键后退出
}, "这是一个特别的模式\n自动测试和手动测试均为测试你的控制台是不是有闪屏或者卡顿\n" +
"字符串测试和字符串随机测试是测试你的控制台是不是有闪屏或者卡顿\n");
}

View File

@ -238,7 +238,6 @@ public class MusicFrom
{
Program.chars[i, j] = new Point(); // 初始化每个元素
}
// Console.WriteLine();
}
}
//分数long 000000000

View File

@ -14,11 +14,10 @@ public class Program
Console.Title = "Tools";//设置窗口标题
// 设置控制台输出编码,以支持复杂字符
Console.OutputEncoding = Encoding.UTF8;
// 切换到英语输入法
Console.Clear();
Console.CursorVisible = false;
new MusicFrom();
// _ = new Program();
// new MusicFrom();
_ = new Program();
}
//Y,X
@ -31,33 +30,29 @@ public class Program
public Program()
{
Console.Clear();
string[] str = ["退出", "抽卡", "搜索引擎", Music.title, "小龙快跑", "贪吃蛇"];
string[] str = ["抽卡", "搜索引擎", Music.title, "小龙快跑", "贪吃蛇"];
var modeSwitch = new ModeSwitch("<--- 请选择你要执行的操作 --->", str, (a) =>
{
switch (a)
{
case 0:
Environment.Exit(0);
break;
case 1:
_ = new ChouKa();//抽卡
break;
case 2:
case 1:
_ = new Mod();
break;
case 3:
// _ = new Music();
_ = new MusicFrom();
case 2:
_ = new Music();
// _ = new MusicFrom();
break;
case 4:
case 3:
_ = new MinLong.Main();
break;
case 5:
case 4:
_ = new Tanchishe.Main();
break;
}
}, "作者MuQing \n使用语言C# \n使用IDEVS Code \n使用框架.NET 8.0 \n项目地址https://git.coldmint.top/muqing/ModelingMusicForm.git");
// modeSwitch.SetMessage(";");
}
public static void KeyEndThread()
{
@ -79,26 +74,17 @@ public class Program
// 等待用户按下任意键后退出
}).Start();
}
/// <summary>
/// 监听键盘事件 注意不要把此方法嵌套在循环里面
/// 重置字符数组
/// </summary>
/// <param name="action"></param>
public static void KeyEnd(Action<ConsoleKeyInfo> action)
{
ConsoleKeyInfo consoleKeyInfo = Console.ReadKey(true);
action.Invoke(consoleKeyInfo);
// 等待用户按下任意键后退出
}
public static void ResetChars()
{
for (int i = 0; i < Program.chars.GetLength(0); i++)
for (int i = 0; i < chars.GetLength(0); i++)
{
for (int j = 0; j < Program.chars.GetLength(1); j++)
for (int j = 0; j < chars.GetLength(1); j++)
{
Program.chars[i, j] = new ModelingMusic.Point();
chars[i, j] = new Point();
}
}
}