kisame1313

1.3.9.3

Jul 18th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.45 KB | None | 0 0
  1. using System;
  2. /*
  3.  * Сделать игровую карту с помощью двухмерного массива.
  4.  * Сделать метод рисования карты.
  5.  * Помимо этого, дать пользователю возможность перемещаться по карте и взаимодействовать с элементами
  6.  * (например пользователь не может пройти сквозь стену)
  7.  * Все элементы являются обычными символами
  8.  */
  9.  
  10. namespace ConsoleApplication
  11. {
  12.     class Program
  13.     {
  14.         static int playerPosX;
  15.         static int playerPosY;
  16.         static int teleportPosX;
  17.         static int teleportPosY;
  18.  
  19.         static char player = '!';
  20.         static char teleport = 'O';
  21.  
  22.         static int [,] mapSize = new int [20,10];
  23.  
  24.         static Random spawnPosition = new Random ();
  25.  
  26.         static bool start = false;
  27.         static bool win = false;
  28.  
  29.         static void Main ( string [] args )
  30.         {
  31.             Console.CursorVisible = false;
  32.  
  33.             while ( !start && !win )
  34.             {
  35.                 Menu1 ();
  36.                 DrawMap ();
  37.                 PlayerMovement ();
  38.                 TeleportSpawn ();
  39.                 Win ();
  40.             }
  41.  
  42.             while ( start && !win )
  43.             {
  44.                 Menu2 ();
  45.                 DrawMap ();
  46.                 PlayerMovement ();
  47.                 Win ();
  48.             }
  49.  
  50.             if ( win )
  51.             {
  52.                 Console.Clear ();
  53.                 Menu3 ();
  54.             }
  55.  
  56.         }
  57.  
  58.         static void DrawMap ()
  59.         {
  60.             for ( int x = 0 ; x <= mapSize.GetLength ( 0 ) ; x++ )
  61.             {
  62.                 for ( int y = 0 ; y <= mapSize.GetLength ( 1 ) ; y++ )
  63.                 {
  64.                     if ( x == 0 || x == 10 || x == mapSize.GetLength ( 0 ) )
  65.                     {
  66.                         Console.SetCursorPosition ( x, y );
  67.                         Console.WriteLine ( "I" );
  68.                     }
  69.                     else if ( y == 0 || y == 5 || y == mapSize.GetLength ( 1 ) )
  70.                     {
  71.                         Console.SetCursorPosition ( x, y );
  72.                         Console.WriteLine ( "-" );
  73.                     }
  74.                 }
  75.             }
  76.         }
  77.  
  78.         static void PlayerSpawn ()
  79.         {
  80.             bool availiable = false;
  81.             int prewPosX;
  82.             int prewPosY;
  83.             prewPosX = playerPosX;
  84.             prewPosY = playerPosY;
  85.                 do
  86.                 {
  87.                     prewPosX = playerPosX;
  88.                     prewPosY = playerPosY;
  89.                     playerPosX = spawnPosition.Next ( 1, 19 );
  90.                     playerPosY = spawnPosition.Next ( 1, 9 );
  91.                 }
  92.                 while ( availiable );
  93.  
  94.             if ( playerPosX != 10 & playerPosY != 5 )
  95.             {
  96.                 availiable = true;
  97.                 DrawPlayer ();
  98.             }
  99.         }
  100.  
  101.         static void TeleportSpawn ()
  102.         {
  103.             bool availiable = false;
  104.  
  105.             do
  106.             {
  107.                 teleportPosX = spawnPosition.Next ( 1, 19 );
  108.                 teleportPosY = spawnPosition.Next ( 1, 9 );
  109.             }
  110.             while ( availiable );
  111.  
  112.             if ( teleportPosX != 10 & teleportPosY != 5 )
  113.             {
  114.                 availiable = true;
  115.                 DrawTeleport ();
  116.             }
  117.         }
  118.  
  119.         static void PlayerMovement ()
  120.         {
  121.             ConsoleKeyInfo key = Console.ReadKey ( true );
  122.  
  123.             switch ( key.Key )
  124.             {
  125.                 case ConsoleKey.W:
  126.                     if ( playerPosY > 1 & playerPosY != 6 )
  127.                     {
  128.                         ClearPrewPlayer ();
  129.                         playerPosY -= 1;
  130.                         DrawPlayer ();
  131.                     }
  132.                     else
  133.                         DrawPlayer ();
  134.                     break;
  135.  
  136.                 case ConsoleKey.S:
  137.                     if ( playerPosY < 9 & playerPosY != 4 )
  138.                     {
  139.                         ClearPrewPlayer ();
  140.                         playerPosY += 1;
  141.                         DrawPlayer ();
  142.                     }
  143.                     else
  144.                         DrawPlayer ();
  145.                     break;
  146.  
  147.                 case ConsoleKey.A:
  148.                     if ( playerPosX > 1 & playerPosX != 11 )
  149.                     {
  150.                         ClearPrewPlayer ();
  151.                         playerPosX -= 1;
  152.                         DrawPlayer ();
  153.                     }
  154.                     else
  155.                         DrawPlayer ();
  156.                     break;
  157.  
  158.                 case ConsoleKey.D:
  159.                     if ( playerPosX < 19 & playerPosX != 9 )
  160.                     {
  161.                         ClearPrewPlayer ();
  162.                         playerPosX += 1;
  163.                         DrawPlayer ();
  164.                     }
  165.                     else
  166.                         DrawPlayer ();
  167.                     break;
  168.  
  169.                 case ConsoleKey.E:
  170.                     ClearPrewPlayer ();
  171.                     start = true;
  172.                     break;
  173.             }
  174.         }
  175.  
  176.         static void DrawPlayer ()
  177.         {
  178.             Console.SetCursorPosition ( playerPosX, playerPosY );
  179.             Console.WriteLine ( player );
  180.         }
  181.  
  182.         static void DrawTeleport ()
  183.         {
  184.             Console.SetCursorPosition ( teleportPosX, teleportPosY );
  185.             Console.WriteLine ( teleport );
  186.         }
  187.  
  188.         static void ClearPrewPlayer ()
  189.         {
  190.             Console.SetCursorPosition ( playerPosX, playerPosY );
  191.             Console.WriteLine ( " " );
  192.             PlayerSpawn ();
  193.         }
  194.  
  195.         static void Win ()
  196.         {
  197.             if ( playerPosX == teleportPosX & playerPosY == teleportPosY )
  198.             {
  199.                 win = true;
  200.             }
  201.         }
  202.  
  203.         static void Menu1 ()
  204.         {
  205.             Console.SetCursorPosition ( 25, 2 );
  206.             Console.WriteLine ( "Для старта нажмите \"E\"".PadLeft ( 30 ) );
  207.         }
  208.  
  209.         static void Menu2 ()
  210.         {
  211.             Console.SetCursorPosition ( 25, 2 );
  212.             Console.WriteLine ( "Для передвижения используйте клавиши W,A,S,D".PadLeft ( 30 ) );
  213.             Console.SetCursorPosition ( 25, 4 );
  214.             Console.WriteLine ( "Для случайной телепортации нажмите клавишу E".PadLeft ( 30 ) );
  215.             Console.SetCursorPosition ( 25, 6 );
  216.             Console.WriteLine ( "Для победы зайдите в телепорт отмеченный знаком \"O\"".PadLeft ( 30 ) );
  217.         }
  218.  
  219.         static void Menu3 ()
  220.         {
  221.             Console.BackgroundColor = ConsoleColor.White;
  222.             Console.ForegroundColor = ConsoleColor.Blue;
  223.             Console.SetCursorPosition ( 15, 1 );
  224.             Console.WriteLine ( " ".PadLeft ( 49 ) );
  225.             Console.SetCursorPosition ( 15, 2 );
  226.             Console.WriteLine ( "\t\t\t  ПОЗДРАВЛЯЮ!\t\t\t" );
  227.             Console.SetCursorPosition ( 15, 3 );
  228.             Console.WriteLine ( "\t\t\t  ВЫ ПОБЕДИЛИ\t\t\t" );
  229.             Console.SetCursorPosition ( 15, 4 );
  230.             Console.WriteLine ( " ".PadLeft ( 49 ) );
  231.             Console.BackgroundColor = ConsoleColor.Black;
  232.             Console.ForegroundColor = ConsoleColor.White;
  233.         }
  234.     }
  235. }
Add Comment
Please, Sign In to add comment