Advertisement
LeRoY_Go

Untitled

Feb 2nd, 2022
745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.23 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.                 if (CheckCollision(map, playerX, playerY, playerDX, playerDY, ref isEndGame))
  31.                 {
  32.                     Move(ref playerX, ref playerY, playerDX, playerDY);
  33.                 }
  34.             }
  35.             Console.Clear();
  36.             Console.WriteLine("Поздравляю. Вы выиграли.");
  37.             Console.ReadLine();
  38.         }
  39.  
  40.         static void DrawMap(char[,] map)
  41.         {
  42.             for (int i = 0; i < map.GetLength(0); i++)
  43.             {
  44.                 for (int j = 0; j < map.GetLength(1); j++)
  45.                 {
  46.                     Console.Write(map[i, j]);
  47.                 }
  48.                 Console.WriteLine();
  49.             }
  50.         }
  51.  
  52.         static bool CheckCollision(char[,] map, int playerX, int playerY, int playerDX, int playerDY, ref bool isEndGame)
  53.         {
  54.             if (map[playerX + playerDX, playerY + playerDY] == 'X')
  55.             {
  56.                 isEndGame = true;
  57.             }
  58.             return map[playerX + playerDX, playerY + playerDY] != '#';
  59.         }
  60.  
  61.         static void Move(ref int playerX, ref int playerY, int playerDX, int playerDY)
  62.         {
  63.             Console.SetCursorPosition(playerY, playerX);
  64.             Console.Write(" ");
  65.             playerX += playerDX;
  66.             playerY += playerDY;
  67.             Console.SetCursorPosition(playerY, playerX);
  68.             Console.Write("@");
  69.         }
  70.  
  71.         static void GetDirection(ref int playerDX, ref int playerDY)
  72.         {
  73.             bool directionDefinitely = false;
  74.             while (directionDefinitely == false)
  75.             {
  76.                 if (Console.KeyAvailable)
  77.                 {
  78.                     ConsoleKeyInfo key = Console.ReadKey(true);
  79.                     switch (key.Key)
  80.                     {
  81.                         case ConsoleKey.UpArrow:
  82.                             playerDX = -1; playerDY = 0;
  83.                             break;
  84.                         case ConsoleKey.DownArrow:
  85.                             playerDX = 1; playerDY = 0;
  86.                             break;
  87.                         case ConsoleKey.LeftArrow:
  88.                             playerDX = 0; playerDY = -1;
  89.                             break;
  90.                         case ConsoleKey.RightArrow:
  91.                             playerDX = 0; playerDY = 1;
  92.                             break;
  93.                     }
  94.                     directionDefinitely = true;
  95.                 }
  96.             }
  97.         }
  98.  
  99.         static void DrawPlayer(int playerX, int playerY)
  100.         {
  101.             Console.SetCursorPosition(playerY, playerX);
  102.             Console.Write("@");
  103.             Console.ReadKey(true);
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement