How to make Console Applications Respond Without Prompt
So, I want to create a roguelike video game in C# for a console application. The game will use procedurally generated environments that features ASCII characters and weapons created with WriteAt() methods. My main concern is this: How can I make it so that the interface will respond without the user having to press enter? In most console application programs, the interface will only respond when the user presses enter. However, I downloaded an executable version of Rogue for Windows and the directional controls allowed the user to move instantaneously without using the enter key. How can I make my program do this? I'm using Visual C# 2010 Express.
Re: How to make Console Applications Respond Without Prompt
you could use Console.ReadKey(false) to read the single key pressed.
If key is not one of the arrows keys, you should loop and read again - or ask if user wants to exit. If the key is an arrow,move.
Console.Readkey does not need the enter key to let you step on.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ReadLIneReadKey
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press H for Humans, D for Dragons, K for Keys - Any other key to step on - try with ann arrow key (to see how you could read it)");
ConsoleKeyInfo theKey= Console.ReadKey(false);
switch (theKey.KeyChar )
{
case 'H': case 'h' :
Console.WriteLine("Humans are around");
break;
case 'D': case 'd':
Console.WriteLine("Dragons are coming");
break;
case 'K': case 'k':
Console.WriteLine("You do not find the key");
break;
default :
Console.WriteLine("Stepping on");
Console.WriteLine("key was " + theKey.Key.ToString());
break;
}
Console.WriteLine("Any key to Exit");
theKey = Console.ReadKey(false);
}
}
}
Re: How to make Console Applications Respond Without Prompt
I added that to my code and it is working properly, thanks for your advice.