简体   繁体   中英

How do i get a character in a given coordinate in the C# console?

I'm trying to make a roguelike in C#, and i got to make a little map, and a loop that checks if w/s/a/d is pressed to move the player; but i have a problem detecting walls ; let's say i want to know if there is a wall to the right before moving there, i get the player position, i check what is on the right of that position, and if it's a '#', i don't let the player move because it's supposed to be a wall.

But here's the problem, in the console, how do i check if the coordinate, for instance "(15,2)" contains a '#' char?

Is there an easier way to check if a given coordinate contains that character?

And how? Because i tried, but i can't GET the char in a certain coord of the console.

Something like:

static bool wallcheck_x(int xpos) {
        xpos++;
        //Now, it should GET the char allocated in xpos++;
        if (/*char that's in xpos++ */ == '#')
        {
            return true; //it's a wall
        }
        return false; //it's not a wall
    }

You shouldn't be doing it that way. You should have something that maps to what is displayed in the console, I assume that's how you even see the map to begin with. When a player moves, you should check it based on your map, not what is displayed in the console.

So for example...

how do i check if the coordinate, for instance "(15,2)" contains a '#' char?

Don't check what's in the console at (15,2), but check your map that displays what's in the console for a # instead. It's much easier that way.

But, let's say i did the map in a multidimensional array... then how do i print the array in the console?

Use Google, man. Here's what I pulled from this question :

int rowLength = arr.GetLength(0);
int colLength = arr.GetLength(1);

for (int i = 0; i < rowLength; i++)
{
    for (int j = 0; j < colLength; j++)
    {
        Console.Write(string.Format("{0} ", arr[i, j]));
    }
    Console.Write(Environment.NewLine + Environment.NewLine);
}

What this does is traverses the array by row, printing each character in the row. When it reaches the end of a row, it prints a new line and begins printing the new row. Your array would probably be a 2-dimensional character array which represents the map. Likewise, you can keep track of a player's position with something like:

int playerXPos;
int playerYPos;

You would need to update those as your character moves. So, credit to gleng for this snippet, you can check if the player has hit a wall:

if (arr[playerXPos, playerYPos] == '#')
{
     // player has collided with a wall
}
else
{
     // player has NOT collided with a wall
}

Though it would be more scalable to have an actual Player data structure, this seems like it would suit you just fine.

Without having access to any of your code (you should really post it for help like this), i'm assuming you would do something like:

if (map[player.X, player.Y] == '#')
{
     // player has collided with a wall
}
else
{
     // player has NOT collided with a wall
}

This code assumes that map is a multidimensional array that contains your map.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM