2

First off, thank you in advance for your help. I'm a student and i'm just getting started with programming in C#, so please excuse me for making my code so incredibly messy.

With the intended purpose of both practicing and having a bit of fun, i'm trying to make an ascii game (top down, like old zelda games, but incredibly simple). I've managed to make a movement system of sorts ('w' moves the cursor up in the console, 'a' moves it left, etc.) The map will be completely drawn in the console as well.

Now to the problem i encountered and the question that follows: Whenever i move my cursor, it actually places the character that corresponds to the button i pressed where a tile of the map should be (example: i press 'w'. The cursor first places a 'w', which replaces the map tile and then it moves up). My solution would be to first copy the character, which is originally in the tile. After that, i move the cursor. finally, i place the copied character back to where it belongs, instead of the 'w', 'a', 's' or 'd' that the program put there earlier). Another solution would be to make sure that the cursor never replaces the ascii character of the map in the first place. The question is: How would i implement either one of these solutions?

The code i've included shows my map building routine (which is currently filled with only '█') and the movement system.

class Program
{   
    static void Main(string[] args)
    {
        Console.WindowWidth = 128; //The map will be 128x32
        Console.WindowHeight = 32;
        LoadMap();
        Console.SetCursorPosition(10, 10); //the cursor will be set at x = 10 and y = 10

        while (true) //a simple loop to check for user input
        {
            ConsoleKeyInfo input = Console.ReadKey();
            Console.Write("\b");
            PosX = Console.CursorLeft;
            PosY = Console.CursorTop;
            switch (input.KeyChar)
            {
                case 'w':
                Console.SetCursorPosition(PosX + 0, PosY - 1);
                    break;
                case 'a':
                Console.SetCursorPosition(PosX - 1, PosY + 0);
                    break;
                case 's':
                Console.SetCursorPosition(PosX + 0, PosY + 1);
                    break;
                case 'd':
                Console.SetCursorPosition(PosX + 1, PosY + 0);
                    break;
            }
        }
    }

    public static void PathWay(int PathSize) //pathsize = amount of █ placed in a row.
    {
        int n = 0;
        Console.ForegroundColor = ConsoleColor.Gray;
        while (n < PathSize)
        {
            n = n + 1;
            Console.Write("█");
        }
    }

    public static void LoadMap() //This will eventually call to many subroutines to create a map (a subroutine for creating a tree for example)
    {
        PathSize = 128;
        int n;
        n = 0;
        while(n < 32)
        {
            n = n + 1;
            PathWay(PathSize);
        }
    }

    public static int PathSize;
    public static int PosX;
    public static int PosY;
    public static string test;
}   

of course i could make it so that you can only walk on one type of character, but the game would be more fun if you could walk on more than one type of character.

Once again, Thank you in advance!

maccettura
  • 10,514
  • 3
  • 28
  • 35
TheBattleCat
  • 121
  • 1
  • 2
  • 13
  • use `ConsoleKeyInfo input = Console.ReadKey(true);` to prevent the character being input from displaying in the console window. – absoluteAquarian Oct 10 '18 at 14:39
  • normally in these senarios you have an array of characters constituting the map, and your character (usually an @) which you draw in place of one of the map characters.. – BugFinder Oct 10 '18 at 14:39
  • @Tau This instantly fixed the problem, thank you very much! – TheBattleCat Oct 10 '18 at 14:41
  • @BugFinder It would be amazing to set the cursor as an "@". how would i go about doing that? – TheBattleCat Oct 10 '18 at 14:48
  • You dont, you just draw the @ as a character – BugFinder Oct 10 '18 at 14:52
  • @BugFinder ah that makes sense. I hate to be annoying, but i would like to ask: how would i draw that @ as a character, without destroying the map behind the character? (as far as i know, the @ will replace the map tile and once it's gone, the map tile it was on will be gone as well) – TheBattleCat Oct 10 '18 at 16:00

2 Answers2

0

Use ReadKey(true) instead of ReadKey()

ConsoleKeyInfo input = Console.ReadKey(true);
//Console.Write ("\b");
apocalypse
  • 5,764
  • 9
  • 47
  • 95
0

Yo shoul only use this sentence for reading the console input key with a true parameter on it

// Start a console read operation. Do not display the input.
ConsoleKeyInfo input = Console.ReadKey(true);