【问题标题】:Controlling menu with the arrow keys and enter使用箭头键控制菜单并输入
【发布时间】:2018-04-05 02:39:23
【问题描述】:

我正在尝试创建一个 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);
        }
    }
}

【问题讨论】:

  • 这看起来不会是非常可重用的,并且(正如您已经遇到的)一种非常容易出错的方式。如果我发现了什么,我会看看,但我建议您尝试将其放入一个函数中,以便您可以多次使用它。

标签: c# colors arrow-keys


【解决方案1】:

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

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 将只是函数返回时所选选项的索引(或-1,如果用户按下了转义键)。您可能需要添加其他参数,例如横幅文本(“选择课程”)或格式选项。

应该是这样的:

您当然可以添加额外的标记,例如 _these_&gt; those &lt;,以进一步突出当前选择。

【讨论】:

  • 如果您的选项都不是“返回”,请记住取消(例如按 Esc):case(ConsoleKey.Escape) return -1;
  • 好主意,我会补充的。
  • 你们是传奇 非常感谢你们,我希望有一天我能写出这么好的代码
  • @Csharpnoob61 多年的经验。编程是理论只能告诉你方法的事情之一——变得更好伴随着犯错和学习如何在下一次做得更好:)
  • @NoamSuissa 在 Java 中它似乎是一个控制代码:stackoverflow.com/questions/1001335/…
猜你喜欢
  • 2015-11-06
  • 1970-01-01
  • 2011-05-20
  • 1970-01-01
  • 1970-01-01
  • 2011-05-07
  • 2021-11-19
  • 1970-01-01
相关资源
最近更新 更多