ModelingDebug/ModelingMusic/Music.cs

176 lines
6.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Diagnostics;
using System.Text;
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(title, str, (a) =>
{
mode = a;
Console.CursorVisible = false; // 隐藏光标
Console.Clear();
setCharArry();
switch (mode)
{
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 == 4)
{
//指定字符串模式
new Thread(() => MusicDebug.DesignateDown(ch)).Start();
}
else if (mode == 5)
{
//随机指定字符串下落
new Thread(() => MusicDebug.DesignateRandmoDown(ch)).Start();
}
break;
default:
break;
}
new Thread(PrintCharArray).Start();
if (mode == 1) { return; }
new Thread(Program.KeyEndThread).Start();
// 等待用户按下任意键后退出
}, "这是一个特别的模式\n自动测试和手动测试均为测试你的控制台是不是有闪屏或者卡顿\n" +
"字符串测试和字符串随机测试是测试你的控制台是不是有闪屏或者卡顿\n");
}
/// <summary>
/// 随机字符下落
/// </summary>
public static void Down(char a)
{
// 随机生成字符出现的初始位置
// 设置光标位置
ConsoleColor Color = Program.random.Next(0, 2) == 1 ? ConsoleColor.Red : ConsoleColor.Blue;
// 让字符从初始位置下落
int x = Program.random.Next(0, Program.chars.GetLength(1));
for (int y = 0; y < Program.chars.GetLength(0); y++) // 注意这里使用consoleHeight
{
// 移动到下一行
Program.chars[y, x].Char = a;
Program.chars[y, x].Color = Color;
if (y > 0)
{
Program.chars[y - 1, x].Char = Program.ShowChar;
}
Thread.Sleep(Program.random.Next(60, 100)); // 控制字符下落速度
}
Thread.Sleep(50);
Program.chars[Program.chars.GetLength(0) - 1, x].Char = Program.ShowChar;
}
private static long time = 0;
// 定义一个方法来打印二维字符数组
public static 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('-');
}
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();
//for (j = 0; i < b; j++)
//{
//}
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();
}
}
}