I try to build a game and I always clear the screen but that makes it lag. So, to make it not lag, can I clear specific text?
Asked
Active
Viewed 1,599 times
-2
-
1possible duplicate of [Rewriting characters in command window](http://stackoverflow.com/questions/2370695/rewriting-characters-in-command-window) – Chris Dec 31 '14 at 16:04
-
1I think the odds of getting a clean refresh in a command line app, even with replacing text, will be very slim. Have you pondered a WPF RichTextBox approach? It's not super performent either, but it's easy and won't flicker. – Brannon Dec 31 '14 at 16:09
-
i make snake so the location of the text always change – gil Dec 31 '14 at 16:12
1 Answers
2
If you want to remove only one last char then simply use console backspace
\b char
Console.Write("\b");
If you want to clear only one char,You can use Console.SetCursorPosition(int, int)
to reach that point then the write empty
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
Console.WriteLine(" ");
To remove more than one char, you can store the current Console.CursorLeft
in a variable and use that value in Console.SetCursorPosition(--variablename, Console.CursorTop)
in a loop to delete many chars you want!
For Example:- if You want to delete Current Console Line Then
public static void RemoveCurrentConsoleLine()
{
int currentCursorLine = Console.CursorTop;
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(new string(' ', Console.WindowWidth));
Console.SetCursorPosition(0, currentCursorLine);
}
Console.WriteLine("Top 1 Line");
Console.WriteLine("Top 2 Line");
Console.WriteLine("Top 3 Line");
Console.SetCursorPosition(0, Console.CursorTop - 1);
ClearCurrentConsoleLine();
Then Output Will be only top 2 lines. third line will be deleted:-
Top 1 Line
Top 2 Line

HaveNoDisplayName
- 8,291
- 106
- 37
- 47