我正在尝试创建一个RPG。我的菜单有问题,我可以选择一个班级。 我正在尝试创建一个菜单,您可以使用箭头键控制指向特定类的方向,然后以前景颜色红色突出显示,就像在真实游戏中,当您想要选择某些内容时,只需使用箭头键和你所关注的事情得到了强调。
我的问题是当我按箭头键时无法指定箭头键的位置。我只能去第一个位置而另一个问题是当我突出显示rpg类以向用户显示他所在的位置时,所有rpg类都获得前景色。我使用Console.Read
来分隔它们,但现在我必须按 Enter 来更改颜色。
这是代码。我想在你打开代码之后你会理解我的问题。
最好的问候Csharpnoob61。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Enter_Eingabe
{
class Program
{
static void Main(string[] args)
{
//ints
int char_HP_Current = 20;
int char_HP_Full = 100;
double char_Exp_Current = 10;
double char_Exp_Full = 100;
int char_Level_Current = 1;
int GameOver = 0;
int char_Move_Left_Right = 0;
int char_Move_Up_Down = 8;
int Error = 0;
int Resting_Time = 0;
int Attack_Bonus = 0;
int Speech_Bonus = 0;
int Sneak_Bonus = 0;
int Armor_Bonus = 0;
int Casting_Bonus = 0;
//Strings
string char_Name = "";
string Current_Command;
string char_Status = "";
string char_Class;
string test;
Console.Clear();
Console.SetCursorPosition(0, 8);
do
{
string text = "Guardian";
Console.SetCursorPosition(15, 8);
Console.WriteLine(text);
Console.SetCursorPosition(45, 8);
Console.WriteLine("Paladin");
Console.SetCursorPosition(30, 8);
Console.WriteLine("Outlaw");
ConsoleKeyInfo KeyInfo;
KeyInfo = Console.ReadKey(true);
switch (KeyInfo.Key)
{
//Player Controlls
case ConsoleKey.RightArrow:
Console.SetCursorPosition(0, 8);
if (char_Move_Left_Right < 78)
{
char_Move_Left_Right+=14;
Console.SetCursorPosition(char_Move_Left_Right, char_Move_Up_Down);
Console.WriteLine("_");
Console.SetCursorPosition(char_Move_Left_Right - 1, char_Move_Up_Down);
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine("_");
Console.ForegroundColor = ConsoleColor.White;
if (char_Move_Left_Right == 14)
{
if (char_Move_Up_Down == 8)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.SetCursorPosition(15, 8);
Console.WriteLine(text);
Console.Read();
}
Console.ForegroundColor = ConsoleColor.White;
}
}
if (char_Move_Left_Right == 29)
{
if (char_Move_Up_Down == 8)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.SetCursorPosition(30,8);
Console.WriteLine("Outlaw");
Console.Read();
}
Console.ForegroundColor = ConsoleColor.White;
}
if (char_Move_Left_Right == 44)
{
if (char_Move_Up_Down == 8)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.SetCursorPosition(45, 8);
Console.WriteLine("Paladin");
Console.ReadLine();
}
Console.ForegroundColor = ConsoleColor.White;
}
break;
case ConsoleKey.LeftArrow:
if (char_Move_Left_Right > 1)
{
char_Move_Left_Right--;
Console.SetCursorPosition(char_Move_Left_Right, char_Move_Up_Down);
Console.WriteLine("_");
Console.SetCursorPosition(char_Move_Left_Right + 1, char_Move_Up_Down);
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine("_");
Console.ForegroundColor = ConsoleColor.White;
}
else { }
break;
case ConsoleKey.UpArrow:
if (char_Move_Up_Down > 3)
{
char_Move_Up_Down--;
Console.SetCursorPosition(char_Move_Left_Right, char_Move_Up_Down);
Console.WriteLine("_");
Console.SetCursorPosition(char_Move_Left_Right, char_Move_Up_Down + 1);
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine("_");
Console.ForegroundColor = ConsoleColor.White;
}
else { }
break;
case ConsoleKey.DownArrow:
if (char_Move_Up_Down < 21)
{
char_Move_Up_Down++;
Console.SetCursorPosition(char_Move_Left_Right, char_Move_Up_Down);
Console.WriteLine("_");
Console.SetCursorPosition(char_Move_Left_Right, char_Move_Up_Down - 1);
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine("_");
Console.ForegroundColor = ConsoleColor.White;
}
else { }
break;
}
}while (Error == 0);
}
}
}
答案 0 :(得分:4)
不是手工编写每个组合,而是我要做的事情:使用辅助类来处理常见任务,例如这些多选操作,传递一组选项并等待用户进行选择。 / p>
public class ConsoleHelper
{
public static int MultipleChoice(bool canCancel, params string[] options)
{
const int startX = 15;
const int startY = 8;
const int optionsPerLine = 3;
const int spacingPerLine = 14;
int currentSelection = 0;
ConsoleKey key;
Console.CursorVisible = false;
do
{
Console.Clear();
for (int i = 0; i < options.Length; i++)
{
Console.SetCursorPosition(startX + (i % optionsPerLine) * spacingPerLine, startY + i / optionsPerLine);
if(i == currentSelection)
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(options[i]);
Console.ResetColor();
}
key = Console.ReadKey(true).Key;
switch (key)
{
case ConsoleKey.LeftArrow:
{
if (currentSelection % optionsPerLine > 0)
currentSelection--;
break;
}
case ConsoleKey.RightArrow:
{
if (currentSelection % optionsPerLine < optionsPerLine - 1)
currentSelection++;
break;
}
case ConsoleKey.UpArrow:
{
if (currentSelection >= optionsPerLine)
currentSelection -= optionsPerLine;
break;
}
case ConsoleKey.DownArrow:
{
if (currentSelection + optionsPerLine < options.Length)
currentSelection += optionsPerLine;
break;
}
case ConsoleKey.Escape:
{
if (canCancel)
return -1;
break;
}
}
} while (key != ConsoleKey.Enter);
Console.CursorVisible = true;
return currentSelection;
}
}
上面的例子可以像这样使用:
int selectedClass = ConsoleHelper.MultipleChoice(true, "Warrior", "Bard", "Mage", "Archer",
"Thief", "Assassin", "Cleric", "Paladin", "etc.");
函数返回时, selectedClass
将只是所选选项的索引(如果用户按下escape,则为-1
)。您可能希望添加其他参数,例如横幅文本(“选择类”)或格式化选项。
应该是这样的:
您当然可以添加其他标记,例如_these_
或> those <
,以进一步突出显示当前选择。