Advertisement
kisame1313

Untitled

Jul 17th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.24 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace sorting
  5. {
  6.     class MainClass
  7.     {
  8.         static int[,] inventory = new int[3, 3];
  9.         static int cursor_x = 0, cursor_y = 0;
  10.  
  11.         public static void Main ( string [] args )
  12.         {
  13.             Console.CursorVisible = false;
  14.             while ( true )
  15.             {
  16.                 DrawInventory ();
  17.                 Input ();
  18.             }
  19.         }
  20.  
  21.         public static void Input ()
  22.         {
  23.             ConsoleKeyInfo keyInfo = Console.ReadKey ( true );
  24.  
  25.             if ( keyInfo.Key == ConsoleKey.UpArrow )
  26.             {
  27.                 cursor_y = Clamp ( cursor_y - 1, 0, inventory.GetLength ( 1 ) - 1 );
  28.             }
  29.             else if ( keyInfo.Key == ConsoleKey.DownArrow )
  30.             {
  31.                 cursor_y = Clamp ( cursor_y + 1, 0, inventory.GetLength ( 1 ) - 1 );
  32.             }
  33.             else if ( keyInfo.Key == ConsoleKey.LeftArrow )
  34.             {
  35.                 cursor_x = Clamp ( cursor_x - 1, 0, inventory.GetLength ( 0 ) - 1 );
  36.             }
  37.             else if ( keyInfo.Key == ConsoleKey.RightArrow )
  38.             {
  39.                 cursor_x = Clamp ( cursor_x + 1, 0, inventory.GetLength ( 0 ) - 1 );
  40.             }
  41.  
  42.         }
  43.  
  44.         public static void DrawInventory ( int padding = 2 )
  45.         {
  46.             Console.BackgroundColor = ConsoleColor.Black;
  47.             int lastSize = CanculateSize ( padding-1 );
  48.  
  49.             for ( int x = 0 ; x < inventory.GetLength ( 0 ) ; x++ )
  50.             {
  51.                 for ( int y = 0 ; y < inventory.GetLength ( 1 ) ; y++ )
  52.                 {
  53.                     if ( x == cursor_x && y == cursor_y )
  54.                     {
  55.                         Console.BackgroundColor = ConsoleColor.Red;
  56.                     }
  57.                     DrawBox ( x * lastSize, y * lastSize, padding, inventory [x, y].ToString () );
  58.                     Console.BackgroundColor = ConsoleColor.Black;
  59.                 }
  60.             }
  61.         }
  62.  
  63.         public static int Clamp ( int value, int min, int max )
  64.         {
  65.             return value < min ? min : ( value > max ) ? max : value;
  66.         }
  67.  
  68.  
  69.         public static int CanculateSize ( int padding )
  70.         {
  71.             return 3 + padding + 2;
  72.         }
  73.  
  74.         public static void DrawBox ( int _x, int _y, int padding, string number )
  75.         {
  76.             int size = CanculateSize ( padding );
  77.             for ( int x = 0 ; x < size ; x++ )
  78.             {
  79.                 for ( int y = 0 ; y < size ; y++ )
  80.                 {
  81.                     if ( x == 0 || y == 0 || x == size - 1 || y == size - 1 )
  82.                     {
  83.                         Console.SetCursorPosition ( x + _x, y + _y );
  84.                         Console.Write ( '#' );
  85.                     }
  86.                 }
  87.             }
  88.  
  89.             Console.SetCursorPosition ( ( _x + size / 2 ) - number.Length / 2, _y + size / 2 );
  90.             Console.Write ( number );
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement