using System;
namespace ConsoleGame
{
class Program
{
static void Main(string[] args)
{
// Set console window size
Console.SetWindowSize(32, 32); // console size
int playerX = 0; // initial x position of the player
int playerY = 0; // initial y position of the player
while (true)
{
Console.Clear(); // clear the console screen
// Draw the game board
for (int y = 0; y < 30; y++)
{
for (int x = 0; x < 30; x++)
{
if (x == playerX && y == playerY)
{
Console.Write("P"); // draw the player
}
else
{
Console.Write("."); // draw empty space
}
}
Console.WriteLine();
}
// user input
ConsoleKeyInfo keyInfo = Console.ReadKey();
ConsoleKey key = keyInfo.Key;
// Move the player on user input
if (key == ConsoleKey.A && playerX > 0)
{
playerX--;
}
else if (key == ConsoleKey.D && playerX < 29)
{
playerX++;
}
else if (key == ConsoleKey.W && playerY > 0)
{
playerY--;
}
else if (key == ConsoleKey.S && playerY < 29)
{
playerY++;
}
}
}
}
}
我试图在我的c#控制台游戏中做到这一点,我无法按住我的按键绑定,所以即使我重新按下按键,它也会移动,例如当我现在运行它并按住S时,它会执行SSSSSSSSSSSS,然后什么时候我知道 P 的密钥将继续下降,直到写完我给它的所有 S。我该如何阻止这个,这样你就无法按住 P 我需要一次又一次地按下它。希望有人理解或者可以尝试看看我的意思
我想我理解你的问题,控制台应用程序和方法 ReadKey 并不意味着像游戏那样捕获输入..因此游戏引擎存在..
您面临的问题是由于键盘重复功能造成的,例如 如果我按住 K,它不会将单个 K 发送到控制台应用程序,而是将一堆 K 连续发送,如下所示:kkkkkkkkkkkkk
我有一个解决方法,可能有助于在读取按键后增加延迟,这应该忽略自动重复功能,从而使用户为每个移动实例重复按键 这不是一个完美的解决方案,因为它改变了游戏的响应能力
但我认为对于 consoleApp 来说这是最好的解决方案:
using System;
using System.Threading; // Import the Threading namespace
namespace ConsoleGame
{
class Program
{
static void Main(string[] args)
{
// Set console window size
Console.SetWindowSize(32, 32); // console size
int playerX = 0; // initial x position of the player
int playerY = 0; // initial y position of the player
while (true)
{
Console.Clear(); // clear the console screen
// Draw the game board
for (int y = 0; y < 30; y++)
{
for (int x = 0; x < 30; x++)
{
if (x == playerX && y == playerY)
{
Console.Write("P"); // draw the player
}
else
{
Console.Write("."); // draw empty space
}
}
Console.WriteLine();
}
// User input
ConsoleKeyInfo keyInfo = Console.ReadKey(true); // Use ReadKey(true) to prevent echoing the character
ConsoleKey key = keyInfo.Key;
// Move the player on user input
if (key == ConsoleKey.A && playerX > 0)
{
playerX--;
}
else if (key == ConsoleKey.D && playerX < 29)
{
playerX++;
}
else if (key == ConsoleKey.W && playerY > 0)
{
playerY--;
}
else if (key == ConsoleKey.S && playerY < 29)
{
playerY++;
}
// Introduce a small delay to mitigate key auto-repeat
Thread.Sleep(100); // Adjust the delay as needed
}
}
}
}
使用
Console.ReadKey(true)
true
参数抑制字符回显到控制台,使输入处理更清晰
对于控制台应用程序来说,更好的解决方案是将游戏转换为回合制,并要求用户输入命令来移动一个方块:
using System;
namespace ConsoleGame
{
class Program
{
static void Main(string[] args)
{
Console.SetWindowSize(32, 32); // Set console window size
int playerX = 0; // Initial x position of the player
int playerY = 0; // Initial y position of the player
while (true)
{
Console.Clear(); // Clear the console screen
// Draw the game board
for (int y = 0; y < 30; y++)
{
for (int x = 0; x < 30; x++)
{
if (x == playerX && y == playerY)
{
Console.Write("P"); // Draw the player
}
else
{
Console.Write("."); // Draw empty space
}
}
Console.WriteLine();
}
// Prompt the user for input
Console.WriteLine("Enter move (W, A, S, D) + Enter: ");
string command = Console.ReadLine().ToUpper(); // Read the command and convert to uppercase
// Move the player based on user input
if (command == "A" && playerX > 0)
{
playerX--;
}
else if (command == "D" && playerX < 29)
{
playerX++;
}
else if (command == "W" && playerY > 0)
{
playerY--;
}
else if (command == "S" && playerY < 29)
{
playerY++;
}
// The console will clear and redraw the board at the start of the next loop iteration
}
}
}
}