Want more features on Pastebin? Sign Up, it's FREE!
Guest

The Main Method

By: a guest on Jul 28th, 2010  |  syntax: C#  |  size: 2.98 KB  |  views: 80  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace ConsoleMenu
  5. {
  6.     enum MenuOptions { Play = 0, Credits, Exit }
  7.  
  8.     class Menu
  9.     {
  10.         public MenuOptions SelectedItem { get; set; }
  11.         public const string menuText = "{0,-3}Play\n{1,-3}Credits\n{2,-3}Exit";
  12.  
  13.         public Menu()
  14.         {
  15.             SelectedItem = MenuOptions.Play;
  16.         }
  17.  
  18.         public void ShowCredits()
  19.         {
  20.             Console.Clear();
  21.             Console.WriteLine("Written By: insertAlias");
  22.             Thread.Sleep(500);
  23.             Console.WriteLine("Directed By: insertAlias");
  24.             Thread.Sleep(500);
  25.             Console.WriteLine("Produced By: insertAlias");
  26.             Thread.Sleep(500);
  27.             Console.WriteLine("Played By: You!");
  28.             Thread.Sleep(500);
  29.             Console.WriteLine("Press any key to return to the menu.");
  30.             Console.ReadKey();
  31.         }
  32.  
  33.         public void Play()
  34.         {
  35.             Console.Clear();
  36.             Console.WriteLine("Beginning Game...");
  37.             Thread.Sleep(500);
  38.             Console.WriteLine("Playing Game...");
  39.             Thread.Sleep(500);
  40.             Console.WriteLine("Exiting Game...");
  41.         }
  42.  
  43.         public void Exit()
  44.         {
  45.             Console.Clear();
  46.             Console.WriteLine("Exiting Game...");
  47.             Thread.Sleep(500);
  48.         }
  49.  
  50.         public void ShowMenu()
  51.         {
  52.             Console.CursorVisible = false;
  53.             ConsoleKeyInfo key;
  54.             SelectedItem = MenuOptions.Play;
  55.             do
  56.             {
  57.                 Console.Clear();
  58.                 Console.WriteLine(menuText,
  59.                     GetPrefix(SelectedItem == MenuOptions.Play),
  60.                     GetPrefix(SelectedItem == MenuOptions.Credits),
  61.                     GetPrefix(SelectedItem == MenuOptions.Exit));
  62.                 key = Console.ReadKey();
  63.                 if (key.Key == ConsoleKey.UpArrow)
  64.                     SelectedItem = MenuUp(SelectedItem);
  65.                 else if (key.Key == ConsoleKey.DownArrow)
  66.                     SelectedItem = MenuDown(SelectedItem);
  67.             } while (key.Key != ConsoleKey.Enter);
  68.             Console.CursorVisible = true;
  69.         }
  70.  
  71.         public void DoSelectedAction()
  72.         {
  73.             if (SelectedItem == MenuOptions.Play)
  74.                 Play();
  75.             else if (SelectedItem == MenuOptions.Exit)
  76.                 Exit();
  77.             else
  78.                 ShowCredits();
  79.         }
  80.  
  81.         public string GetPrefix(bool selected)
  82.         {
  83.             return selected ? ">" : "";
  84.         }
  85.  
  86.         public MenuOptions MenuUp(MenuOptions current)
  87.         {
  88.             if (current == MenuOptions.Play)
  89.                 return MenuOptions.Exit;
  90.             else return current - 1;
  91.         }
  92.  
  93.         public MenuOptions MenuDown(MenuOptions current)
  94.         {
  95.             if (current == MenuOptions.Exit)
  96.                 return MenuOptions.Play;
  97.             else return current + 1;
  98.         }
  99.     }
  100. }
clone this paste RAW Paste Data