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!