优化界面

This commit is contained in:
muqing 2024-10-12 10:47:46 +08:00
commit 0c0c3855d5
10 changed files with 284 additions and 29 deletions

7
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,7 @@
{
// 使 IntelliSense
//
// 访: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": []
}

View File

@ -24,6 +24,7 @@ public class ChouKa
{
if (number == 0)
{
_ = new Program();
break;
}
if (number > 0 && number < 11)

View File

@ -11,4 +11,8 @@
<PackageReference Include="NAudio" Version="2.2.1" />
</ItemGroup>
<ItemGroup>
<Folder Include="SheEat\" />
</ItemGroup>
</Project>

View File

@ -10,7 +10,7 @@ public class Main
public Main()
{
Console.WriteLine("MinLong");
// Console.WriteLine("MinLong");
ResetChars();//初始化 渲染界面
ResetLong();//初始化小龙
new Thread(PrintCharArray).Start();
@ -104,7 +104,6 @@ public class Main
//死方法 不知道为什么 控制台限制60 帧这样闪烁不会太严重
Thread.Sleep(100);
}
Console.WriteLine("任务失败!!!");
}
public class XiaoLong
@ -152,6 +151,16 @@ public class Main
return;
}
// Console.WriteLine(consoleKeyInfo.Key);
if (Program.IsWhile == false)
{
if (consoleKeyInfo.Key == ConsoleKey.Enter)
{
Console.Clear();
Program.IsWhile = true;
_ = new MinLong.Main();
}
return;
}
switch (consoleKeyInfo.Key)
{

View File

@ -1,6 +1,7 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace Debug.MinLong;
#pragma warning disable CS8981 // 该类型名称仅包含小写 ascii 字符。此类名称可能会成为该语言的保留值。
@ -19,6 +20,10 @@ public class Zhangaiwu
bool isjiafeng = false;//是否加分
for (int i = 1; i < x; i++)
{
if (Program.IsWhile == false)
{
break;
}
// System.Diagnostics.Debug.WriteLine("shitou");
// //测试 自动跳跃
// if (!xiaoLong.istiao && shitou.x == xiaoLong.x + Program.random.Next(1, 2))
@ -37,17 +42,35 @@ public class Zhangaiwu
}
else
{
SleepTime = 100;
Program.IsWhile = false;
shibai();
break;
}
Thread.Sleep(SleepTime);
if (shitou.x == xiaoLong.x && shitou.y == xiaoLong.y)
{
Program.IsWhile = false;
}
Program.chars[shitou.y, shitou.x].Char = Program.ShowChar;
--shitou.x;
}
}
public static void shibai()
{
Console.WriteLine("任务失败!!!");
Console.WriteLine("按 任意键重新开始游戏/Esc结束");
// Program.KeyEnd((a) =>
// {
// if (a.Key == ConsoleKey.Enter)
// {
// Console.Clear();
// Program.IsWhile = true;
// _ = new MinLong.Main();
// }
// else if (a.Key == ConsoleKey.Escape)
// {
// //销毁程序
// Environment.Exit(0);
// }
// });
}
public class Shitou

View File

@ -15,19 +15,26 @@ public class ModeSwitch
{
this.Enter = Enter;
str = a;
Console.WriteLine(title);
PrintCentered(title);
int windowWidth = Console.WindowWidth;
for (int i = 0; i < str.Length; i++)
{
Console.WriteLine($"{i}.{str[i]}");
// string paddedString = str[i].PadLeft((windowWidth + str[i].Length) / 2).PadRight(windowWidth);
// Console.WriteLine(paddedString);
PrintCentered($"{str[i]}");
}
do
{
Console.SetCursorPosition(CalculateActualLength($"{mode}.{str[mode]}") + 1, mode + 1);
Console.Write('>');
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(CalculateActualLength($"{mode}.{str[mode]}") + 1, mode + 1);
Console.Write(' ');
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)
{
@ -54,8 +61,6 @@ public class ModeSwitch
}
mode = int.Max(0, int.Min(str.Length - 1, mode));
} while (true);
//Console.CursorVisible = false;
}
private static int CalculateActualLength(string text)
{
@ -79,6 +84,39 @@ public class ModeSwitch
static void PrintCentered(string text)
{
// 获取控制台窗口的宽度
int consoleWidth = Console.WindowWidth;
// 计算文本的宽度,对于多字节字符(如中文),可能需要特殊处理
int textLength = GetTextDisplayLength(text);
// 计算左右两侧应该填充的空格数
int padding = (consoleWidth - textLength) / 2;
if (padding > 0)
{
// 打印左侧空格
Console.Write(new string(' ', padding));
}
// 打印文本
Console.WriteLine(text);
}
static int GetTextDisplayLength(string text)
{
// 默认每个字符占一个位置包括ASCII字符和非ASCII字符如中文
// return text.Length;
// * 如果需要更精确地计算字符宽度(例如考虑全角半角字符的区别),可以使用以下代码:
int length = 0;
foreach (char c in text)
{
// 假设所有非ASCII字符都是双宽度字符
length += char.IsHighSurrogate(c) || char.IsLowSurrogate(c) || c > 127 ? 2 : 1;
}
return length;
}
}

View File

@ -30,17 +30,6 @@ public class Program
// 设置控制台输出编码,以支持复杂字符
Console.OutputEncoding = Encoding.UTF8;
//// 更改控制台窗口的大小
//Console.SetWindowSize(100, 100);
//// 设置控制台屏幕缓冲区的大小以适应窗口大小
//if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
//{
// Console.SetBufferSize(100, 100);
//}
// Get the handle of the console window
Console.Clear();
Console.CursorVisible = false;
_ = new Program();
@ -55,8 +44,9 @@ public class Program
public Program()
{
string[] str = ["退出", "抽卡", "搜索引擎", "下落字符", "小龙快跑"];
var modeSwitch = new ModeSwitch("请选择你要执行的操作:", str, (a) =>
Console.Clear();
string[] str = ["退出", "抽卡", "搜索引擎", "下落字符", "小龙快跑", "贪吃蛇"];
var modeSwitch = new ModeSwitch("<--- 请选择你要执行的操作 --->", str, (a) =>
{
switch (a)
{
@ -64,7 +54,7 @@ public class Program
Environment.Exit(0);
break;
case 1:
_ = new ChouKa();
_ = new ChouKa();//抽卡
break;
case 2:
_ = new Mod();
@ -75,6 +65,9 @@ public class Program
case 4:
_ = new MinLong.Main();
break;
case 5:
_ = new Tanchishe.Main();
break;
default:
printfLine("还没有这个功能");
break;
@ -102,4 +95,28 @@ public class Program
// 等待用户按下任意键后退出
}).Start();
}
/// <summary>
/// 监听键盘事件 注意不要把此方法嵌套在循环里面
/// </summary>
/// <param name="action"></param>
public static void KeyEnd(Action<ConsoleKeyInfo> action)
{
ConsoleKeyInfo consoleKeyInfo = Console.ReadKey(true);
action.Invoke(consoleKeyInfo);
// 等待用户按下任意键后退出
}
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();
}
}
}
}

113
Tanchishe/Main.cs Normal file
View File

@ -0,0 +1,113 @@
using System;
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;
public class Main
{
public Main()
{
Console.Clear();
Program.ResetChars();
she.x = b / 2;
she.y = a / 2;
Program.chars[she.y, she.x].Char = she.Char;
//sx();
new Thread(PrintCharArray).Start();//打印
KeyThread();//键盘
new Zhixing(this).Run();
}
public class She
{
public int x, y;
public int direction = 0;//方向 0不动 1上 2右 -1下 -2左
public char Char = 'o';
}
public She she = new();
int i; int j;
int a = Program.chars.GetLength(0);
int b = Program.chars.GetLength(1);
private void PrintCharArray()
{
while (Program.IsWhile)
{
sx();
Thread.Sleep(50);
}
}
public void sx()
{
Console.SetCursorPosition(0, 0);// 设定光标位置
Console.Clear();
for (i = 0; i < a; i++)
{
for (j = 0; j < b; j++)
{
// Console.ForegroundColor = chars[i, j].Color;
Console.Write(Program.chars[i, j].Char);
}
Console.WriteLine();
}
}
/// <summary>
/// 全局键盘监听
/// </summary>
public void KeyThread()
{
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.W:
case ConsoleKey.UpArrow:
she.direction = 1;
//Program.chars[she.y--, she.x].Char = Program.ShowChar;
//Program.chars[she.y, she.x].Char = she.Char;
break;
case ConsoleKey.S:
case ConsoleKey.DownArrow:
Program.chars[she.y++, she.x].Char = Program.ShowChar;
Program.chars[she.y, she.x].Char = she.Char;
break;
case ConsoleKey.A:
case ConsoleKey.LeftArrow:
Program.chars[she.y, she.x--].Char = Program.ShowChar;
Program.chars[she.y, she.x].Char = she.Char;
break;
case ConsoleKey.D:
case ConsoleKey.RightArrow:
Program.chars[she.y, she.x++].Char = Program.ShowChar;
Program.chars[she.y, she.x].Char = she.Char;
break;
}
//sx();
}
// 等待用户按下任意键后退出
}).Start();
}
}

38
Tanchishe/Zhixing.cs Normal file
View File

@ -0,0 +1,38 @@
using Debug.MinLong;
using static Debug.Tanchishe.Main;
namespace Debug.Tanchishe;
public class Zhixing
{
Main main;
public Zhixing(Main main)
{
this.main = main;
new Thread(Run).Start();
}
public void Run()
{
//var chars = Program.chars;
while (Program.IsWhile)
{
var she = main.she;
switch (main.she.direction)
{
case 0:
break;
case 1:
if (she.y > 0) // ·ÀÖ¹Ô½½ç
{
Program.chars[main.she.y--, she.x].Char = Program.ShowChar;
Program.chars[main.she.y, she.x].Char = she.Char;
//main.sx();
}
break;
}
}
}
}

5
pyDebug.py Normal file
View File

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