2017-10-24 6 views
2

私はRPGを作成しようとしていました。私はクラスを選択できるメニューに問題があります。 私は、矢印キーで特定のクラスへの方向を制御できるメニューを作成しようとしていましたが、実際のゲームのように前景色でハイライト表示されます。あなたがいるものが強調表示されます。矢印キーでメニューを制御して入力してください

私の問題は、矢印キーを押したときに矢印キーの位置を指定できないことです。私は最初の場所に行くことができ、別の問題は、ユーザーがどこにいるかを示すためにrpgクラスを強調表示するときです。すべてのrpgクラスがフォアグラウンドカラーを取得します。私はConsole.Readを使い分けましたが、今はいつも押す必要がありますを入力して色を変更してください。

ここにコードがあります。あなたがコードを開いた後、私は自分の問題を理解するだろうと思います。

最高の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

これは非常にエラーが発生しやすい方法で再利用できないようです(既に遭遇したように)。私は何かを見つけたら見ていきますが、それを何度も使うことができるように関数に入れてみることをお勧めします。 –

答えて

0

は、ここで私は何をするのかであります選択。

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_または> those <のような追加のマークを追加することができます

Console output

は、次のようになります。

+1

あなたのオプションがどれも "Back"でない場合、キャンセル(Escを押したように)について覚えておいてください。case(ConsoleKey.Escape)-1を返します。 –

+0

良いアイデア、私はそれを追加します。 –

+0

皆さん、伝説ありがとうございます。いつかよかったです。 – Csharpnoob61

関連する問題