2017-10-24 2 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

관련 문제