C# 中的控制台跳转和 运行
Console Jump and Run in C#
我想用 C# 在控制台中编写一个简单的 Jump 和 运行。像超级马里奥这样的东西,只是没有怪物。当我想更新 "player" 它并不总是工作或闪烁。
static int cursorX = 5;
static int cursorY = 10;
static void Main(string[] args)
{
Console.SetCursorPosition(cursorX, cursorY);
Console.Write("A");
while(true)
{
MovePlayer();
}
Console.ReadKey(true);
}
private static void MovePlayer()
{
if (Console.ReadKey().Key == ConsoleKey.RightArrow)
{
updateCursor(cursorX + 1, cursorY);
}
else if(Console.ReadKey().Key == ConsoleKey.LeftArrow)
{
updateCursor(cursorX - 1, cursorY);
}
}
private static void updateCursor(int x, int y)
{
Console.Clear();
Console.SetCursorPosition(x, y);
Console.Write("A");
}
}
你的角色 "A" 没有移动,因为 cursorX + 1
和 cursorX - 1
不会给它自己分配新的值(光标位置)。它只是从其当前值中添加 +1
并从中减去 -1
。您需要为 cursorX
分配新值。您需要使用 Increment operator (++)
和 Decrement operator (--)
.
private static void MovePlayer()
{
if (Console.ReadKey().Key == ConsoleKey.RightArrow)
{
updateCursor(cursorX++, cursorY);
}
else if (Console.ReadKey().Key == ConsoleKey.LeftArrow)
{
updateCursor(cursorX--, cursorY);
}
}
我想用 C# 在控制台中编写一个简单的 Jump 和 运行。像超级马里奥这样的东西,只是没有怪物。当我想更新 "player" 它并不总是工作或闪烁。
static int cursorX = 5;
static int cursorY = 10;
static void Main(string[] args)
{
Console.SetCursorPosition(cursorX, cursorY);
Console.Write("A");
while(true)
{
MovePlayer();
}
Console.ReadKey(true);
}
private static void MovePlayer()
{
if (Console.ReadKey().Key == ConsoleKey.RightArrow)
{
updateCursor(cursorX + 1, cursorY);
}
else if(Console.ReadKey().Key == ConsoleKey.LeftArrow)
{
updateCursor(cursorX - 1, cursorY);
}
}
private static void updateCursor(int x, int y)
{
Console.Clear();
Console.SetCursorPosition(x, y);
Console.Write("A");
}
}
你的角色 "A" 没有移动,因为 cursorX + 1
和 cursorX - 1
不会给它自己分配新的值(光标位置)。它只是从其当前值中添加 +1
并从中减去 -1
。您需要为 cursorX
分配新值。您需要使用 Increment operator (++)
和 Decrement operator (--)
.
private static void MovePlayer()
{
if (Console.ReadKey().Key == ConsoleKey.RightArrow)
{
updateCursor(cursorX++, cursorY);
}
else if (Console.ReadKey().Key == ConsoleKey.LeftArrow)
{
updateCursor(cursorX--, cursorY);
}
}