使用箭头键控制菜单并输入

Csh*_*b61 5 c# colors arrow-keys

我试图创建一个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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Man*_*mer 7

而不是手动编写每个组合,而是执行以下操作:使用辅助程序类完成诸如此类的多项选择操作之类的常见任务,传递一组选项并等待用户做出选择。

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

例如,上面的一个可以这样使用:

int selectedClass = ConsoleHelper.MultipleChoice(true, "Warrior", "Bard", "Mage", "Archer", 
    "Thief", "Assassin", "Cleric", "Paladin", "etc.");
Run Code Online (Sandbox Code Playgroud)

selectedClass当函数返回时(或者-1如果用户按了转义),它将只是所选选项的索引。您可能需要添加其他参数,例如横幅文本(“选择类别”)或格式选项。

应该看起来像这样:

控制台输出

您当然可以添加其他标记,例如_these_> those <进一步突出显示当前选择。

  • 并记住如果没有一个选项是“ Back”,则要取消(如按Esc键):case(ConsoleKey.Escape)返回-1; (2认同)