ModelingDebug/Tanchishe/Main.cs

114 lines
3.2 KiB
C#

namespace Debug.Tanchishe;
/// <summary>
/// 贪吃蛇主入口
/// </summary>
public class Main
{
readonly int MaxY;
readonly int MaxX;
public Main()
{
MaxY = Program.chars.GetLength(0);
MaxX = Program.chars.GetLength(1);
Console.Clear();
Program.ResetChars();
she.x = MaxY / 2;
she.y = MaxX / 2;
Program.chars[she.y, she.x].Char = she.Char;
new Thread(PrintCharArray).Start();//打印
KeyThread();//键盘
new Zhixing(this).Run();
}
/// <summary>
/// 蛇类
/// </summary>
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();
for (i = 0; i < MaxY; i++)
{
for (j = 0; j < MaxX; 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();
}
}