2025-01-15 08:47:02 +00:00
|
|
|
|
namespace Debug.Tanchishe;
|
2024-10-12 02:47:46 +00:00
|
|
|
|
|
2025-01-15 08:47:02 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 贪吃蛇主入口
|
|
|
|
|
/// </summary>
|
2024-10-12 02:47:46 +00:00
|
|
|
|
public class Main
|
|
|
|
|
{
|
2025-01-15 08:47:02 +00:00
|
|
|
|
readonly int MaxY;
|
|
|
|
|
readonly int MaxX;
|
2024-10-12 02:47:46 +00:00
|
|
|
|
public Main()
|
|
|
|
|
{
|
2025-01-15 08:47:02 +00:00
|
|
|
|
MaxY = Program.chars.GetLength(0);
|
|
|
|
|
MaxX = Program.chars.GetLength(1);
|
|
|
|
|
|
2024-10-12 02:47:46 +00:00
|
|
|
|
Console.Clear();
|
|
|
|
|
Program.ResetChars();
|
2025-01-15 08:47:02 +00:00
|
|
|
|
she.x = MaxY / 2;
|
|
|
|
|
she.y = MaxX / 2;
|
2024-10-12 02:47:46 +00:00
|
|
|
|
Program.chars[she.y, she.x].Char = she.Char;
|
|
|
|
|
new Thread(PrintCharArray).Start();//打印
|
|
|
|
|
KeyThread();//键盘
|
|
|
|
|
new Zhixing(this).Run();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-15 08:47:02 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 蛇类
|
|
|
|
|
/// </summary>
|
2024-10-12 02:47:46 +00:00
|
|
|
|
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;
|
|
|
|
|
private void PrintCharArray()
|
|
|
|
|
{
|
|
|
|
|
while (Program.IsWhile)
|
|
|
|
|
{
|
|
|
|
|
sx();
|
|
|
|
|
Thread.Sleep(50);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void sx()
|
|
|
|
|
{
|
|
|
|
|
Console.SetCursorPosition(0, 0);// 设定光标位置
|
|
|
|
|
Console.Clear();
|
2025-01-15 08:47:02 +00:00
|
|
|
|
for (i = 0; i < MaxY; i++)
|
2024-10-12 02:47:46 +00:00
|
|
|
|
{
|
2025-01-15 08:47:02 +00:00
|
|
|
|
for (j = 0; j < MaxX; j++)
|
2024-10-12 02:47:46 +00:00
|
|
|
|
{
|
|
|
|
|
// 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:
|
2025-01-15 08:47:02 +00:00
|
|
|
|
case ConsoleKey.LeftArrow:
|
2024-10-12 02:47:46 +00:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|