控制台优化选择

This commit is contained in:
muqing 2024-08-26 07:58:04 +08:00
parent 6a261ecae7
commit 49aa3ad02a
5 changed files with 255 additions and 123 deletions

View File

@ -1,17 +1,23 @@
using System.Diagnostics;
using System.Text;
namespace Debug.MinLong;
#pragma warning disable CA1822 // 将成员标记为 static
#pragma warning disable IDE0051 // 删除未使用的私有成员
#pragma warning disable IDE1006 // 命名样式
public class KongLong
{
public KongLong()
{
Console.WriteLine("MinLong");
Console.CursorVisible = false;
ResetChars();//初始化
ResetLong();//初始化小龙
new Thread(PrintCharArray).Start();
KeyThread();//监听键盘
}
public char[,] chars = new char[10, 30];
private long time;
public void PrintCharArray()
{
@ -20,39 +26,42 @@ public class KongLong
int frameCount = 0;
double totalTime = 0;
int i; int j;
var a = chars.GetLength(0);
var b = chars.GetLength(1);
var a = Program.chars.GetLength(0);
var b = Program.chars.GetLength(1);
var buffer = new StringBuilder();
while (Program.IsWhile)
{
stopwatch.Restart();
Console.SetCursorPosition(0, 0);// 设定光标位置
Console.Clear();
Console.SetCursorPosition(0, 0);
buffer.Clear();
for (i = 0; i < a; i++)
{
for (j = 0; j < b; j++)
{
// Console.ForegroundColor = chars[i, j].Color;
// Console.Write($" {chars[i, j].Char} ");
Console.Write(chars[i, j]);
buffer.Append(Program.chars[i, j].Char);
time++;
if (time > 100000)
{
GC.Collect(); time = 0;
}
}
Console.WriteLine();
buffer.AppendLine();
}
Console.ForegroundColor = ConsoleColor.White;
for (i = 0; i < b; i++)
{
Console.Write("---");
buffer.Append('Ξ');
}
if (time % 1000 == 0)
{
new Thread(Zhangaiwu.newshitou).Start();
}
Console.Write(buffer);
Console.ResetColor();
// 停止计时并计算本帧的处理时间
@ -72,13 +81,95 @@ public class KongLong
Thread.Sleep(100);
}
}
private void SetChars()
public class XiaoLong
{
for (int i = 0; i < chars.GetLength(0); i++)
public char Char = '口';//小龙的形状
public ConsoleColor Color = ConsoleColor.Red;//小龙的颜色
public int x;//小龙X坐标
public int y;//小龙Y坐标
public bool istiao = false;//是否跳
}
public XiaoLong xiaoLong = new();
int charsy = Program.chars.GetLength(0);
/// <summary>
/// 初始化小龙
/// </summary>
public void ResetLong()
{
for (int j = 0; j < chars.GetLength(1); j++)
int x = Program.chars.GetLength(1);
xiaoLong.x = x / 6;
xiaoLong.y = charsy - 1;
Program.chars[xiaoLong.y, xiaoLong.x].Char = xiaoLong.Char;
}
/// <summary>
/// 全局键盘监听
/// </summary>
public void KeyThread()
{
chars[i, j] = '0';
new Thread(() =>
{
while (Program.IsWhile)
{
ConsoleKeyInfo consoleKeyInfo = Console.ReadKey(true);
if (consoleKeyInfo.Key == ConsoleKey.Escape)
{
Program.IsWhile = false;
Thread.Sleep(500);
Console.Clear();
Environment.Exit(0);
return;
}
// Console.WriteLine(consoleKeyInfo.Key);
switch (consoleKeyInfo.Key)
{
//空格
case ConsoleKey.Spacebar:
if (!xiaoLong.istiao) { new Thread(tiaoyue).Start(); }
break;
}
}
// 等待用户按下任意键后退出
}).Start();
}
/// <summary>
/// 跳跃
/// </summary>
private void tiaoyue()
{
if (xiaoLong.istiao) return;
xiaoLong.istiao = true;
// Console.WriteLine("跳跃");
//设置离地高度
for (int i = 0; i < 3; i++)
{
Program.chars[xiaoLong.y, xiaoLong.x].Char = Program.ShowChar;
xiaoLong.y -= 1;
Program.chars[xiaoLong.y, xiaoLong.x].Char = xiaoLong.Char;
Thread.Sleep(100);
}
for (int i = 0; i < 3; i++)
{
Program.chars[xiaoLong.y, xiaoLong.x].Char = Program.ShowChar;
xiaoLong.y += 1;
Program.chars[xiaoLong.y, xiaoLong.x].Char = xiaoLong.Char;
Thread.Sleep(100);
}
xiaoLong.istiao = false;
}
public static void ResetChars()
{
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 ModelingMusic.Point();
}
}
}

31
MinLong/Zhangaiwu.cs Normal file
View File

@ -0,0 +1,31 @@
namespace Debug.MinLong;
#pragma warning disable CS8981 // 该类型名称仅包含小写 ascii 字符。此类名称可能会成为该语言的保留值。
public class Zhangaiwu
{
public static void newshitou()
{
var y = Program.chars.GetLength(0);
var x = Program.chars.GetLength(1);
var shitou = new Shitou
{
x = x - 1,
y = y - 1
};
for (int i = 1; i < x; i++)
{
Program.chars[shitou.y, shitou.x].Char = 'O';
Thread.Sleep(100);
Program.chars[shitou.y, shitou.x].Char = Program.ShowChar;
shitou.x--;
}
}
public class Shitou
{
public int x;
public int y;
public char Char = '0';
}
}

View File

@ -5,10 +5,9 @@ using System.Text;
namespace Debug.ModelingMusic;
public class Music
{
public static bool IsWhile = true;
//选择的模式
public static int mode = 0;
private static int mode = 0;
public Music()
{
Console.Clear();
@ -92,22 +91,7 @@ public class Music
Console.CursorVisible = false; // 隐藏光标
new Thread(PrintCharArray).Start();
if (mode == 1) { return; }
new Thread(() =>
{
while (IsWhile)
{
ConsoleKeyInfo consoleKeyInfo = Console.ReadKey(true);
if (consoleKeyInfo.Key == ConsoleKey.Escape)
{
IsWhile = false;
Thread.Sleep(500);
Console.Clear();
Environment.Exit(0);
return;
}
Thread.Sleep(500);
}
}).Start();
Program.KeyEndThread();
// 等待用户按下任意键后退出
}
@ -120,8 +104,6 @@ public class Music
//常量
public const char ShowChar = ' ';
//Y,X
public static readonly Point[,] chars = new Point[10, 30];
// public static readonly char[,] chars = new char[10, 30];
public static void Down(char a)
{
@ -141,21 +123,21 @@ public class Music
}
// 让字符从初始位置下落
int x = random.Next(0, chars.GetLength(1));
for (int y = 0; y < chars.GetLength(0); y++) // 注意这里使用consoleHeight
int x = random.Next(0, Program.chars.GetLength(1));
for (int y = 0; y < Program.chars.GetLength(0); y++) // 注意这里使用consoleHeight
{
// 移动到下一行
chars[y, x].Char = a;
Program.chars[y, x].Char = a;
if (y > 0)
{
chars[y - 1, x].Char = ShowChar;
Program.chars[y - 1, x].Char = ShowChar;
}
chars[y, x].Color = Color;
Program.chars[y, x].Color = Color;
Thread.Sleep(random.Next(60, 120)); // 控制字符下落速度
}
Thread.Sleep(50);
chars[chars.GetLength(0) - 1, x].Char = ShowChar;
Program.chars[Program.chars.GetLength(0) - 1, x].Char = ShowChar;
}
private static long time = 0;
// 定义一个方法来打印二维字符数组
@ -167,10 +149,11 @@ public class Music
int frameCount = 0;
double totalTime = 0;
int i; int j;
var a = chars.GetLength(0);
var b = chars.GetLength(1);
var a = Program.chars.GetLength(0);
var b = Program.chars.GetLength(1);
var buffer = new StringBuilder();
while (IsWhile)
ConsoleColor xiantiaoColor = random.Next(0, 2) == 0 ? ConsoleColor.Red : ConsoleColor.Blue;//线条的颜色
while (Program.IsWhile)
{
stopwatch.Restart();
Console.SetCursorPosition(0, 0);// 设定光标位置
@ -180,9 +163,10 @@ public class Music
{
for (j = 0; j < b; j++)
{
Console.ForegroundColor = chars[i, j].Color;
//Console.Write($" {chars[i, j].Char} ");
buffer.Append(chars[i, j].Char);
//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)
{
@ -192,21 +176,21 @@ public class Music
buffer.AppendLine();
//Console.WriteLine();
}
Console.ForegroundColor = ConsoleColor.DarkBlue;
Console.Write(buffer.ToString());
if (time % 1000 == 0)
{
Console.ForegroundColor = random.Next(0, 2) == 0 ? ConsoleColor.Red : ConsoleColor.Blue;
xiantiaoColor = 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++)
{
Console.Write("-");
buffer.Append('-');
}
Console.ResetColor();
Console.Write(buffer);
// 停止计时并计算本帧的处理时间
stopwatch.Stop();
double elapsedMilliseconds = stopwatch.Elapsed.TotalMilliseconds;
@ -214,21 +198,28 @@ public class Music
frameCount++;
// 计算帧率
double fps = frameCount / (totalTime / 1000.0);
Console.WriteLine($" {fps:0} FPS");
// 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));
//死方法 不知道为什么 控制台限制60 帧这样闪烁不会太严重
Thread.Sleep(150);
//这样闪烁不会太严重
Thread.Sleep(100);
}
}
private void setCharArry()
{
for (int i = 0; i < chars.GetLength(0); i++)
for (int i = 0; i < Program.chars.GetLength(0); i++)
{
for (int j = 0; j < chars.GetLength(1); j++)
for (int j = 0; j < Program.chars.GetLength(1); j++)
{
chars[i, j] = new Point(); // 初始化每个元素
Program.chars[i, j] = new Point(); // 初始化每个元素
}
// Console.WriteLine();
}

View File

@ -26,7 +26,7 @@ public class MusicDebug
{
new Thread(() => Music.Down(keyChar)).Start();
}
} while (Music.IsWhile);
} while (Program.IsWhile);
}
/// <summary>
@ -38,7 +38,7 @@ public class MusicDebug
Console.CursorVisible = false; // 隐藏光标
var random = new Random();
while (Music.IsWhile)
while (Program.IsWhile)
{
//随机生成 a-z和A-Z
var ch = (char)(random.Next(26) + 65);
@ -57,7 +57,7 @@ public class MusicDebug
Console.Clear();
var random = new Random();
while (Music.IsWhile)
while (Program.IsWhile)
{
for (int v = 0; v < ch.Length; v++)
{
@ -75,7 +75,7 @@ public class MusicDebug
Console.Clear();
var random = new Random();
while (Music.IsWhile)
while (Program.IsWhile)
{
int v = random.Next(0, ch.Length);
new Thread(() => Music.Down(ch[v])).Start();
@ -140,19 +140,19 @@ public class MusicDebug
static void DrawVolumeBar(float rmsVolume)
{
int barLength = Music.chars.GetLength(1); // 控制条长度
int barLength = Program.chars.GetLength(1); // 控制条长度
int filledLength = Math.Min((int)(rmsVolume * 1000), barLength);
for (int x = 0; x < Music.chars.GetLength(1); x++)
for (int x = 0; x < Program.chars.GetLength(1); x++)
{
Music.chars[9, x].Color = ConsoleColor.DarkCyan;
Program.chars[9, x].Color = ConsoleColor.DarkCyan;
if (x < filledLength)
{
Music.chars[9, x].Char = '#'; // 绘制音量条
Program.chars[9, x].Char = '#'; // 绘制音量条
}
else
{
Music.chars[9, x].Char = Music.ShowChar;
Program.chars[9, x].Char = Music.ShowChar;
}
}
}

View File

@ -32,42 +32,15 @@ public class Program
// 设置控制台输出编码,以支持复杂字符
Console.OutputEncoding = Encoding.UTF8;
//// 主循环
//while (true)
//{
// // 创建一个缓冲区
// char[,] buffer = new char[height, width];
// // 在缓冲区中绘制内容
// for (int y = 0; y < height; y++)
// {
// for (int x = 0; x < width; x++)
// {
// buffer[y, x] = (x + y) % 2 == 0 ? '#' : ' ';
// }
// }
// // 将缓冲区的内容输出到控制台
// Console.SetCursorPosition(0, 0);
// Console.Clear();
// StringBuilder sb = new StringBuilder();
// for (int y = 0; y < height; y++)
// {
// for (int x = 0; x < width; x++)
// {
// sb.Append(buffer[y, x]);
// }
// sb.AppendLine();
// }
// Console.Write(sb.ToString());
// // 控制刷新率
// Thread.Sleep(200);
//}
_ = new Program();
}
//Y,X
public static readonly Point[,] chars = new Point[10, 50];
public static bool IsWhile = true;
public static char ShowChar = ' ';
public Program()
{
// new Thread(MusicDebug.MusicRhythm).Start();
@ -75,15 +48,35 @@ public class Program
// new Debug.MinLong.KongLong();
// return;
Console.Clear();
int y = 0;
printfLine("请选择你要执行的操作:");
// printfLine(" 0.退出 ");
// printfLine(" 1.抽拉 ");
// printfLine(" 2.铁锈位置搜索引擎 ");
// printfLine(" 3.音乐调试 ");
// printfLine(" 4.小龙快跑 ");
string[] str = ["0 退出", "1 抽卡", "2 铁锈位置搜索引擎", "3 音乐调试", "4 小龙快跑"];
for (int i = 0; i < str.Length; i++)
{
printfLine(str[i]);
}
Console.SetCursorPosition(0, y);
do
{
printfLine("请选择你要执行的操作:"); printf(" 0.退出 ");
printf(" 1.抽拉 "); printf(" 2.铁锈位置搜索引擎 ");
Console.WriteLine();
var v = Console.ReadLine();
if (int.TryParse(v, out var i))
var v = Console.ReadKey(true);
switch (v.Key)
{
switch (i)
case ConsoleKey.UpArrow:
//设置光标位置
y = y == 0 ? y : y - 1;
break;
case ConsoleKey.DownArrow:
y = y + 1 == str.Length ? y : y + 1;
break;
case ConsoleKey.Enter:
switch (y)
{
case 0:
Environment.Exit(0);
@ -101,7 +94,33 @@ public class Program
printfLine("还没有这个功能");
break;
}
break;
}
// Console.WriteLine(v.Key);
Console.SetCursorPosition(0, y);
// {
} while (true);
}
public static void KeyEndThread()
{
new Thread(() =>
{
while (IsWhile)
{
ConsoleKeyInfo consoleKeyInfo = Console.ReadKey(true);
if (consoleKeyInfo.Key == ConsoleKey.Escape)
{
IsWhile = false;
Thread.Sleep(500);
Console.Clear();
Environment.Exit(0);
return;
}
Thread.Sleep(500);
}
// 等待用户按下任意键后退出
}).Start();
}
}