ModelingDebug/ModelingMusic/Music.cs

255 lines
8.3 KiB
C#
Raw Normal View History

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
{
//选择的模式
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();
2024-08-27 13:52:00 +00:00
Program.printfLine("模拟音游 1.0 by ");
string[] str = ["0 退出", "1.启动", "2.测试下落", "3.指定字符串", "4.随机指定字符串下落", "5.扬声器节奏"];
// Program.printfLine("1.启动 2.测试下落 3.指定字符串 4.随机指定字符串下落 5.扬声器节奏 0.退出");
foreach (var aa in str)
{
Program.printfLine(aa);
}
Console.SetCursorPosition(0, mode + 1);
2024-08-25 09:55:05 +00:00
do
{
2024-08-27 13:52:00 +00:00
var v = Console.ReadKey(true);
// 检查是否为数字
if (v.Key >= ConsoleKey.D0 && v.Key <= ConsoleKey.D9)
{
int v1 = int.Parse(v.KeyChar.ToString());
if (v1 >= 0 && v1 < str.Length)
mode = v1;
}
else
2024-08-25 09:55:05 +00:00
{
2024-08-27 13:52:00 +00:00
switch (v.Key)
2024-08-25 09:55:05 +00:00
{
2024-08-27 13:52:00 +00:00
case ConsoleKey.UpArrow:
mode = mode == 0 ? mode : mode - 1;
break;
case ConsoleKey.DownArrow:
mode = mode == str.Length - 1 ? mode : mode + 1;
break;
case ConsoleKey.Enter:
if (mode == 0)
{
//退出程序
Environment.Exit(0);
return;
}
goto Start;
break;
2024-08-25 09:55:05 +00:00
}
}
2024-08-27 13:52:00 +00:00
// Console.WriteLine("无效输入请确保您输入的是一个数字0-9。");
Console.SetCursorPosition(0, mode + 1);
2024-08-25 09:55:05 +00:00
2024-08-27 13:52:00 +00:00
} while (true);
Start:
{
Init().GetAwaiter().GetResult(); // 启动
}
2024-08-25 09:55:05 +00:00
}
2024-08-25 23:51:27 +00:00
2024-08-25 09:55:05 +00:00
private async Task Init()
{
Console.CursorVisible = false; // 隐藏光标
Console.Clear();
Program.printf("加载资源中");
2024-08-25 23:51:27 +00:00
// Console.SetWindowSize(chars.GetLength(0) * 5, chars.GetLength(1) * 5); // 设置窗口大小
// Console.SetBufferSize(chars.GetLength(0) * 5, chars.GetLength(1) * 5); // 设置缓冲区大小
2024-08-25 09:55:05 +00:00
// 获取控制台的大小
2024-08-25 23:51:27 +00:00
// consoleWidth = Console.WindowWidth;
// consoleHeight = Console.WindowHeight - 1;
2024-08-25 09:55:05 +00:00
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.CursorVisible = true; // 隐藏光标
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;
}
Console.CursorVisible = false; // 隐藏光标
new Thread(PrintCharArray).Start();
2024-08-25 23:51:27 +00:00
if (mode == 1) { return; }
2024-08-27 13:52:00 +00:00
new Thread(Program.KeyEndThread).Start();
2024-08-25 09:55:05 +00:00
// 等待用户按下任意键后退出
}
/// <summary>
/// 随机字符下落
/// </summary>
private static readonly Random random = new();
//常量
public const char ShowChar = ' ';
2024-08-25 23:51:27 +00:00
// public static readonly char[,] chars = new char[10, 30];
2024-08-25 09:55:05 +00:00
public static void Down(char a)
{
// 随机生成字符出现的初始位置
// 设置光标位置
ConsoleColor Color = ConsoleColor.White;
int v = random.Next(0, 2);
switch (v)
{
case 0:
Color = ConsoleColor.Red;
break;
case 1:
Color = ConsoleColor.Blue;
break;
}
// 让字符从初始位置下落
2024-08-25 23:58:04 +00:00
int x = random.Next(0, Program.chars.GetLength(1));
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-25 09:55:05 +00:00
if (y > 0)
{
2024-08-25 23:58:04 +00:00
Program.chars[y - 1, x].Char = ShowChar;
2024-08-25 09:55:05 +00:00
}
2024-08-25 23:58:04 +00:00
Program.chars[y, x].Color = Color;
2024-08-25 09:55:05 +00:00
Thread.Sleep(random.Next(60, 120)); // 控制字符下落速度
}
Thread.Sleep(50);
2024-08-25 23:58:04 +00:00
Program.chars[Program.chars.GetLength(0) - 1, x].Char = 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-25 23:58:04 +00:00
ConsoleColor xiantiaoColor = random.Next(0, 2) == 0 ? ConsoleColor.Red : ConsoleColor.Blue;//线条的颜色
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-25 23:58:04 +00:00
xiantiaoColor = 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();
}
}
}