C# - 用户定义他们希望用于程序的键绑定



我目前正在制作一个 C# 2 玩家剪刀石头布游戏,我被困住了怎么办。 基本上,就目前而言,用户输入他们想要的石头,纸和剪刀键。 我将这些键存储在变量中,以便程序记住它们。问题是我找不到或想不到程序如何检测这些键在游戏本身启动后是如何按下的。 我想可能是枚举或将它们存储在数组中?

不确定,这是我到目前为止所拥有的。

static bool winner = false;
static string player1;
static string player2;
static ConsoleKeyInfo Rock1;
static ConsoleKeyInfo Rock2; //variables for objects and players names etc
static ConsoleKeyInfo Scissors1;
static ConsoleKeyInfo Scissors2;
static ConsoleKeyInfo Paper1;
static ConsoleKeyInfo Paper2;
static void Main(string[] args)
{
do
{
PlayerChoosesNameAndKeys();
game();                       //main game loop
} while (winner == false);
}
static public void PlayerChoosesNameAndKeys()
{
for (int i = 1; i <= 2; i++)
{
Console.WriteLine("Player {0} chooses there name:", i);
if (i == 1)
{
player1 = Console.ReadLine();                            //players choose names
Console.WriteLine("Player 1's name is: {0}", player1);
}
else
{
player2 = Console.ReadLine();
Console.WriteLine("Player 2's name is: {0}", player2);
}
}
Console.WriteLine("Players now choose the keys they wish to represent the different objects");

for (int i = 0; i <= 5; i++)
{
if (i == 0)
{
Console.WriteLine("{0} choose what key Rock is", player1);
Rock1 = Console.ReadKey();
Console.WriteLine();
Console.WriteLine("Press enter to confirm");
Console.ReadLine();
}
else
{
if (i == 1)
{
Console.WriteLine("{0} choose what key Paper is", player1);
Paper1 = Console.ReadKey();
Console.WriteLine();
Console.WriteLine("Press enter to confirm");
Console.ReadLine();
}
if (i == 2)                                   //selection to decide what keybinds players want 
{
Console.WriteLine("{0} choose what Key Sisscors is", player1);
Scissors1 = Console.ReadKey();
Console.WriteLine();
Console.WriteLine("Press enter to confirm");
Console.ReadLine();
}
if (i == 3)
{
Console.WriteLine("{0} choose what key Rock is", player2);
Rock2 = Console.ReadKey();
Console.WriteLine();
Console.WriteLine("Press enter to confirm");
Console.ReadLine();
}
if (i == 4)
{
Console.WriteLine("{0} choose what key Paper is", player2);
Paper2 = Console.ReadKey();
Console.WriteLine();
Console.WriteLine("Press enter to confirm");
Console.ReadLine();
}
if (i == 5)
{
Console.WriteLine("{0} choose what Key Sisscors is", player2);
Scissors2 = Console.ReadKey();
Console.WriteLine();
Console.WriteLine("Press enter to confirm");
Console.ReadLine();
}
}  
}
static public void game()
{
int score1 = 0;
int score2 = 0;
int count = 1;
//actual game 
do
{
Console.WriteLine("Round {0}", count);
Console.WriteLine("Rock...");
Console.WriteLine("Paper...");
Console.WriteLine("Scissors...");
Console.WriteLine("{0} input Object!: ",player1);
ConsoleKeyInfo input = Console.ReadKey();
Console.WriteLine();
if (input.Key == ConsoleKey.Rock1)
{
}
} while (score1 != 10 || score2 != 10);
}

您可以简单地将用户选择保存为字符(例如char Rock(; 然后使用ConsoleKeyInfo.KeyChar

if (input.KeyChar == Rock)
{
}

例如:

char Rock = 'r'; // default value
Console.WriteLine("{0} choose what key Rock is", player1);
Rock = Console.ReadKey().KeyChar;

或者只需使用您保存的密钥检查输入的密钥:

Console.WriteLine("{0} choose what key Rock is", player1);
ConsoleKeyInfo Rock = Console.ReadKey();

然后:

ConsoleKeyInfo input = Console.ReadKey();
if (input.Key == Rock.Key)
{
}

最新更新