Advertisement
holllowknight

ДЗ: World

Mar 19th, 2020 (edited)
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.08 KB | None | 0 0
  1. using System;
  2.  
  3. namespace world
  4. {
  5.     class Program
  6.     {
  7.         const int DimentionX = 0;
  8.         const int DimentionY = 1;
  9.         const char CellWall = '#';
  10.         const char CellBonus = '*';
  11.         const char CellPlayer = '@';
  12.         const char CellEmpty = ' ';
  13.  
  14.         static void Main(string[] args)
  15.         {
  16.  
  17.             char[,] map = new char[6, 6] {
  18.             {CellWall, CellWall, CellWall, CellWall, CellWall, CellWall},
  19.             {CellWall, CellBonus, CellEmpty, CellWall, CellBonus, CellWall},
  20.             {CellWall, CellEmpty, CellEmpty, CellWall, CellEmpty, CellWall},
  21.             {CellWall, CellEmpty, CellEmpty, CellWall, CellEmpty, CellWall},
  22.             {CellWall, CellBonus, CellEmpty, CellEmpty, CellEmpty, CellWall},
  23.             {CellWall, CellWall, CellWall, CellWall, CellWall, CellWall}};
  24.             int playerX = 1;
  25.             int playerY = 2;
  26.             int score = 0;
  27.             bool isRunning = true;
  28.             Console.CursorVisible = false;
  29.  
  30.             while (isRunning)
  31.             {
  32.                 Console.SetCursorPosition(0, 0);
  33.                 DrawMap(map, playerX, playerY);
  34.                 Console.SetCursorPosition(0, map.GetLength(DimentionX) + 1);
  35.                 Console.WriteLine($"Score: {score}");
  36.                 ConsoleKeyInfo userInput = Console.ReadKey();
  37.  
  38.                 switch (userInput.Key)
  39.                 {
  40.                     case ConsoleKey.UpArrow:
  41.                         if (IsPassable(playerY - 1, playerX))
  42.                             playerY--;
  43.                         break;
  44.  
  45.                     case ConsoleKey.DownArrow:
  46.                         if (IsPassable(playerY + 1, playerX))
  47.                             playerY++;
  48.                         break;
  49.  
  50.                     case ConsoleKey.LeftArrow:
  51.                         if (IsPassable(playerY, playerX - 1))
  52.                             playerX--;
  53.                         break;
  54.  
  55.                     case ConsoleKey.RightArrow:
  56.                         if (IsPassable(playerY, playerX + 1))
  57.                             playerX++;
  58.                         break;
  59.  
  60.                     case ConsoleKey.Escape:
  61.                         isRunning = false;
  62.                         break;
  63.                 }
  64.  
  65.                 if (map[playerY, playerX] == CellBonus)
  66.                 {
  67.                     map[playerY, playerX] = CellEmpty;
  68.                     score++;
  69.                 }
  70.             }
  71.  
  72.             bool IsPassable(int coordinateY, int coordinateX)
  73.             {
  74.                 return map[coordinateY, coordinateX] != CellWall;
  75.             }
  76.         }
  77.  
  78.         static void DrawMap(char[,] map, int playerX, int playerY)
  79.         {
  80.             for (int y = 0; y < map.GetLength(DimentionY); y++)
  81.             {
  82.                 for (int x = 0; x < map.GetLength(DimentionX); x++)
  83.                 {
  84.                     Console.Write(map[y, x]);
  85.                 }
  86.                 Console.WriteLine();
  87.             }
  88.  
  89.             Console.SetCursorPosition(playerX, playerY);
  90.             Console.Write(CellPlayer);
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement