Skip to content

Instantly share code, notes, and snippets.

@squareiguana
Created February 27, 2014 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save squareiguana/9254736 to your computer and use it in GitHub Desktop.
Save squareiguana/9254736 to your computer and use it in GitHub Desktop.
Example (Menu System) Using Functions - Using Coding 101 - Episode 05
using System;
namespace SquareIguana.Coding101.Example.E05
{
/* Example on what was learned on Coding 101 - Episode 05 - Functions
* I made the example trying not to use "advanced" concepts, and only what the episode was trying to teach.
*
* When I think of a Menu, I think of a GUI that you can select with your keyboard or mouse, not a series of
* commands, so I decided to make a Real Menu. I didn't have the time or energy to create something more complete,
* but I did create a page for each Menu Item, even if just a simple message.
*
* The most useful thing in the example, I think is the whole Menu system, even if it is a hackish way to do the
* whole selection background thing, at least it works, and that is the important thing, there is probably a more
* elegant solution to this particular problem, but I could think of something better.
*
* This was built with the knowledge learned from Episode 01-05 and my last Example: https://gist.github.com/squareiguana/9254716
*
* I'm going out of my way to use only the simple concepts, this definitely needs a refactor once they go into
* more "advanced" topics like classes, also the functions are to big, It is better to have a function that does only
* one thing. Maybe I'll do a refactor or several, after some of those topics or when I have some free time to show a
* more cleaner code.
*
* Definition of Refactor: To rewrite existing source code in order to improve its readability, reusability or
* structure without affecting its meaning or behaviour.
*/
class Program
{
static int width = 80;
static int height = 25;
static string BorderCharacter = "◄►";
static int border = BorderCharacter.Length;
static ConsoleColor white = ConsoleColor.White;
static ConsoleColor black = ConsoleColor.Black;
static ConsoleColor gray = ConsoleColor.Gray;
static ConsoleColor cyan = ConsoleColor.DarkCyan;
static ConsoleColor green = ConsoleColor.DarkGreen;
static ConsoleColor red = ConsoleColor.DarkRed;
static ConsoleColor blue = ConsoleColor.DarkBlue;
static ConsoleColor yellow = ConsoleColor.DarkYellow;
static int headerWidth = 0;
static int headerHeight = 0;
static int padding = 0;
static bool IsStartGameItemSelected = true;
static bool IsLoadGameItemSelected = false;
static bool IsSettingsItemSelected = false;
static bool IsAboutItemSelected = false;
static bool IsExitItemSelected = false;
static void Main(string[] args)
{
MainPage();
}
static void MainPage()
{
Console.Title = "Coding 101 - C# E05 - Functions and a Menu";
Console.CursorVisible = false;
Console.SetWindowSize(width, height);
Console.BufferWidth = width;
Console.BufferHeight = height;
Console.BackgroundColor = gray;
Console.ForegroundColor = black;
Console.Clear();
DrawMenu();
}
#region Pages
static void StartGamePage()
{
Console.BackgroundColor = gray;
Console.Clear();
if (border != 0)
DrawBorder();
DrawHeader();
SetHeaderText(" Start Game Page is Not Coded Yet ");
WaitUntilEscape();
}
static void LoadGamePage()
{
Console.BackgroundColor = gray;
Console.Clear();
if (border != 0)
DrawBorder();
DrawHeader();
SetHeaderText(" Load Game Page is Not Coded Yet ");
WaitUntilEscape();
}
static void SettingsPage()
{
Console.BackgroundColor = gray;
Console.Clear();
if (border != 0)
DrawBorder();
DrawHeader();
SetHeaderText(" Settings Page is Not Coded Yet ");
WaitUntilEscape();
}
static void AboutPage()
{
Console.BackgroundColor = gray;
Console.Clear();
DrawInfoBox();
if (border != 0)
DrawBorder();
InfoBoxText();
}
#endregion
#region Helpers
static void WaitUntilEscape()
{
Console.ReadKey();
MainPage();
}
static void SetHeaderText(string text)
{
Console.BackgroundColor = red;
Console.ForegroundColor = white;
Console.SetCursorPosition(padding + 2, border + 2);
Console.MoveBufferArea(padding + 3, border + 1, headerWidth - (3 * 2), 1, padding + 3, border + 2, '▒', white, black);
Console.SetCursorPosition((headerWidth / 2) - (text.Length / 2) + padding, border + 2);
Console.Write(text);
}
#endregion
#region Border
static void DrawBorder()
{
Console.BackgroundColor = gray;
Console.ForegroundColor = red;
int tmpCursor = 0;
for (int cursor = 0; cursor < width - border; cursor++)
{
if (cursor < height - 1)
{
Console.SetCursorPosition(0, cursor);
Console.Write(BorderCharacter);
Console.SetCursorPosition(width - border, cursor);
Console.Write(BorderCharacter);
}
if (tmpCursor >= width - border)
continue;
Console.SetCursorPosition(tmpCursor, 0);
Console.Write(BorderCharacter);
Console.SetCursorPosition(tmpCursor, height - border);
Console.Write(BorderCharacter);
if (border >= 2)
{
for (int borderLine = 1; borderLine < border; borderLine++)
{
Console.SetCursorPosition(tmpCursor, borderLine);
Console.Write(BorderCharacter);
Console.SetCursorPosition(tmpCursor, height - borderLine);
Console.Write(BorderCharacter);
}
}
tmpCursor += border;
}
// This is to write the right lower corner without going to the next line and loosing the first line
Console.BufferWidth = Console.BufferWidth + 1; // Enlarge buffer width in order to prevent it from going to the next line
Console.SetCursorPosition(width - border, height - 1);
Console.Write(BorderCharacter);
Console.BufferWidth = Console.BufferWidth - 1; // Restore buffer to the original size
}
#endregion
#region Menu
static void DrawMenu()
{
string menuTop = " ╔════════════════════════════╗ ";
string menuFill = " ║ ║ ";
string menuBottom = " ╚════════════════════════════╝ ";
string line = " ╟────────────────────────────╢ ";
int menuWidth = menuTop.Length;
padding = ((width - (menuWidth + (border * 2))) / 2) + border;
// Add Drop Shadow
Console.MoveBufferArea(padding + 1, border + 8, menuWidth, height - border - 8, padding + 1, height - border - 2, '░', black, gray);
if (border != 0)
DrawBorder();
DrawMenuAscii();
Console.BackgroundColor = white;
Console.ForegroundColor = black;
Console.SetCursorPosition(padding, height - border - 14);
Console.Write(menuTop);
for (int i = 13; i >= 5; i--)
{
Console.SetCursorPosition(padding, height - border - i);
Console.Write(menuFill);
i--;
Console.SetCursorPosition(padding, height - border - i);
Console.Write(line);
}
MenuItems();
Console.SetCursorPosition(padding, height - border - 4);
Console.Write(menuBottom);
MenuLogic();
}
static void MenuLogic()
{
int position = border + 8;
Console.SetCursorPosition(padding + 2, position);
ConsoleKeyInfo keyInfo;
while ((keyInfo = Console.ReadKey(true)).Key != ConsoleKey.Enter)
{
if (keyInfo.Key == ConsoleKey.UpArrow)
{
if (position > border + 8)
position -= 2;
}
else if (keyInfo.Key == ConsoleKey.DownArrow)
{
if (position < border + 16)
position += 2;
}
SelectedMenuItem((position - 8) / 2);
}
ActivateMenuItem((position - 8) / 2);
}
static void SelectedMenuItemColor()
{
Console.BackgroundColor = blue;
Console.ForegroundColor = white;
}
static void UnselectedMenuItemColor()
{
Console.BackgroundColor = white;
Console.ForegroundColor = black;
}
static void MenuItems()
{
if (IsStartGameItemSelected)
SelectedMenuItemColor();
else
UnselectedMenuItemColor();
Console.SetCursorPosition(padding + 2, height - border - 13);
Console.Write(" [Start Game]".PadRight(28));
if (IsLoadGameItemSelected)
SelectedMenuItemColor();
else
UnselectedMenuItemColor();
Console.SetCursorPosition(padding + 2, height - border - 11);
Console.Write(" [Load Game]".PadRight(28));
if (IsSettingsItemSelected)
SelectedMenuItemColor();
else
UnselectedMenuItemColor();
Console.SetCursorPosition(padding + 2, height - border - 9);
Console.Write(" [Settings]".PadRight(28));
if (IsAboutItemSelected)
SelectedMenuItemColor();
else
UnselectedMenuItemColor();
Console.SetCursorPosition(padding + 2, height - border - 7);
Console.Write(" [About]".PadRight(28));
if (IsExitItemSelected)
SelectedMenuItemColor();
else
UnselectedMenuItemColor();
Console.SetCursorPosition(padding + 2, height - border - 5);
Console.Write(" [Exit]".PadRight(28));
}
static void SelectedMenuItem(int menuOption)
{
if (menuOption == 1)
{
IsStartGameItemSelected = true;
IsLoadGameItemSelected = false;
IsSettingsItemSelected = false;
IsAboutItemSelected = false;
IsExitItemSelected = false;
}
else if (menuOption == 2)
{
IsStartGameItemSelected = false;
IsLoadGameItemSelected = true;
IsSettingsItemSelected = false;
IsAboutItemSelected = false;
IsExitItemSelected = false;
}
else if (menuOption == 3)
{
IsStartGameItemSelected = false;
IsLoadGameItemSelected = false;
IsSettingsItemSelected = true;
IsAboutItemSelected = false;
IsExitItemSelected = false;
}
else if (menuOption == 4)
{
IsStartGameItemSelected = false;
IsLoadGameItemSelected = false;
IsSettingsItemSelected = false;
IsAboutItemSelected = true;
IsExitItemSelected = false;
}
else if (menuOption == 5)
{
IsStartGameItemSelected = false;
IsLoadGameItemSelected = false;
IsSettingsItemSelected = false;
IsAboutItemSelected = false;
IsExitItemSelected = true;
}
MenuItems();
}
static void ClearSelectedItem()
{
IsStartGameItemSelected = true;
IsLoadGameItemSelected = false;
IsSettingsItemSelected = false;
IsAboutItemSelected = false;
IsExitItemSelected = false;
}
static void ActivateMenuItem(int menuOption)
{
if (menuOption == 1)
{
ClearSelectedItem();
StartGamePage();
}
else if (menuOption == 2)
{
ClearSelectedItem();
LoadGamePage();
}
else if (menuOption == 3)
{
ClearSelectedItem();
SettingsPage();
}
else if (menuOption == 4)
{
ClearSelectedItem();
AboutPage();
}
else if (menuOption == 5)
{
ClearSelectedItem();
EndScreen();
}
}
static void DrawMenuAscii()
{
Console.BackgroundColor = gray;
Console.ForegroundColor = red;
Console.SetCursorPosition(padding - 3, border + 1);
Console.Write("███╗ ███╗███████╗███╗ ██╗██╗ ██╗");
Console.SetCursorPosition(padding - 3, border + 2);
Console.Write("████╗ ████║██╔════╝████╗ ██║██║ ██║");
Console.SetCursorPosition(padding - 3, border + 3);
Console.Write("██╔████╔██║█████╗ ██╔██╗ ██║██║ ██║");
Console.SetCursorPosition(padding - 3, border + 4);
Console.Write("██║╚██╔╝██║██╔══╝ ██║╚██╗██║██║ ██║");
Console.SetCursorPosition(padding - 3, border + 5);
Console.Write("██║ ╚═╝ ██║███████╗██║ ╚████║╚██████╔╝");
Console.SetCursorPosition(padding - 3, border + 6);
Console.Write("╚═╝ ╚═╝╚══════╝╚═╝ ╚═══╝ ╚═════╝ ");
}
#endregion
#region InfoBox
static void DrawInfoBox()
{
string headerTop = " ╔══════════════════════════════════════════════════════════╗ ";
string headerFill = " ║ ║ ";
string headerBottom = " ╚══════════════════════════════════════════════════════════╝ ";
headerWidth = headerTop.Length;
padding = ((width - (headerWidth + (border * 2))) / 2) + border;
// Add Drop Shadow
Console.MoveBufferArea(padding + 1, border + 3, headerWidth, height - border - 3, padding + 1, height - border - 2, '▒', black, white);
Console.BackgroundColor = cyan;
Console.ForegroundColor = white;
Console.SetCursorPosition(padding, border + 2);
Console.Write(headerTop);
for (int i = 3; i < height - border - 6; i++)
{
Console.SetCursorPosition(padding, border + i);
Console.Write(headerFill);
}
Console.SetCursorPosition(padding, height - border - 4);
Console.Write(headerBottom);
}
static void InfoBoxText()
{
Console.BackgroundColor = cyan;
Console.ForegroundColor = white;
string headerText = " CODING 101 - Example Using E05 "; // Empty spaces before and after text to make it look better
int positionOfText = padding + (headerWidth / 2) - (headerText.Length / 2);
Console.SetCursorPosition(positionOfText, border + 2);
Console.Write(headerText);
Console.SetCursorPosition(padding + 4, border + 4);
Console.Write("This is an Example of a Menu System, you can use the");
Console.SetCursorPosition(padding + 4, border + 5);
Console.Write("arrow keys on your keyboard to select an item on the");
Console.SetCursorPosition(padding + 4, border + 6);
Console.Write("menu by pressing <ENTER>");
Console.SetCursorPosition(padding + 4, border + 8);
Console.Write("In this stage of the Application, the Menu Options for");
Console.SetCursorPosition(padding + 4, border + 9);
Console.Write("[Start Game], [Load Game], [Settings] are not available");
Console.SetCursorPosition(padding + 4, border + 10);
Console.Write("and are here only to make the menu look more complete.");
Console.SetCursorPosition(padding + 4, border + 12);
Console.Write("In the future this code could be use as a basis for the");
Console.SetCursorPosition(padding + 4, border + 13);
Console.Write("Menu of a more complete Application or a Game.");
Console.BackgroundColor = black;
Console.ForegroundColor = white;
Console.SetCursorPosition(headerWidth - 27, border + 16);
Console.Write("<Press ESC to Return to the Menu>");
AnimateInfoBoxText(headerText, positionOfText);
}
static void AnimateInfoBoxText(string text, int position)
{
Console.BackgroundColor = cyan;
Console.ForegroundColor = red;
ConsoleKeyInfo cki = new ConsoleKeyInfo();
while (cki.Key != ConsoleKey.Escape)
{
for (int i = 0; Console.KeyAvailable == false; i++)
{
if (i >= text.Length)
i = 0;
Console.ForegroundColor = red;
Console.SetCursorPosition(position + i, border + 2);
Console.Write(text[i]);
System.Threading.Thread.Sleep(200);
Console.ForegroundColor = white;
Console.SetCursorPosition(position + i, border + 2);
Console.Write(text[i]);
}
cki = Console.ReadKey(true);
}
MainPage();
}
#endregion
#region End Screen
static void EndScreen()
{
Console.Title = "End of Example";
Console.BackgroundColor = white;
Console.ForegroundColor = black;
Console.Clear();
DrawBorder();
DrawHeader();
Console.BackgroundColor = red;
Console.ForegroundColor = white;
Console.SetCursorPosition(padding + 2, border + 2);
string over = " EXAMPLE IS NOW OVER ";
Console.MoveBufferArea(padding + 3, border + 1, headerWidth - (3 * 2), 1, padding + 3, border + 2, '▒', white, black);
Console.SetCursorPosition((headerWidth / 2) - (over.Length / 2) + padding, border + 2);
Console.Write(over);
AsciiArt();
Console.ReadKey(); // Pause until a key is entered
}
static void DrawHeader()
{
string headerTop = " ╔══════════════════════════════════════════════════════════╗ ";
string headerFill = " ║ ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ ║ ";
string headerBottom = " ╚══════════════════════════════════════════════════════════╝ ";
headerWidth = headerTop.Length;
headerHeight = 5;
padding = ((width - (headerWidth + (border * 2))) / 2) + border;
Console.BackgroundColor = black;
Console.ForegroundColor = white;
Console.SetCursorPosition(padding, border);
Console.Write(headerTop);
Console.SetCursorPosition(padding, border + 1);
Console.Write(headerFill);
Console.SetCursorPosition(padding, border + 2);
Console.Write(headerFill);
Console.SetCursorPosition(padding, border + 3);
Console.Write(headerFill);
Console.SetCursorPosition(padding, border + 4);
Console.Write(headerBottom);
}
static void AsciiArt()
{
Console.BackgroundColor = white;
Console.ForegroundColor = green;
padding = ((width - (68 + (border * 2))) / 2) + border;
Console.SetCursorPosition(padding, border + headerHeight + 1);
Console.Write(" ██████╗ ██████╗ ██████╗ ██╗███╗ ██╗ ██████╗ ██╗ ██████╗ ██╗");
Console.SetCursorPosition(padding, border + headerHeight + 2);
Console.Write("██╔════╝██╔═══██╗██╔══██╗██║████╗ ██║██╔════╝ ███║██╔═████╗███║");
Console.SetCursorPosition(padding, border + headerHeight + 3);
Console.Write("██║ ██║ ██║██║ ██║██║██╔██╗ ██║██║ ███╗ ╚██║██║██╔██║╚██║");
Console.SetCursorPosition(padding, border + headerHeight + 4);
Console.Write("██║ ██║ ██║██║ ██║██║██║╚██╗██║██║ ██║ ██║████╔╝██║ ██║");
Console.SetCursorPosition(padding, border + headerHeight + 5);
Console.Write("╚██████╗╚██████╔╝██████╔╝██║██║ ╚████║╚██████╔╝ ██║╚██████╔╝ ██║");
Console.SetCursorPosition(padding, border + headerHeight + 6);
Console.Write(" ╚═════╝ ╚═════╝ ╚═════╝ ╚═╝╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═════╝ ╚═╝");
Console.ForegroundColor = black;
padding = ((width - (65 + (border * 2))) / 2) + border;
Console.SetCursorPosition(padding, border + headerHeight + 8);
Console.Write("╦ ╦┬┌┬┐┬ ┬ ╔═╗┬─┐ ╦═╗┌─┐┌┐ ┌─┐┬─┐┌┬┐ ╔╗ ┌─┐┬ ┬ ┌─┐┌─┐┌─┐┬─┐");
Console.SetCursorPosition(padding, border + headerHeight + 9);
Console.Write("║║║│ │ ├─┤ ╠╣ ├┬┘ ╠╦╝│ │├┴┐├┤ ├┬┘ │ ╠╩╗├─┤│ │ ├┤ │ ├┤ ├┬┘");
Console.SetCursorPosition(padding, border + headerHeight + 10);
Console.Write("╚╩╝┴ ┴ ┴ ┴ ╚ ┴└─o ╩╚═└─┘└─┘└─┘┴└─ ┴ ╚═╝┴ ┴┴─┘┴─┘└─┘└─┘└─┘┴└─");
padding = ((width - (49 + (border * 2))) / 2) + border;
Console.SetCursorPosition(padding, border + headerHeight + 11);
Console.Write("┌─┐┌┐┌┌┬┐ ╔═╗┬ ┬┌─┐┌┐┌┌┐┌┌─┐┌┐┌ ╔╦╗┌─┐┬─┐┌─┐┌─┐");
Console.SetCursorPosition(padding, border + headerHeight + 12);
Console.Write("├─┤│││ ││ ╚═╗├─┤├─┤│││││││ ││││ ║║║│ │├┬┘└─┐├┤ ");
Console.SetCursorPosition(padding, border + headerHeight + 13);
Console.Write("┴ ┴┘└┘─┴┘ ╚═╝┴ ┴┴ ┴┘└┘┘└┘└─┘┘└┘ ╩ ╩└─┘┴└─└─┘└─┘");
Console.BackgroundColor = black;
Console.ForegroundColor = green;
padding = ((width - (61 + (border * 2))) / 2) + border;
Console.SetCursorPosition(padding, border + headerHeight + 15);
Console.Write(" /') _/_ ( /_ _ _ ' _ -- / /|/| _ _ _ _ _ _ ");
Console.SetCursorPosition(padding, border + headerHeight + 16);
Console.Write(" (__()(/(- |/|/(// / /()/ -- (__()(/ / |(// (-_) ( (/ ");
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment