2013-12-21 91 views
0

所以我想要做的是在C#中創建一個簡單的基於文本的RPG遊戲。在向玩家詢問他/她想使用什麼武器之後,如果用戶按下無效鍵,我希望能夠再次啓動循環。如何在用戶按錯了鍵後再次啓動循環?

//Runs battle interactive 
Console.WriteLine(""); 
Console.WriteLine("You have encountered a simple guard! He deals 2 damage per attack and has 1 HP."); 
Console.WriteLine("You currently have: " + Program.Inventory); 
Console.WriteLine("Choose a weapon!"); 
var input2 = Console.ReadKey(); 

//Key checker for items 
switch (input2.Key) 
{ 
    case ConsoleKey.D1: 
     Console.WriteLine(""); 
     if (Items.iniFists == true) 
     { 
      Console.WriteLine("You have attacked with your Fists for 1 DMG!"); 
     }else 
     { 
      //this will never run, just a placeholder 
      Console.WriteLine("You Don't have your fists!"); 
      switch (input2.Key) 
{ 
    case ConsoleKey.D1: 
     Console.WriteLine(""); 
     if (Items.iniFists == true) 
     { 
      Console.WriteLine("You have attacked with your Fists for 1 DMG!"); 
     }else 
     { 
      //this will never run, just a placeholder 
      Console.WriteLine("You Don't have your fists!"); 
     } 
     break; 
    case ConsoleKey.D2: 
     Console.WriteLine(""); 
     if (Items.iniLongsword == true) 
     { 
     Console.WriteLine("You have chosen to attack with the Longsword for 2 DMG!"); 
     }else 
     { 
      Console.WriteLine("You don't have a longsword!"); 
     } 
     break; 
    case ConsoleKey.D3: 
     Console.WriteLine(""); 
     if (Items.iniBow == true) 
     { 
      Console.WriteLine("You have chosen to attack with the Bow for 3 DMG!"); 
     } 
     else 
     { 
      Console.WriteLine("You don't have a Bow!"); 
     } 
     break; 
    case ConsoleKey.D4: 
     Console.WriteLine(""); 
     if (Items.iniLightstaff == true) 
     { 
      Console.WriteLine("You have chosen to attack with the Lightstaff for 4 DMG!"); 
     } 
     else 
     { 
      Console.WriteLine("You don't have a Lightstaff!"); 
     } 
     break; 
    case ConsoleKey.D5: 
     Console.WriteLine(""); 
     Console.WriteLine("You can't attack with an Apple!"); 
     break; 
    case ConsoleKey.D6: 
     Console.WriteLine(""); 
     Console.WriteLine("You can't attack with a Golden Key!"); 
     break; 
    case ConsoleKey.D7: 
     Console.WriteLine(""); 
     Console.WriteLine("You can't attack with a Steak!"); 
     break; 
} 
     } 
     break; 
    case ConsoleKey.D2: 
     Console.WriteLine(""); 
     if (Items.iniLongsword == true) 
     { 
     Console.WriteLine("You have chosen to attack with the Longsword for 2 DMG!"); 
     }else 
     { 
      Console.WriteLine("You don't have a longsword!"); 
     } 
     break; 
    case ConsoleKey.D3: 
     Console.WriteLine(""); 
     if (Items.iniBow == true) 
     { 
      Console.WriteLine("You have chosen to attack with the Bow for 3 DMG!"); 
     } 
     else 
     { 
      Console.WriteLine("You don't have a Bow!"); 
     } 
     break; 
    case ConsoleKey.D4: 
     Console.WriteLine(""); 
     if (Items.iniLightstaff == true) 
     { 
      Console.WriteLine("You have chosen to attack with the Lightstaff for 4 DMG!"); 
     } 
     else 
     { 
      Console.WriteLine("You don't have a Lightstaff!"); 
     } 
     break; 
    case ConsoleKey.D5: 
     Console.WriteLine(""); 
     Console.WriteLine("You can't attack with an Apple!"); 
     break; 
    case ConsoleKey.D6: 
     Console.WriteLine(""); 
     Console.WriteLine("You can't attack with a Golden Key!"); 
     break; 
    case ConsoleKey.D7: 
     Console.WriteLine(""); 
     Console.WriteLine("You can't attack with a Steak!"); 
     break; 
} 

例如,如果玩家擊中7鍵,遊戲會告訴他們他們不能用牛排攻擊。在告訴玩家之後,我該如何讓玩家選擇其他物品?

+1

在我看來,你已經張貼了太多的代碼。 –

+0

@UweKeim對不起:/ – AgentMcBride

+0

@AgentMcBride:你可以使用wile(true)循環,看我的答案。 –

回答

2

這裏被包裹在一個循環代碼:

bool correct = false; 
do 
{ 
    Console.WriteLine(""); 
    Console.WriteLine("You have encountered a simple guard! He deals 2 damage per attack and has 1 HP."); 
    Console.WriteLine("You currently have: " + Program.Inventory); 
    Console.WriteLine("Choose a weapon!"); 
    var input2 = Console.ReadKey(); 

    correct = true; 
    //Key checker for items 
    switch (input2.Key) 
    { 
     case ConsoleKey.D1: 
      Console.WriteLine(""); 
      if (Items.iniFists == true) 
      { 
       Console.WriteLine("You have attacked with your Fists for 1 DMG!"); 
      } 
      else 
      { 
       //this will never run, just a placeholder 
       Console.WriteLine("You Don't have your fists!"); 
       switch (input2.Key) 
       { 
        case ConsoleKey.D1: 
         Console.WriteLine(""); 
         if (Items.iniFists == true) 
         { 
          Console.WriteLine("You have attacked with your Fists for 1 DMG!"); 
         } 
         else 
         { 
          //this will never run, just a placeholder 
          Console.WriteLine("You Don't have your fists!"); 
         } 
         break; 
        case ConsoleKey.D2: 
         Console.WriteLine(""); 
         if (Items.iniLongsword == true) 
         { 
          Console.WriteLine("You have chosen to attack with the Longsword for 2 DMG!"); 
         } 
         else 
         { 
          Console.WriteLine("You don't have a longsword!"); 
         } 
         break; 
        case ConsoleKey.D3: 
         Console.WriteLine(""); 
         if (Items.iniBow == true) 
         { 
          Console.WriteLine("You have chosen to attack with the Bow for 3 DMG!"); 
         } 
         else 
         { 
          Console.WriteLine("You don't have a Bow!"); 
         } 
         break; 
        case ConsoleKey.D4: 
         Console.WriteLine(""); 
         if (Items.iniLightstaff == true) 
         { 
          Console.WriteLine("You have chosen to attack with the Lightstaff for 4 DMG!"); 
         } 
         else 
         { 
          Console.WriteLine("You don't have a Lightstaff!"); 
         } 
         break; 
        case ConsoleKey.D5: 
         Console.WriteLine(""); 
         Console.WriteLine("You can't attack with an Apple!"); 
         break; 
        case ConsoleKey.D6: 
         Console.WriteLine(""); 
         Console.WriteLine("You can't attack with a Golden Key!"); 
         break; 
        case ConsoleKey.D7: 
         Console.WriteLine(""); 
         Console.WriteLine("You can't attack with a Steak!"); 
         break; 
       } 
      } 
      break; 
     case ConsoleKey.D2: 
      Console.WriteLine(""); 
      if (Items.iniLongsword == true) 
      { 
       Console.WriteLine("You have chosen to attack with the Longsword for 2 DMG!"); 
      } 
      else 
      { 
       Console.WriteLine("You don't have a longsword!"); 
      } 
      break; 
     case ConsoleKey.D3: 
      Console.WriteLine(""); 
      if (Items.iniBow == true) 
      { 
       Console.WriteLine("You have chosen to attack with the Bow for 3 DMG!"); 
      } 
      else 
      { 
       Console.WriteLine("You don't have a Bow!"); 
      } 
      break; 
     case ConsoleKey.D4: 
      Console.WriteLine(""); 
      if (Items.iniLightstaff == true) 
      { 
       Console.WriteLine("You have chosen to attack with the Lightstaff for 4 DMG!"); 
      } 
      else 
      { 
       Console.WriteLine("You don't have a Lightstaff!"); 
      } 
      break; 
     case ConsoleKey.D5: 
      Console.WriteLine(""); 
      Console.WriteLine("You can't attack with an Apple!"); 
      break; 
     case ConsoleKey.D6: 
      Console.WriteLine(""); 
      Console.WriteLine("You can't attack with a Golden Key!"); 
      break; 
     case ConsoleKey.D7: 
      Console.WriteLine(""); 
      Console.WriteLine("You can't attack with a Steak!"); 
      break; 
     default: 
      correct = false; 
      break; 
    } 
} 
while (!correct); 
+0

對不起,我對編程有點新了,究竟在哪裏呢? – AgentMcBride

+0

@AgentMcBride將你的switch語句的循環包裝起來。 – kenny

+0

第一個「...」是什麼代碼應該去的地方? – AgentMcBride

0

示例代碼:

string key; 
do { 

    key = Console.Read(); 
    if(key == ..) { 
    } 
    else if(key == ...) { 
    } 
    else if(..) { 
    } 

}while(key == WRONG_KEY); 
0

您需要打印出「請按其它鍵」,然後添加一個新的輸入變量和switch語句。這樣用戶可以按兩次鍵7。當用戶第一次按下按鈕時,第一個開關將用於做任何你想做的事情。第二個switch語句將控制用戶再次按下該鍵時發生的情況。

  var input2 = Console.ReadKey(); 

      //Key checker for items 
      switch (input2.Key) 
      { 
       //CODE HERE (PRINT PLEASE PRESS ANOTHER KEY) 
      } 

      var input3 = Console.ReadKey(); 
      switch (input3.Key) 
      { 
       //CODE HERE 
      } 
0

您可以使用while(true)來解決問題。

如果您使用while(true)它將迭代infinite次,但如果用戶輸入無效輸入,則應確保quit循環。

如果用戶輸入無效輸入,那麼沒有切換大小的數值,那麼它進入default塊,在默認塊中可以使用return語句來退出循環。

示例代碼:

Console.WriteLine("Please enter key 1-5"); 
    int key =Convert.Int32(Console.ReadLine()); 

    while (true) 
    { 
     switch (key) 
     { 
      case 1: Console.WriteLine("ur option "+key); 
       break; 
      case 2: Console.WriteLine("ur option " + key); 
       break; 
      case 3: Console.WriteLine("ur option " + key); 
       break; 
      case 4: Console.WriteLine("ur option " + key); 
       break; 
      case 5: Console.WriteLine("ur option " + key); 
       break; 
      default: 
       Console.WriteLine("invalid choice ,quitting the loop"); 
       return; 

     } 
     Console.WriteLine("Please enter key 1-5"); 
     key = Console.ReadLine(); 
    } 
    Console.WriteLine("outside loop"); 

完整代碼:

  Console.WriteLine(""); 
      Console.WriteLine("You have encountered a simple guard! He deals 2 damage per attack and has 1 HP."); 
      Console.WriteLine("You currently have: " + Program.Inventory); 
      Console.WriteLine("Choose a weapon!"); 
      var input2 = Console.ReadKey(); 

      //Key checker for items 
      while(true) 
      { 
      switch (input2.Key) 
      { 
       case ConsoleKey.D1: 
        Console.WriteLine(""); 
        if (Items.iniFists == true) 
        { 
         Console.WriteLine("You have attacked with your Fists for 1 DMG!"); 
        }else 
        { 
         //this will never run, just a placeholder 
         Console.WriteLine("You Don't have your fists!"); 
         switch (input2.Key) 
      { 
       case ConsoleKey.D1: 
        Console.WriteLine(""); 
        if (Items.iniFists == true) 
        { 
         Console.WriteLine("You have attacked with your Fists for 1 DMG!"); 
        }else 
        { 
         //this will never run, just a placeholder 
         Console.WriteLine("You Don't have your fists!"); 
        } 
        break; 
       case ConsoleKey.D2: 
        Console.WriteLine(""); 
        if (Items.iniLongsword == true) 
        { 
        Console.WriteLine("You have chosen to attack with the Longsword for 2 DMG!"); 
        }else 
        { 
         Console.WriteLine("You don't have a longsword!"); 
        } 
        break; 
       case ConsoleKey.D3: 
        Console.WriteLine(""); 
        if (Items.iniBow == true) 
        { 
         Console.WriteLine("You have chosen to attack with the Bow for 3 DMG!"); 
        } 
        else 
        { 
         Console.WriteLine("You don't have a Bow!"); 
        } 
        break; 
       case ConsoleKey.D4: 
        Console.WriteLine(""); 
        if (Items.iniLightstaff == true) 
        { 
         Console.WriteLine("You have chosen to attack with the Lightstaff for 4 DMG!"); 
        } 
        else 
        { 
         Console.WriteLine("You don't have a Lightstaff!"); 
        } 
        break; 
       case ConsoleKey.D5: 
        Console.WriteLine(""); 
        Console.WriteLine("You can't attack with an Apple!"); 
        break; 
       case ConsoleKey.D6: 
        Console.WriteLine(""); 
        Console.WriteLine("You can't attack with a Golden Key!"); 
        break; 
       case ConsoleKey.D7: 
        Console.WriteLine(""); 
        Console.WriteLine("You can't attack with a Steak!"); 
        break; 
      } 
        } 
        break; 
       case ConsoleKey.D2: 
        Console.WriteLine(""); 
        if (Items.iniLongsword == true) 
        { 
        Console.WriteLine("You have chosen to attack with the Longsword for 2 DMG!"); 
        }else 
        { 
         Console.WriteLine("You don't have a longsword!"); 
        } 
        break; 
       case ConsoleKey.D3: 
        Console.WriteLine(""); 
        if (Items.iniBow == true) 
        { 
         Console.WriteLine("You have chosen to attack with the Bow for 3 DMG!"); 
        } 
        else 
        { 
         Console.WriteLine("You don't have a Bow!"); 
        } 
        break; 
       case ConsoleKey.D4: 
        Console.WriteLine(""); 
        if (Items.iniLightstaff == true) 
        { 
         Console.WriteLine("You have chosen to attack with the Lightstaff for 4 DMG!"); 
        } 
        else 
        { 
         Console.WriteLine("You don't have a Lightstaff!"); 
        } 
        break; 
       case ConsoleKey.D5: 
        Console.WriteLine(""); 
        Console.WriteLine("You can't attack with an Apple!"); 
        break; 
       case ConsoleKey.D6: 
        Console.WriteLine(""); 
        Console.WriteLine("You can't attack with a Golden Key!"); 
        break; 
       case ConsoleKey.D7: 
        Console.WriteLine(""); 
        Console.WriteLine("You can't attack with a Steak!"); 
        break; 
       default: Console.WriteLine("Invalid Option Qutting loop"); 
         return; 

      } 
      Console.WriteLine("Choose a weapon!"); 
      var input2 = Console.ReadKey(); 
      } 
相關問題