Skip to content

Instantly share code, notes, and snippets.

@squareiguana
Created February 27, 2014 17:22
Show Gist options
  • Save squareiguana/9254716 to your computer and use it in GitHub Desktop.
Save squareiguana/9254716 to your computer and use it in GitHub Desktop.
Example (Paint App) Using For Loops, If Statements and Functions - Using Coding 101 - Episode 04
using System;
namespace SquareIguana.Coding101.Example.E04
{
/* Example on what was learned on Coding 101 - Episode 04 - For Loops, If Statements and Functions
* I made the example trying not to use "advanced" concepts, and only what the episode was trying to teach.
*
* I originally was just going to move a character around, but creating the code let me to a bug in my code,
* that was interesting, the character lead a trail behind him, so I thought this is something that I can do
* something different, and that is how this simple Paint Application got started
*
* The most useful thing in the example, I think is the whole painting thing, and the menu to select different
* colors for the paint brush, and the "eraser", but I think the most interesting thing for me was that sometimes
* errors in your code can take you in a direction you never expected.
*
* This was built with the knowledge learned from Episode 01-04 and my last Example: https://gist.github.com/squareiguana/9254664
*
* 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 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 magenta = ConsoleColor.DarkMagenta;
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 ConsoleColor paintPoint = ConsoleColor.Yellow;
static ConsoleColor paintBakground = white;
static ConsoleColor paintColor = black;
static ConsoleColor tmpPaintColor = paintColor;
static int pointHorizontal = border;
static int pointVeritcal = border;
static int initialCursorSize = Console.CursorSize;
static bool isPainting = false;
static void Main(string[] args)
{
Init();
Paint();
EndScreen();
}
static void Init()
{
Console.Title = "Coding 101 - C# E04 - For Loops, If Statements and Functions";
Console.SetWindowSize(width, height);
Console.BufferWidth = width;
Console.BufferHeight = height;
Console.BackgroundColor = gray;
Console.ForegroundColor = black;
Console.Clear();
DrawInfoBox();
if (border != 0)
DrawBorder();
InfoBoxText();
}
#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 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 E04 "; // 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 Simple Paint Application, use");
Console.SetCursorPosition(padding + 4, border + 5);
Console.Write("the arrow keys on your keyboard to paint on the console.");
Console.SetCursorPosition(padding + 4, border + 7);
Console.Write("You can change the Color of the brush by pressing <C>");
Console.SetCursorPosition(padding + 4, border + 8);
Console.Write("and by pressing <Enter> Or by pressing <E> you can erase");
Console.SetCursorPosition(padding + 4, border + 10);
Console.Write("This is what I would call a POC (Proof of Concept),");
Console.SetCursorPosition(padding + 4, border + 11);
Console.Write("and makes me want to spend more time coding, to make");
Console.SetCursorPosition(padding + 4, border + 12);
Console.Write("a more complete Paint Application");
Console.SetCursorPosition(padding + 4, border + 14);
Console.Write("Is it Useful? Not really, but with a small modification");
Console.SetCursorPosition(padding + 4, border + 15);
Console.Write("on the code, it could be use as basis for the movement");
Console.SetCursorPosition(padding + 4, border + 16);
Console.Write("of a character in a game.");
Console.BackgroundColor = black;
Console.ForegroundColor = white;
Console.SetCursorPosition(headerWidth - 18, border + 16);
Console.Write("<Press ENTER to Continue>");
AnimateInfoBoxText(headerText, positionOfText);
}
static void AnimateInfoBoxText(string text, int position)
{
Console.BackgroundColor = cyan;
Console.ForegroundColor = red;
ConsoleKeyInfo cki = new ConsoleKeyInfo();
while(cki.Key != ConsoleKey.Enter)
{
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);
}
}
#endregion
#region Paint Application
/// <summary>
/// Main Paint Loop Logic
/// </summary>
static void Paint()
{
InitPaint();
ConsoleKeyInfo keyInfo;
while ((keyInfo = Console.ReadKey(true)).Key != ConsoleKey.Escape)
{
if (keyInfo.Key == ConsoleKey.UpArrow)
{
MovePencil(0, -1);
}
else if (keyInfo.Key == ConsoleKey.DownArrow)
{
MovePencil(0, 1);
}
else if (keyInfo.Key == ConsoleKey.LeftArrow)
{
MovePencil(-1, 0);
}
else if (keyInfo.Key == ConsoleKey.RightArrow)
{
MovePencil(1, 0);
}
else if (keyInfo.Key == ConsoleKey.C)
{
ChangePaintColor();
}
else if (keyInfo.Key == ConsoleKey.E)
{
if (isPainting)
StopPainting();
else
ContinuePainting();
}
}
}
static void InitPaint()
{
Console.Title = "Paint Application Screen";
Console.CursorVisible = false;
Console.BackgroundColor = paintBakground;
Console.Clear();
Console.BackgroundColor = paintPoint;
Console.SetCursorPosition(border, border);
Console.Write(" ");
isPainting = true;
DrawBorderOptions();
}
static void DrawBorderOptions()
{
if (border != 0)
DrawBorder();
Console.BackgroundColor = black;
Console.ForegroundColor = white;
Console.SetCursorPosition(border + 1, height - 1);
Console.Write("Press <C> to select a Color");
Console.SetCursorPosition(width - 33, height - 1);
Console.Write("Press <E> to select the Eraser");
}
static void MovePencil(int x, int y)
{
if (CanMove(pointHorizontal + x, pointVeritcal + y))
{
Console.MoveBufferArea(pointHorizontal, pointVeritcal, 1, 1, pointHorizontal + x, pointVeritcal + y, ' ', paintColor, paintColor);
pointHorizontal += x;
pointVeritcal += y;
}
}
/// <summary>
/// Make sure that the coordinates are not placed outside the console to avoid the Application to Crash.
/// </summary>
static bool CanMove(int x, int y)
{
if (x < border || x >= width - border)
return false;
if (y < border || y >= height - border)
return false;
return true;
}
static void ChangePaintColor()
{
int position = border + 4;
Console.CursorVisible = true;
Console.CursorSize = 100;
DrawColorSelection();
Console.SetCursorPosition(position, height - 2);
ConsoleKeyInfo keyInfo;
while ((keyInfo = Console.ReadKey(true)).Key != ConsoleKey.Enter)
{
if (keyInfo.Key == ConsoleKey.LeftArrow)
{
if (position > border + 4)
position -= 3;
}
else if (keyInfo.Key == ConsoleKey.RightArrow)
{
if (position < border + 24)
position += 3;
}
Console.SetCursorPosition(position, height - 2);
}
SelectColor((position - 3) / 3);
Console.CursorSize = initialCursorSize;
Console.CursorVisible = false;
DrawBorderOptions();
}
static void DrawColorSelection()
{
Console.BackgroundColor = black;
Console.SetCursorPosition(border + 3, height - 2);
Console.Write(" ");
Console.BackgroundColor = blue;
Console.SetCursorPosition(border + 6, height - 2);
Console.Write(" ");
Console.BackgroundColor = red;
Console.SetCursorPosition(border + 9, height - 2);
Console.Write(" ");
Console.BackgroundColor = magenta;
Console.SetCursorPosition(border + 12, height - 2);
Console.Write(" ");
Console.BackgroundColor = green;
Console.SetCursorPosition(border + 15, height - 2);
Console.Write(" ");
Console.BackgroundColor = yellow;
Console.SetCursorPosition(border + 18, height - 2);
Console.Write(" ");
Console.BackgroundColor = cyan;
Console.SetCursorPosition(border + 21, height - 2);
Console.Write(" ");
Console.BackgroundColor = gray;
Console.SetCursorPosition(border + 24, height - 2);
Console.Write(" ");
}
static void SelectColor(int colorOption)
{
if (colorOption == 1)
paintColor = black;
else if (colorOption == 2)
paintColor = blue;
else if (colorOption == 3)
paintColor = red;
else if (colorOption == 4)
paintColor = magenta;
else if (colorOption == 5)
paintColor = green;
else if (colorOption == 6)
paintColor = yellow;
else if (colorOption == 7)
paintColor = cyan;
else if (colorOption == 8)
paintColor = gray;
}
static void StopPainting()
{
tmpPaintColor = paintColor;
paintColor = paintBakground;
isPainting = false;
}
static void ContinuePainting()
{
paintColor = tmpPaintColor;
isPainting = true;
}
#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