Modifying console window properties can improve your application interface for players of your text-based game, or users of your application. The application's title bar and window size are a couple of the basic command line window properties to consider modifying. Code examples are included to help you test out these tips in your projects.
Using design elements in your console application can make a big difference for the player or user. Used strategically, they can make the interface more interesting and your application more immersive. The tips below about the Console Window can help you add design elements to your command line application, and the code examples can help you test out the ideas in your own projects.
Window Properties
Changing basic elements of your application's "canvas" are easy! Three aspects you might want to consider modifying are the title, height, and width properties.
Window Title
The console class has a property (Title) that can be used to write text to your application's title bar.
Title Changes
You can even change the window title after your application starts.
using System; namespace ConsoleApplicationDesignElements { class Program { static void Main() { Console.Title = "Welcome!"; Console.ReadKey(); //waits for key press before changing title Console.Title = "Design Elements for Console Applications"; Console.Read(); } } }
Changing the title can be helpful to orient the player if you have different modes in your application, or if you want to customize the title based on player action. For example, a madlib console application's title could change depending on words entered by the player.
The next code example mocks up a scenario in which the title is first "Welcome", then "Character Setup", and finally "Level One". For simplification, there is a key press between each of the title changes. When the title is "Character Setup" you can imagine the screen has changed to options for a player to customize their character, and when it is "Level One" the screen has changed again to show the beginning of a game.
using System; namespace ConsoleApplicationDesignElements { class Program { static void Main() { Console.Title = "Neato-Keen Game: Welcome"; Console.ReadKey(); Console.Title = "Neato-Keen Game: Character Setup"; Console.ReadKey(); Console.Title = "Neato-Keen Game: Level One"; Console.Read(); } } }
Title Animation
Using nested loops you can even animate your title. Movement in the title bar can pull the player's eyes away from your content, so using this technique would be rare. Click the toggle box below if you'd like to see a code example.
Show Code
In this example, change the value of LoadingText to be the full string you want to animate in the title bar. The variable TitleBarText starts as an empty string.
The for loop will cycle through each character within the LoadingText string, with each pass adding another character to the TitleBarText variable. The TitleBarText value is written in the title bar. As each character is added, the illusion of animation is created.
After reaching the end of the string (which we can easily find with the string property Length: LoadingText.Length), the TitleBarText is reset to an empty string. While the value of the variable Loading is set to true, the while loop will keep calling the for loop.
using System; using System.Threading; namespace ConsoleApplicationDesignElements { class Program { static void Main() { string LoadingText = "Loading..."; string TitleBarText = ""; bool Loading = true; Console.Title = TitleBarText; while (Loading) { for (int i = 0; i < LoadingText.Length; i++) { TitleBarText = TitleBarText + LoadingText[i]; Console.Title = TitleBarText; Thread.Sleep(240); } TitleBarText = ""; } Console.Read(); } } }
This example is a modification of Arunkumar Gudelli's code.
Window Height
Another property of the console class that you might want to modify is WindowHeight. This value isn't in pixels, though, it is in rows. Changing the WindowHeight property to a fixed number might work great on your setup, but could throw an error on a different system.
Console.WindowHeight = Console.LargestWindowHeight-20;
The LargestWindowHeight property can be used to find out the largest a window can be. Using this property allows the window to adjust for different resolutions. For example, subtracting 20 makes the height a little smaller than the full screen; for most resolutions this keeps the application window a comfortable size.
You can see the code and read more at: Repetitive Lyrics Using Arrays & Nested Loop.
If you'd like to change both the height and width of your application, you can use the console class method SetWindowSize Method.
Window Width
Similar to WindowHeight, WindowWidth is not in pixels. It is in columns. So to set the width, you might want to first find out what the largest width could be.
Console.WindowWidth = Console.LargestWindowWidth-20;
Changing the width of your window can be tricky if you have text for your player or user to read. There is a generally agreed upon optimal range of characters per line for reading (40-75 characters, or not more than 12 words per line) based on user experience research. If you are expecting someone to read a lot of text, it's helpful if they are comfortable in doing so.
"At normal reading distances the arc of the visual field covered by the macula is only a few inches wide—about the width of a well-designed column of text, or about twelve words per line. Research shows that reading slows as line lengths begin to exceed the ideal width, because the reader then needs to use the muscles of the eye or neck to track from the end of one line to the beginning of the next line." (Web Style Guide).
Wikipedia has a nice example graphic by Jrinkerdesign to illustrate the range:
Related Posts
- Console text and background color: Console Application Color: Text and Background.
- Console Application ASCII Design: Console Application ASCII Design.