代码是这样写的:
static void Main(string[] args)
{
//char s = Console.ReadKey(true).KeyChar;
Console.SetWindowSize(60, 30); //设置窗口大小
Console.SetBufferSize(61, 30); //设置缓冲区的宽高
Console.BackgroundColor = ConsoleColor.Red;
Console.Clear();
Console.ForegroundColor = ConsoleColor.Yellow;
int x = 0;
int y = 0;
int newx = 0;
int newy = 0;
Console.SetCursorPosition(0, 0);
Console.Write("■");
while (true)
{
x = newx;
y = newy;
char a = Console.ReadKey(true).KeyChar;
switch (a)
{
case 'a':
case 'A':
x -= 2; //同步光标位置
if (newx < 0)
{
newx = 0;
}
break;
case 'd':
case 'D':
newx += 2;//同步方块位置,x的临界值是58
if (newx > 59) //判断下一个位置是否合法,当x大于59时,超出了控制台预设的60个位置
{
newx = 58;//未实现临界值问题x无法等于临界值
}
break;
case 'w':
case 'W':
newy -= 1;
if (newy - 1 < 0)
{
newy = 0;
}
break;
case 's':
case 'S':
newy += 1;
if (newy + 1 > 30)
{
newy = 30;
}
break;
default:
break;
}
Console.SetCursorPosition(x, y);
Console.Write(" ");
Console.SetCursorPosition(newx, newy);
Console.Write("■");
}
}
static void Main(string[] args)
{
//char s = Console.ReadKey(true).KeyChar;
Console.SetWindowSize(60, 30); //设置窗口大小
Console.SetBufferSize(61, 30); //设置缓冲区的宽高
Console.BackgroundColor = ConsoleColor.Red;
Console.Clear();
Console.ForegroundColor = ConsoleColor.Yellow;
int x = 0;
int y = 0;
int newx = 0;
int newy = 0;
Console.SetCursorPosition(0, 0);
Console.Write("■");
while (true)
{
x = newx;
y = newy;
char a = Console.ReadKey(true).KeyChar;
switch (a)
{
case 'a':
case 'A':
x -= 2; //同步光标位置
if (newx < 0)
{
newx = 0;
}
break;
case 'd':
case 'D':
newx += 2;//同步方块位置,x的临界值是58
if (newx > 59) //判断下一个位置是否合法,当x大于59时,超出了控制台预设的60个位置
{
newx = 58;//未实现临界值问题x无法等于临界值
}
break;
case 'w':
case 'W':
newy -= 1;
if (newy - 1 < 0)
{
newy = 0;
}
break;
case 's':
case 'S':
newy += 1;
if (newy + 1 > 30)
{
newy = 30;
}
break;
default:
break;
}
Console.SetCursorPosition(x, y);
Console.Write(" ");
Console.SetCursorPosition(newx, newy);
Console.Write("■");
}
}