重构 MusicFrom 和 Main 类,优化字符数组处理逻辑,调整变量声明为 readonly,清理无用代码

This commit is contained in:
muqing 2025-01-15 16:47:02 +08:00
parent 66f4c5ffaf
commit 37e5c0b998
3 changed files with 30 additions and 42 deletions

View File

@ -33,22 +33,20 @@ public class MusicFrom
private const int STD_INPUT_HANDLE = -10; private const int STD_INPUT_HANDLE = -10;
private long time = 0; private long time = 0;
int MaxX = 0; readonly int MaxX;
int MaxY = 0; readonly int MaxY;
public MusicFrom() public MusicFrom()
{ {
setCharArry();
MaxY = Program.chars.GetLength(0);
MaxX = Program.chars.GetLength(1); MaxX = Program.chars.GetLength(1);
MaxY = Program.chars.GetLength(0);
SetCharArry();
new Thread(PrintCharArray).Start(); new Thread(PrintCharArray).Start();
new Thread(KeyboardMapping).Start(); new Thread(KeyboardMapping).Start();
new Thread(Run).Start(); new Thread(Run).Start();
// 恢复控制台输入模式 // 恢复控制台输入模式
// mode |= ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT; // mode |= ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
// SetConsoleMode(hStdin, mode); // SetConsoleMode(hStdin, mode);
} }
public void Run() public void Run()
@ -70,8 +68,8 @@ public class MusicFrom
DownKey('G', 17); DownKey('G', 17);
break; break;
case 3: case 3:
DownKey('M', 24); // DownKey('M', 24);
break; return;
case 4: case 4:
DownKey('J', 31); DownKey('J', 31);
break; break;
@ -118,8 +116,7 @@ public class MusicFrom
IntPtr hStdin = GetStdHandle(STD_INPUT_HANDLE); IntPtr hStdin = GetStdHandle(STD_INPUT_HANDLE);
// 获取当前控制台输入模式 // 获取当前控制台输入模式
uint mode; if (!GetConsoleMode(hStdin, out uint mode))
if (!GetConsoleMode(hStdin, out mode))
{ {
Console.WriteLine("无法获取控制台输入模式"); Console.WriteLine("无法获取控制台输入模式");
return; return;
@ -136,9 +133,6 @@ public class MusicFrom
int frameCount = 0; int frameCount = 0;
double totalTime = 0; double totalTime = 0;
int i; int j; int i; int j;
// 获取字符数组的行数和列数
var a = Program.chars.GetLength(0);
var b = Program.chars.GetLength(1);
var buffer = new StringBuilder(); var buffer = new StringBuilder();
while (Program.IsWhile) while (Program.IsWhile)
{ {
@ -146,9 +140,9 @@ public class MusicFrom
Console.SetCursorPosition(0, 0);// 设定光标位置 Console.SetCursorPosition(0, 0);// 设定光标位置
Console.Clear(); Console.Clear();
buffer.Clear(); buffer.Clear();
for (i = 0; i < a; i++) for (i = 0; i < MaxY; i++)
{ {
for (j = 0; j < b; j++) for (j = 0; j < MaxX; j++)
{ {
buffer.Append(Program.chars[i, j].Char); buffer.Append(Program.chars[i, j].Char);
time++; time++;
@ -162,7 +156,7 @@ public class MusicFrom
Console.ForegroundColor = ConsoleColor.DarkBlue; Console.ForegroundColor = ConsoleColor.DarkBlue;
Console.Write(buffer.ToString()); Console.Write(buffer.ToString());
buffer.Clear(); buffer.Clear();
for (i = 0; i < b; i++) for (i = 0; i < MaxX; i++)
{ {
buffer.Append('-'); buffer.Append('-');
} }
@ -172,18 +166,11 @@ public class MusicFrom
buffer.Clear(); buffer.Clear();
// int totalLength = 50; // int totalLength = 50;
int totalAs = jianpan.Length; int totalAs = jianpan.Length;
int spaceBetweenAs = (b - totalAs) / (totalAs - 1); int spaceBetweenAs = (MaxX - totalAs) / (totalAs - 1);
for (i = 0; i < totalAs; i++) for (i = 0; i < totalAs; i++)
{ {
buffer.Clear(); buffer.Clear();
// if (isDown[3] && i == 3)
// {
// // 显示空格键
// // ConsoleKey.Spacebar
// buffer.Append("Spacebar");
// continue;
// }
for (j = 0; j < spaceBetweenAs / 2; j++) for (j = 0; j < spaceBetweenAs / 2; j++)
{ {
buffer.Append(' '); buffer.Append(' ');
@ -230,7 +217,7 @@ public class MusicFrom
mode |= ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT; mode |= ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
SetConsoleMode(hStdin, mode); SetConsoleMode(hStdin, mode);
} }
private void setCharArry() private void SetCharArry()
{ {
for (int i = 0; i < Program.chars.GetLength(0); i++) for (int i = 0; i < Program.chars.GetLength(0); i++)
{ {

View File

@ -29,6 +29,7 @@ public class Program
public Program() public Program()
{ {
Console.Clear(); Console.Clear();
// ResetChars();
string[] str = ["抽卡", "搜索引擎", Music.title, "小龙快跑", "贪吃蛇", "铁锈助手(联机模块)[未开发]"]; string[] str = ["抽卡", "搜索引擎", Music.title, "小龙快跑", "贪吃蛇", "铁锈助手(联机模块)[未开发]"];
var modeSwitch = new ModeSwitch("请选择你要执行的操作", str, (a) => var modeSwitch = new ModeSwitch("请选择你要执行的操作", str, (a) =>
{ {

View File

@ -1,23 +1,22 @@
using System; namespace Debug.Tanchishe;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Debug.MinLong.Main;
namespace Debug.Tanchishe;
/// <summary>
/// 贪吃蛇主入口
/// </summary>
public class Main public class Main
{ {
readonly int MaxY;
readonly int MaxX;
public Main() public Main()
{ {
MaxY = Program.chars.GetLength(0);
MaxX = Program.chars.GetLength(1);
Console.Clear(); Console.Clear();
Program.ResetChars(); Program.ResetChars();
she.x = b / 2; she.x = MaxY / 2;
she.y = a / 2; she.y = MaxX / 2;
Program.chars[she.y, she.x].Char = she.Char; Program.chars[she.y, she.x].Char = she.Char;
//sx();
new Thread(PrintCharArray).Start();//打印 new Thread(PrintCharArray).Start();//打印
KeyThread();//键盘 KeyThread();//键盘
new Zhixing(this).Run(); new Zhixing(this).Run();
@ -25,6 +24,9 @@ public class Main
} }
/// <summary>
/// 蛇类
/// </summary>
public class She public class She
{ {
public int x, y; public int x, y;
@ -33,8 +35,6 @@ public class Main
} }
public She she = new(); public She she = new();
int i; int j; int i; int j;
int a = Program.chars.GetLength(0);
int b = Program.chars.GetLength(1);
private void PrintCharArray() private void PrintCharArray()
{ {
while (Program.IsWhile) while (Program.IsWhile)
@ -48,9 +48,9 @@ public class Main
{ {
Console.SetCursorPosition(0, 0);// 设定光标位置 Console.SetCursorPosition(0, 0);// 设定光标位置
Console.Clear(); Console.Clear();
for (i = 0; i < a; i++) for (i = 0; i < MaxY; i++)
{ {
for (j = 0; j < b; j++) for (j = 0; j < MaxX; j++)
{ {
// Console.ForegroundColor = chars[i, j].Color; // Console.ForegroundColor = chars[i, j].Color;
Console.Write(Program.chars[i, j].Char); Console.Write(Program.chars[i, j].Char);
@ -95,7 +95,7 @@ public class Main
break; break;
case ConsoleKey.A: case ConsoleKey.A:
case ConsoleKey.LeftArrow: case ConsoleKey.LeftArrow:
Program.chars[she.y, she.x--].Char = Program.ShowChar; Program.chars[she.y, she.x--].Char = Program.ShowChar;
Program.chars[she.y, she.x].Char = she.Char; Program.chars[she.y, she.x].Char = she.Char;
break; break;