模拟音游1.5 by优化更新

This commit is contained in:
muqing 2025-01-04 19:26:18 +08:00
parent c3af5bd380
commit ac00035e31
8 changed files with 207 additions and 80 deletions

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection.Metadata;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -10,58 +11,80 @@ public class ModeSwitch
{ {
public event Action<int>? Enter; public event Action<int>? Enter;
private readonly string[] str = ["退出"]; private readonly string[] str = ["退出"];
private readonly int mode = 0; private int mode = 0;
public ModeSwitch(string title, string[] a, Action<int> Enter) // private bool zhuyi = false;//是否显示提醒词
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; this.Enter = Enter;
str = a; str = a;
PrintCentered(title); new Thread(() =>
int windowWidth = Console.WindowWidth;
for (int i = 0; i < str.Length; i++)
{ {
// string paddedString = str[i].PadLeft((windowWidth + str[i].Length) / 2).PadRight(windowWidth); // Thread logic here
// Console.WriteLine(paddedString); PrintCentered(title);
PrintCentered($"{str[i]}"); int windowWidth = Console.WindowWidth;
} for (int i = 0; i < str.Length; i++)
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()); // string paddedString = str[i].PadLeft((windowWidth + str[i].Length) / 2).PadRight(windowWidth);
if (v1 >= 0 && v1 < str.Length) // Console.WriteLine(paddedString);
mode = v1; PrintCentered($"{str[i]}");
} }
else // 判断message是不是为空
switch (key.Key)
if (message != string.Empty)
{
for (int i = 0; i < windowWidth; i++)
{ {
case ConsoleKey.UpArrow: Console.Write('-');
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)); Console.WriteLine();
} while (true); 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();
} }
private string message = "";
private static int CalculateActualLength(string text) private static int CalculateActualLength(string text)
{ {
Encoding encoding = Encoding.UTF8; Encoding encoding = Encoding.UTF8;

View File

@ -1,18 +1,19 @@
using System.Diagnostics; using System.Diagnostics;
using System.Text; using System.Text;
using ModelingMusic;
namespace Debug.ModelingMusic; namespace Debug.ModelingMusic;
public class Music public class Music
{ {
public static string title = "模拟音游 1.5 by";
//选择的模式 //选择的模式
private static int mode = 0; private static int mode = 0;
public Music() public Music()
{ {
Console.Clear(); Console.Clear();
string[] str = ["退出", "启动", "测试下落", "指定字符串", "随机指定字符串下落", "扬声器节奏"]; string[] str = ["退出", "启动", "测试下落", "指定字符串", "随机指定字符串下落", "**音游模式**"];
ModeSwitch.New("模拟音游 1.0 by ", str, (a) => ModeSwitch.New(title, str, (a) =>
{ {
Console.Clear(); Console.Clear();
mode = a; mode = a;
@ -46,7 +47,8 @@ public class Music
setCharArry(); setCharArry();
switch (mode) switch (mode)
{//默认模式 {
//默认模式
case 1: case 1:
new Thread(MusicDebug.Default).Start(); new Thread(MusicDebug.Default).Start();
break; break;
@ -73,9 +75,10 @@ public class Music
break; break;
} }
case 5: case 5:
new Thread(MusicDebug.MusicRhythm).Start(); // new Thread(MusicDebug.MusicRhythm).Start();
//MusicDebug.(); //MusicDebug.();
break; _ = new MusicFrom();
return;
default: default:
break; break;
} }

View File

@ -118,15 +118,6 @@ public class MusicDebug
Thread.Sleep(100); Thread.Sleep(100);
}; };
cap.StartRecording(); cap.StartRecording();
// new Thread(() =>
// {
// while (Music.IsWhile)
// {
// // Console.Clear();
// // Console.WriteLine("RMS Volume: " + rmsVolume);
// Thread.Sleep(100);
// }
// }).Start();
} }
static float CalculateRMS(float[] samples) static float CalculateRMS(float[] samples)

View File

@ -1,6 +0,0 @@

namespace Debug.ModelingMusic;
public class MusicForm
{
}

117
ModelingMusic/MusicFrom.cs Normal file
View File

@ -0,0 +1,117 @@
using System;
using System.Diagnostics;
using System.Text;
using Debug;
using Debug.ModelingMusic;
namespace ModelingMusic;
public class MusicFrom
{
private long time = 0;
public MusicFrom()
{
setCharArry();
new Thread(PrintCharArray).Start();
}
char[] jianpan = { 'D', 'F', 'G', ' ', 'J', 'K', 'L' };
public 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('-');
}
// 打印ASDF HJKL
buffer.AppendLine();
// int totalLength = 50;
int totalAs = jianpan.Length;
int spaceBetweenAs = (b - totalAs) / (totalAs - 1);
for (i = 0; i < totalAs; i++)
{
for (j = 0; j < spaceBetweenAs / 2; j++)
{
buffer.Append(' ');
}
buffer.Append(jianpan[i]);
for (j = 0; j < spaceBetweenAs / 2; j++)
{
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();
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();
}
}
}

View File

@ -5,6 +5,7 @@ using System.Net;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
using Debug.ModelingMusic; using Debug.ModelingMusic;
using ModelingMusic;
namespace Debug; namespace Debug;
/// <summary> /// <summary>
@ -23,7 +24,6 @@ public class Program
Console.WriteLine(a); Console.WriteLine(a);
} }
//开始计时
public static void Main(string[] args) public static void Main(string[] args)
{ {
Console.Title = "Tools";//设置窗口标题 Console.Title = "Tools";//设置窗口标题
@ -45,7 +45,7 @@ public class Program
public Program() public Program()
{ {
Console.Clear(); Console.Clear();
string[] str = ["退出", "抽卡", "搜索引擎", "下落字符", "小龙快跑", "贪吃蛇"]; string[] str = ["退出", "抽卡", "搜索引擎", Music.title, "小龙快跑", "贪吃蛇"];
var modeSwitch = new ModeSwitch("<--- 请选择你要执行的操作 --->", str, (a) => var modeSwitch = new ModeSwitch("<--- 请选择你要执行的操作 --->", str, (a) =>
{ {
switch (a) switch (a)
@ -60,7 +60,8 @@ public class Program
_ = new Mod(); _ = new Mod();
break; break;
case 3: case 3:
_ = new Music(); // _ = new Music();
_ = new MusicFrom();
break; break;
case 4: case 4:
_ = new MinLong.Main(); _ = new MinLong.Main();
@ -72,9 +73,9 @@ public class Program
printfLine("还没有这个功能"); printfLine("还没有这个功能");
break; 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() public static void KeyEndThread()
{ {
new Thread(() => new Thread(() =>

View File

@ -48,10 +48,13 @@
break; break;
} }
}); });
### 新增底部注意提醒词
public ModeSwitch(string title, string[] a, Action<int> Enter, string message)
### 输出 ### 输出
请选择你要执行的操作: <--- 请选择你要执行的操作 --->
0.退出 > - << 退出 >> -
1.模块 - 模块 -
2.模块 - 模块 -
3.模块 - 模块 -
4.模块 - 模块 -
------------------------------

View File

@ -1,5 +0,0 @@
print(sum(range(1, 101)))
print(sum(range(1, 101, 2)))
print(sum(range(2, 101, 2)))