2016-04-20 12 views
1

Ich muss bestimmte Tasten (arrows.left und arrows.right) in meine Konsolenanwendung eingeben, ohne eine Schleife zu blockieren.Konsolenspezifische Tasteneingabe ohne Blockierung

Hier ist der Code:

while (fuel>0) { 
    moveAndGenerate(); 

    for (int i=0;i<road.GetLength(0); i++) 
    { 
     for (int j = 0; j < road.GetLength(1); j++) 
     { 
      Console.Write(string.Format("{0} ", road[i, j])); 
     } 
     Console.Write(Environment.NewLine + Environment.NewLine); 
    } 
    Console.WriteLine("Paliwo: "+ (fuel=fuel-5) + "%"); 

    moveAndGenerate(); 
    replaceArrays();    

    Thread.Sleep(1000); 
    Console.Clear(); 
} 

erzeugt sie ein einfaches Spiel:

| :x| 
| : | 
|x: | 
| :↑| 

Innerhalb der Schleife, solange Kraftstoff ist. Ich möchte, dass sich der Pfeil nach rechts/links bewegt, ohne auf Console.ReadKey() zu warten. Ist es möglich?

+1

Mögliches Duplikat [Listen für Tastendruck in .NET Konsole app] (http://stackoverflow.com/questions/5891538/listen-for-key-press-in-net-console-app) –

+0

'if (Console.KeyAvailable) {Zeichenschlüssel = Console.ReadKey (false);}'? –

Antwort

0

Wie RB angegeben, Sie können Setup ein Zuhörer für den Tastendruck statt, und überprüfen dann, wenn, wenn, so dass Sie sie den Tastendruck auf Null zurückgesetzt und das Auto in dieser Richtung

Listen for key press in .NET console app

0

andere verschieben Eine mögliche Problemumgehung besteht darin, eine BackgroundWorker zu verwenden, um auf Eingaben zu warten. Auf diese Weise können Sie sowohl die Benutzereingabe als auch den Hauptcode zur gleichen Zeit bearbeiten. Es ist einem separaten Thread ähnlich.

Sie müssen using System.ComponentModel; zu Ihrem Programm hinzufügen.

static BackgroundWorker backgroundWorker1 = new BackgroundWorker(); // Create the background worker 
static string input = ""; // where the user command is stored 

public static void Main() 
{ 
    // All the code preceding the main while loop is here 
    // Variable declarations etc. 


    //Setup a background worker 
    backgroundWorker1.DoWork += BackgroundWorker1_DoWork; // This tells the worker what to do once it starts working 
    backgroundWorker1.RunWorkerCompleted += BackgroundWorker1_RunWorkerCompleted; // This tells the worker what to do once its task is completed 

    backgroundWorker1.RunWorkerAsync(); // This starts the background worker 

    // Your main loop 
    while (fuel>0) 
    { 
     moveAndGenerate(); 

     for (int i=0;i<road.GetLength(0); i++) 
     { 
      for (int j = 0; j < road.GetLength(1); j++) 
      { 
       Console.Write(string.Format("{0} ", road[i, j])); 
      } 
      Console.Write(Environment.NewLine + Environment.NewLine); 
     } 
     Console.WriteLine("Paliwo: "+ (fuel=fuel-5) + "%"); 

     moveAndGenerate(); 
     replaceArrays();    

     Thread.Sleep(1000); 
     Console.Clear(); 
    } 

    // This is what the background worker will do in the background 
    private static void BackgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
    { 
     if (Console.KeyAvailable == false) 
     { 
      System.Threading.Thread.Sleep(100); // prevent the thread from eating too much CPU time 
     } 
     else 
     { 
      input = Console.In.ReadLine(); 

      // Do stuff with input here or, since you can make it a static 
      // variable, do stuff with it in the while loop. 
     } 
    } 

    // This is what will happen when the worker completes reading 
    // a user input; since its task was completed, it will need to restart 
    private static void BackgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs 
    { 
     if(!backgroundWorker1.IsBusy) 
     { 
      backgroundWorker1.RunWorkerAsync(); // restart the worker 
     } 

    } 
}