CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2011
    Posts
    33

    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.

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    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);
                
            }
        }
    }
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Jun 2011
    Posts
    33

    Re: How to make Console Applications Respond Without Prompt

    I added that to my code and it is working properly, thanks for your advice.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured