ModelingDebug/ModelingMusic/Music.cs
2024-08-30 14:48:22 +08:00

200 lines
6.6 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
{
//选择的模式
private static int mode = 0;
public Music()
{
Console.Clear();
string[] str = ["退出", "启动", "测试下落", "指定字符串", "随机指定字符串下落", "扬声器节奏"];
ModeSwitch.New("模拟音游 1.0 by ", str, (a) =>
{
Console.Clear();
mode = a;
if (a == 0)
{
return;
}
Init().GetAwaiter().GetResult(); // 启动
});
}
private async Task Init()
{
Console.CursorVisible = false; // 隐藏光标
Console.Clear();
Program.printf("加载资源中");
// 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++)
{
Program.printf("·");
await Task.Delay(300);
}
Console.WriteLine();
Program.printfLine("加载完成");
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:
{
Console.WriteLine("请输入字符串");
string? v = Console.ReadLine();
//string转成char[]
char[] ch = v!.ToCharArray();
if (mode == 3)
{
//指定字符串模式
new Thread(() => MusicDebug.DesignateDown(ch)).Start();
}
else
{
//随机指定字符串下落
new Thread(() => MusicDebug.DesignateRandmoDown(ch)).Start();
}
break;
}
case 5:
new Thread(MusicDebug.MusicRhythm).Start();
//MusicDebug.();
break;
default:
break;
}
new Thread(PrintCharArray).Start();
if (mode == 1) { return; }
new Thread(Program.KeyEndThread).Start();
// 等待用户按下任意键后退出
}
/// <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();
}
}
}