2024-08-25 09:55:05 +00:00
|
|
|
|
|
|
|
|
|
using System.Diagnostics;
|
2024-08-25 23:51:27 +00:00
|
|
|
|
using System.Text;
|
2024-08-25 09:55:05 +00:00
|
|
|
|
|
|
|
|
|
namespace Debug.ModelingMusic;
|
|
|
|
|
public class Music
|
|
|
|
|
{
|
2025-01-04 11:26:18 +00:00
|
|
|
|
public static string title = "模拟音游 1.5 by";
|
2024-08-25 09:55:05 +00:00
|
|
|
|
//选择的模式
|
2024-08-25 23:58:04 +00:00
|
|
|
|
private static int mode = 0;
|
2024-08-25 09:55:05 +00:00
|
|
|
|
public Music()
|
|
|
|
|
{
|
|
|
|
|
Console.Clear();
|
2025-01-12 10:31:53 +00:00
|
|
|
|
string[] str = ["返回首页", "启动", "手动测试", "自动测试", "字符串测试", "字符串随机测试"];
|
2025-01-04 11:26:18 +00:00
|
|
|
|
ModeSwitch.New(title, str, (a) =>
|
2024-08-27 13:52:00 +00:00
|
|
|
|
{
|
2024-08-28 00:56:27 +00:00
|
|
|
|
mode = a;
|
2025-01-12 10:31:53 +00:00
|
|
|
|
Console.CursorVisible = false; // 隐藏光标
|
|
|
|
|
Console.Clear();
|
|
|
|
|
setCharArry();
|
|
|
|
|
switch (mode)
|
2024-08-28 04:19:44 +00:00
|
|
|
|
{
|
2025-01-12 10:31:53 +00:00
|
|
|
|
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:
|
2024-08-25 09:55:05 +00:00
|
|
|
|
Console.WriteLine("请输入字符串");
|
|
|
|
|
string? v = Console.ReadLine();
|
|
|
|
|
//string转成char[]
|
|
|
|
|
char[] ch = v!.ToCharArray();
|
2025-01-12 10:31:53 +00:00
|
|
|
|
if (mode == 4)
|
2024-08-25 09:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
//指定字符串模式
|
|
|
|
|
new Thread(() => MusicDebug.DesignateDown(ch)).Start();
|
|
|
|
|
}
|
2025-01-12 10:31:53 +00:00
|
|
|
|
else if (mode == 5)
|
2024-08-25 09:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
//随机指定字符串下落
|
|
|
|
|
new Thread(() => MusicDebug.DesignateRandmoDown(ch)).Start();
|
|
|
|
|
}
|
|
|
|
|
break;
|
2025-01-12 10:31:53 +00:00
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
new Thread(PrintCharArray).Start();
|
|
|
|
|
if (mode == 1) { return; }
|
|
|
|
|
new Thread(Program.KeyEndThread).Start();
|
|
|
|
|
// 等待用户按下任意键后退出
|
|
|
|
|
}, "这是一个特别的模式\n自动测试和手动测试均为测试你的控制台是不是有闪屏或者卡顿\n" +
|
|
|
|
|
"字符串测试和字符串随机测试是测试你的控制台是不是有闪屏或者卡顿\n");
|
2024-08-25 09:55:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 随机字符下落
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static void Down(char a)
|
|
|
|
|
{
|
|
|
|
|
// 随机生成字符出现的初始位置
|
|
|
|
|
|
|
|
|
|
// 设置光标位置
|
2024-08-30 06:48:22 +00:00
|
|
|
|
ConsoleColor Color = Program.random.Next(0, 2) == 1 ? ConsoleColor.Red : ConsoleColor.Blue;
|
2024-08-25 09:55:05 +00:00
|
|
|
|
// 让字符从初始位置下落
|
2024-08-30 06:48:22 +00:00
|
|
|
|
int x = Program.random.Next(0, Program.chars.GetLength(1));
|
2024-08-25 23:58:04 +00:00
|
|
|
|
for (int y = 0; y < Program.chars.GetLength(0); y++) // 注意这里使用consoleHeight
|
2024-08-25 09:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
// 移动到下一行
|
2024-08-25 23:58:04 +00:00
|
|
|
|
Program.chars[y, x].Char = a;
|
2024-08-30 06:48:22 +00:00
|
|
|
|
Program.chars[y, x].Color = Color;
|
2024-08-25 09:55:05 +00:00
|
|
|
|
if (y > 0)
|
|
|
|
|
{
|
2024-08-30 06:48:22 +00:00
|
|
|
|
Program.chars[y - 1, x].Char = Program.ShowChar;
|
2024-08-25 09:55:05 +00:00
|
|
|
|
}
|
2024-08-30 06:48:22 +00:00
|
|
|
|
Thread.Sleep(Program.random.Next(60, 100)); // 控制字符下落速度
|
2024-08-25 09:55:05 +00:00
|
|
|
|
}
|
|
|
|
|
Thread.Sleep(50);
|
2024-08-30 06:48:22 +00:00
|
|
|
|
Program.chars[Program.chars.GetLength(0) - 1, x].Char = Program.ShowChar;
|
2024-08-25 09:55:05 +00:00
|
|
|
|
}
|
2024-08-25 23:51:27 +00:00
|
|
|
|
private static long time = 0;
|
2024-08-25 09:55:05 +00:00
|
|
|
|
// 定义一个方法来打印二维字符数组
|
|
|
|
|
|
|
|
|
|
public static void PrintCharArray()
|
|
|
|
|
{
|
|
|
|
|
var stopwatch = new Stopwatch();
|
|
|
|
|
|
|
|
|
|
int frameCount = 0;
|
|
|
|
|
double totalTime = 0;
|
|
|
|
|
int i; int j;
|
2024-08-25 23:58:04 +00:00
|
|
|
|
var a = Program.chars.GetLength(0);
|
|
|
|
|
var b = Program.chars.GetLength(1);
|
2024-08-25 23:51:27 +00:00
|
|
|
|
var buffer = new StringBuilder();
|
2024-08-30 06:48:22 +00:00
|
|
|
|
ConsoleColor xiantiaoColor = Program.random.Next(0, 2) == 0 ? ConsoleColor.Red : ConsoleColor.Blue;//线条的颜色
|
2024-08-25 23:58:04 +00:00
|
|
|
|
while (Program.IsWhile)
|
2024-08-25 09:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
stopwatch.Restart();
|
2024-08-25 23:51:27 +00:00
|
|
|
|
Console.SetCursorPosition(0, 0);// 设定光标位置
|
|
|
|
|
Console.Clear();
|
|
|
|
|
buffer.Clear();
|
2024-08-25 09:55:05 +00:00
|
|
|
|
for (i = 0; i < a; i++)
|
|
|
|
|
{
|
|
|
|
|
for (j = 0; j < b; j++)
|
|
|
|
|
{
|
2024-08-25 23:58:04 +00:00
|
|
|
|
//Console.ForegroundColor = Program.chars[i, j].Color;
|
|
|
|
|
//Console.Write($" {Program.chars[i, j].Char} ");
|
|
|
|
|
|
|
|
|
|
buffer.Append(Program.chars[i, j].Char);
|
2024-08-25 23:51:27 +00:00
|
|
|
|
time++;
|
|
|
|
|
if (time > 100000)
|
2024-08-25 09:55:05 +00:00
|
|
|
|
{
|
2024-08-25 23:51:27 +00:00
|
|
|
|
GC.Collect(); time = 0;
|
2024-08-25 09:55:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-25 23:51:27 +00:00
|
|
|
|
buffer.AppendLine();
|
|
|
|
|
//Console.WriteLine();
|
2024-08-25 09:55:05 +00:00
|
|
|
|
}
|
2024-08-25 23:58:04 +00:00
|
|
|
|
Console.ForegroundColor = ConsoleColor.DarkBlue;
|
2024-08-25 23:51:27 +00:00
|
|
|
|
Console.Write(buffer.ToString());
|
|
|
|
|
if (time % 1000 == 0)
|
2024-08-25 09:55:05 +00:00
|
|
|
|
{
|
2024-08-30 06:48:22 +00:00
|
|
|
|
xiantiaoColor = Program.random.Next(0, 2) == 0 ? ConsoleColor.Red : ConsoleColor.Blue;
|
2024-08-25 09:55:05 +00:00
|
|
|
|
}
|
2024-08-25 23:51:27 +00:00
|
|
|
|
// Console.ForegroundColor = (ConsoleColor)random.Next(0, 16);
|
2024-08-25 23:58:04 +00:00
|
|
|
|
Console.ForegroundColor = xiantiaoColor;
|
|
|
|
|
buffer.Clear();
|
2024-08-25 09:55:05 +00:00
|
|
|
|
for (i = 0; i < b; i++)
|
|
|
|
|
{
|
2024-08-25 23:58:04 +00:00
|
|
|
|
buffer.Append('-');
|
2024-08-25 09:55:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-25 23:58:04 +00:00
|
|
|
|
Console.Write(buffer);
|
2024-08-25 09:55:05 +00:00
|
|
|
|
// 停止计时并计算本帧的处理时间
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
double elapsedMilliseconds = stopwatch.Elapsed.TotalMilliseconds;
|
|
|
|
|
totalTime += elapsedMilliseconds;
|
|
|
|
|
frameCount++;
|
|
|
|
|
// 计算帧率
|
|
|
|
|
double fps = frameCount / (totalTime / 1000.0);
|
2024-08-25 23:58:04 +00:00
|
|
|
|
// Console.ResetColor();
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.White;
|
|
|
|
|
buffer.Clear();
|
|
|
|
|
//for (j = 0; i < b; j++)
|
|
|
|
|
//{
|
|
|
|
|
//}
|
|
|
|
|
buffer.Append($" {fps:0} FPS ");
|
|
|
|
|
Console.WriteLine(buffer);
|
2024-08-25 09:55:05 +00:00
|
|
|
|
// 保持每秒约16.67ms(即大约60 FPS)的间隔
|
|
|
|
|
// Thread.Sleep(Math.Max(0, 60 - (int)elapsedMilliseconds));
|
|
|
|
|
|
2024-08-25 23:58:04 +00:00
|
|
|
|
//这样闪烁不会太严重
|
|
|
|
|
Thread.Sleep(100);
|
2024-08-25 09:55:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private void setCharArry()
|
|
|
|
|
{
|
2024-08-25 23:58:04 +00:00
|
|
|
|
for (int i = 0; i < Program.chars.GetLength(0); i++)
|
2024-08-25 09:55:05 +00:00
|
|
|
|
{
|
2024-08-25 23:58:04 +00:00
|
|
|
|
for (int j = 0; j < Program.chars.GetLength(1); j++)
|
2024-08-25 09:55:05 +00:00
|
|
|
|
{
|
2024-08-25 23:58:04 +00:00
|
|
|
|
Program.chars[i, j] = new Point(); // 初始化每个元素
|
2024-08-25 09:55:05 +00:00
|
|
|
|
}
|
|
|
|
|
// Console.WriteLine();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|