Advertisement
LeRoY_Go

Untitled

Feb 2nd, 2022
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.34 KB | None | 0 0
  1. using System;
  2.  
  3. namespace C_Sharp_Junior
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Console.WriteLine("Найди выход.Цель игры: Попасть игроком на клетку обозначеной знаком << X >>");
  10.             Console.ReadLine();
  11.             Console.Clear();
  12.             bool isEndGame = false;
  13.             Console.CursorVisible = false;
  14.             int playerX = 1, playerY = 1;
  15.             int playerDX = 1, playerDY = 1;
  16.             char[,] map = {{'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
  17.                            {'#',' ',' ',' ',' ',' ','#',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
  18.                            {'#',' ','#','#','#',' ','#',' ',' ','#','#','#','#','#','#',' ','#'},
  19.                            {'#',' ',' ',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  20.                            {'#','#','#',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ','#'},
  21.                            {'#',' ','#',' ','#','#','#',' ',' ','#','#',' ','#','#','#',' ','#'},
  22.                            {'#',' ','#',' ',' ',' ',' ',' ',' ','#',' ',' ','#','X','#',' ','#'},
  23.                            {'#',' ',' ',' ','#','#','#',' ',' ','#',' ',' ','#',' ',' ',' ','#'},
  24.                            {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'}};
  25.             DrawMap(map);
  26.             while (isEndGame == false)
  27.             {
  28.                 DrawPlayer(playerX, playerY);
  29.                 GetDirection(ref playerDX, ref playerDY);
  30.                 isEndGame = CheckEndGame(map, playerX, playerY, playerDX, playerDY);
  31.                 if (CheckCollision(map, playerX, playerY, playerDX, playerDY))
  32.                 {
  33.                     Move(ref playerX, ref playerY, playerDX, playerDY);
  34.                 }
  35.             }
  36.             Console.Clear();
  37.             Console.WriteLine("Поздравляю. Вы выиграли.");
  38.             Console.ReadLine();
  39.         }
  40.  
  41.         static void DrawMap(char[,] map)
  42.         {
  43.             for (int i = 0; i < map.GetLength(0); i++)
  44.             {
  45.                 for (int j = 0; j < map.GetLength(1); j++)
  46.                 {
  47.                     Console.Write(map[i, j]);
  48.                 }
  49.                 Console.WriteLine();
  50.             }
  51.         }
  52.  
  53.         static bool CheckEndGame(char[,] map, int playerX, int playerY, int playerDX, int playerDY)
  54.         {
  55.             return map[playerX + playerDX, playerY + playerDY] == 'X';
  56.         }
  57.  
  58.         static bool CheckCollision(char[,] map, int playerX, int playerY, int playerDX, int playerDY)
  59.         {
  60.             return map[playerX + playerDX, playerY + playerDY] != '#';
  61.         }
  62.  
  63.         static void Move(ref int playerX, ref int playerY, int playerDX, int playerDY)
  64.         {
  65.             Console.SetCursorPosition(playerY, playerX);
  66.             Console.Write(" ");
  67.             playerX += playerDX;
  68.             playerY += playerDY;
  69.             Console.SetCursorPosition(playerY, playerX);
  70.             Console.Write("@");
  71.         }
  72.  
  73.         static void GetDirection(ref int playerDX, ref int playerDY)
  74.         {
  75.             bool directionDefinitely = false;
  76.             while (directionDefinitely == false)
  77.             {
  78.                 if (Console.KeyAvailable)
  79.                 {
  80.                     ConsoleKeyInfo key = Console.ReadKey(true);
  81.                     switch (key.Key)
  82.                     {
  83.                         case ConsoleKey.UpArrow:
  84.                             playerDX = -1; playerDY = 0;
  85.                             break;
  86.                         case ConsoleKey.DownArrow:
  87.                             playerDX = 1; playerDY = 0;
  88.                             break;
  89.                         case ConsoleKey.LeftArrow:
  90.                             playerDX = 0; playerDY = -1;
  91.                             break;
  92.                         case ConsoleKey.RightArrow:
  93.                             playerDX = 0; playerDY = 1;
  94.                             break;
  95.                     }
  96.                     directionDefinitely = true;
  97.                 }
  98.             }
  99.         }
  100.  
  101.         static void DrawPlayer(int playerX, int playerY)
  102.         {
  103.             Console.SetCursorPosition(playerY, playerX);
  104.             Console.Write("@");
  105.             Console.ReadKey(true);
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement