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

    Roguelike Console App Control Problems

    I'm working on a Roguelike console app for C#. I have several key features working such as movement arithmetic but I am unable to coordinate things so my sprite moves around the screen properly.

    If you'll compile and run the code you'll notice you have to enter in a "w", "a", "s", or "d" key twice before you can start moving the character. After that, the character keeps moving in one direction whether you enter in a different key or not. Not only that, but the Console.ReadKey(false) method doesn't work and you have to enter another key and press enter in order to move the character again. Other problems persist as well, like the character disappearing from the screen.

    Instead of what I have, I want smooth movement. For example, if the user presses the "w" key, it should move the character "up" without the user having to hit enter. Likewise, "a" and "d" should move the character left and right to allow for movement as seen in the original Rogue.

    I've tried using Console.ReadKey(false) but it doesn't work to move the character without having to hit enter. The code seems to be a mess and I would like to know how I can eliminate those two Console.ReadLine methods so the user doesn't have to enter in two characters to start the game properly.

    Can anybody help me make sense of this?

    Code:
    using System;
    
    class Sample
    {
        protected static int origRow;
        protected static int origCol;
    
        protected static void WriteAt(string s, int x, int y)
        {
            try
            {
                Console.SetCursorPosition(origCol + x, origRow + y);
                Console.Write(s);
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.Clear();
                Console.WriteLine(e.Message);
            }
        }
    
        public static void Main()
        {
            Console.ReadKey(false);
    
            int sr = 10;
            int sc = 10;
    
            Console.ReadKey(false);
    
            ConsoleKeyInfo theKey = Console.ReadKey(false);
            string choice = Console.ReadLine();
    
            do
            {
                Console.Clear();
                origRow = Console.CursorTop;
                origCol = Console.CursorLeft;
    
                string input = Console.ReadLine();
    
                WriteAt("@", sr, sc);
    
                Console.ReadKey(false);
    
                switch (theKey.KeyChar)
                {
                    case 'W': case 'w' :
                        sc = sc - 1;
                        WriteAt("@", sr, sc);
                        break;
    
                    case 'S': case 's' :
                        sc = sc + 1;
                        WriteAt("@", sr, sc);
                        break;
    
                    case 'A': case 'a' :
                        sr = sr - 1;
                        WriteAt("@", sr, sc);
                        break;
    
                    case 'D': case 'd' :
                        sr = sr + 1;
                        WriteAt("@", sr, sc);
                        break;
                }
            } while (choice != "Q");
        }
    }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Roguelike Console App Control Problems

    A good habit to get into when first learning .Net is to check out other methods and properties in the class you are using (or planning to use).

    See Console Class in msdn.

    There are 4 methods related to reading, including the ReadLine method you are using.

    See if any of the other read related methods will do what you want (see the description on the right of each method).

    Hint: there are two of them that will do what you are looking for. One of them might work better than the other.

  3. #3
    Join Date
    Jun 2011
    Posts
    33

    Re: Roguelike Console App Control Problems

    Quote Originally Posted by Arjay View Post
    A good habit to get into when first learning .Net is to check out other methods and properties in the class you are using (or planning to use).

    See Console Class in msdn.

    There are 4 methods related to reading, including the ReadLine method you are using.

    See if any of the other read related methods will do what you want (see the description on the right of each method).

    Hint: there are two of them that will do what you are looking for. One of them might work better than the other.
    I see the 4 read methods you speak of. I also am looking at the write methods (not cursor methods), would these be able to help me?

  4. #4
    Join Date
    Jun 2011
    Posts
    33

    Re: Roguelike Console App Control Problems

    Bump.

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Roguelike Console App Control Problems

    Quote Originally Posted by Krunchyman View Post
    I see the 4 read methods you speak of. I also am looking at the write methods (not cursor methods), would these be able to help me?
    Why don't you try them and see? The best way to understand this stuff is by hands on experience. Type in the code and see what happens. Just remember to keep backups so you can revert back if you hose your code.
    Always use [code][/code] tags when posting code.

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