kwest87

Untitled

Nov 28th, 2022
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.77 KB | None | 0 0
  1. [StartCode]
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics.Eventing.Reader;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Security.Cryptography.X509Certificates;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. /* Сделать игровую карту с помощью двумерного массива. Сделать функцию рисования карты.
  11.  *
  12.  * Помимо этого, дать пользователю возможность перемещаться по карте и взаимодействовать с элементами
  13.  * (например пользователь не может пройти сквозь стену)
  14.  *
  15.  * Все элементы являются обычными символами
  16. */
  17.  
  18.  
  19. namespace ConsoleApp1
  20. {
  21.     internal class Program
  22.     {
  23.         static void Main(string[] args)
  24.         {
  25.             Console.CursorVisible = false;
  26.             bool isPlay = true;
  27.             char pacmanSumbol = '@';
  28.             char enemySumbol = '&';
  29.             char wallSumbol = '#';
  30.             char cherrySumbol = '.';
  31.             char emptySumbol = ' ';
  32.             int pacmanPositionX;
  33.             int pacmanPositionY;
  34.             int enemyPositionX;
  35.             int enemyPositionY;
  36.             int enemyDirectionX = 1;
  37.             int enemyDirectionY = 0;
  38.             int pacmanDirectionX = 0;
  39.             int pacmanDirectionY = 0;
  40.             int allCherry = 0;
  41.             int harvestedCherry = 0;
  42.             char[,] map = ReadMap("MapPacMan", out enemyPositionX, out enemyPositionY, out pacmanPositionX, out pacmanPositionY, ref allCherry, pacmanSumbol, enemySumbol, cherrySumbol, emptySumbol);
  43.             DrawMap(map);
  44.  
  45.             while (isPlay)
  46.             {
  47.                 harvestedCherry = PickUpCherry(map, pacmanPositionX, pacmanPositionY, harvestedCherry, cherrySumbol, emptySumbol);
  48.  
  49.                 if (Console.KeyAvailable)
  50.                 {
  51.                     ConsoleKeyInfo key = Console.ReadKey(true);
  52.                     ChangeDireсtion(key, ref pacmanDirectionX, ref pacmanDirectionY);
  53.                 }
  54.  
  55.                 if (map[pacmanPositionY + pacmanDirectionY, pacmanPositionX + pacmanDirectionX] != wallSumbol)
  56.                 {
  57.                     Move(map, pacmanSumbol, ref pacmanPositionX, ref pacmanPositionY, pacmanDirectionX, pacmanDirectionY);
  58.                 }
  59.  
  60.                 if (map[enemyPositionY + enemyDirectionY, enemyPositionX + enemyDirectionX] != wallSumbol)
  61.                 {
  62.                     Move(map, enemySumbol, ref enemyPositionX, ref enemyPositionY, enemyDirectionX, enemyDirectionY);
  63.                 }
  64.                 else
  65.                 {
  66.                     Random random = new Random();
  67.                     int randomDirection = random.Next(0, 4);
  68.                     ChangeDireсtion(randomDirection, ref enemyDirectionX, ref enemyDirectionY);
  69.                 }
  70.  
  71.                 if (enemyPositionX == pacmanPositionX && enemyPositionY == pacmanPositionY)
  72.                 {
  73.                     Console.SetCursorPosition(0, 22);
  74.                     Console.WriteLine("Вы попались !");
  75.                     isPlay = false;
  76.                 }
  77.  
  78.                 if (harvestedCherry == allCherry)
  79.                 {
  80.                     Console.SetCursorPosition(0, 22);
  81.                     Console.WriteLine("Вы победили !");
  82.                     isPlay = false;
  83.                 }
  84.  
  85.                 Console.SetCursorPosition(0, 20);
  86.                 Console.WriteLine($"Собрано : {harvestedCherry}/{allCherry}");
  87.             }
  88.         }
  89.  
  90.  
  91.         static void ChangeDireсtion(int direction, ref int directionX, ref int directionY)
  92.         {
  93.             switch (direction)
  94.             {
  95.                 case 0:
  96.                     directionX = 0;
  97.                     directionY = -1;
  98.                     break;
  99.                 case 1:
  100.                     directionX = 0;
  101.                     directionY = 1;
  102.                     break;
  103.                 case 2:
  104.                     directionX = -1;
  105.                     directionY = 0;
  106.                     break;
  107.                 case 3:
  108.                     directionX = 1;
  109.                     directionY = 0;
  110.                     break;
  111.             }
  112.         }
  113.  
  114.         static void ChangeDireсtion(ConsoleKeyInfo key, ref int positionX, ref int positionY)
  115.         {
  116.             const int LeftSide = 37;
  117.             const int UpperSide = 38;
  118.             const int RightSide = 39;
  119.             const int DownSide = 40;
  120.             int direction = Convert.ToChar(key.Key);
  121.            
  122.             switch (direction)
  123.             {
  124.                 case UpperSide:
  125.                     positionX = 0;
  126.                     positionY = -1;
  127.                     break;
  128.                 case DownSide:
  129.                     positionX = 0;
  130.                     positionY = 1;
  131.                     break;
  132.                 case LeftSide:
  133.                     positionX = -1;
  134.                     positionY = 0;
  135.                     break;
  136.                 case RightSide:
  137.                     positionX = 1;
  138.                     positionY = 0;
  139.                     break;
  140.             }
  141.         }
  142.  
  143.         static void Move(char[,] map, char creature, ref int positionX, ref int positionY, int directionX, int directionY)
  144.         {
  145.             Console.SetCursorPosition(positionX, positionY);
  146.             Console.Write(map[positionY, positionX]);
  147.             Console.SetCursorPosition(positionX += directionX, positionY += directionY);
  148.             Console.Write(creature);
  149.             System.Threading.Thread.Sleep(100);
  150.         }
  151.  
  152.         static int PickUpCherry(char[,] map, int positionX, int positionY, int cherry, char cherrySumbol, char emptySumbol)
  153.         {
  154.             if (map[positionY, positionX] == cherrySumbol)
  155.             {
  156.                 cherry++;
  157.                 map[positionY, positionX] = emptySumbol;
  158.             }
  159.  
  160.             return cherry;
  161.         }
  162.  
  163.         static void DrawMap(char[,] map)
  164.         {
  165.             for (int i = 0; i < map.GetLength(0); i++)
  166.             {
  167.                 for (int j = 0; j < map.GetLength(1); j++)
  168.                 {
  169.                     Console.Write(map[i, j]);
  170.                 }
  171.  
  172.                 Console.WriteLine();
  173.             }
  174.         }
  175.  
  176.         static char[,] ReadMap(string mapName, out int enemyPositionX, out int enemyPositionY, out int pacmanPositionX, out int pacmanPositionY, ref int allCHerry, char pacmanSumbol, char enemySumbol, char cherrySumbol, char emptySumbol)
  177.         {
  178.             pacmanPositionX = 1;
  179.             pacmanPositionY = 0;
  180.             enemyPositionX = 0;
  181.             enemyPositionY = 0;
  182.             string[] newFile = File.ReadAllLines($"Maps/{mapName}.txt");
  183.             char[,] map = new char[newFile.Length, newFile[0].Length];
  184.  
  185.             for (int i = 0; i < map.GetLength(0); i++)
  186.             {
  187.                 for (int j = 0; j < map.GetLength(1); j++)
  188.                 {
  189.                     map[i, j] = newFile[i][j];
  190.  
  191.                     if (map[i, j] == pacmanSumbol)
  192.                     {
  193.                         pacmanPositionX = j;
  194.                         pacmanPositionY = i;
  195.                         map[i, j] = emptySumbol;
  196.                     }
  197.                     else if (map[i, j] == enemySumbol)
  198.                     {
  199.                         enemyPositionX = j;
  200.                         enemyPositionY = i;
  201.                         map[i, j] = emptySumbol;
  202.                     }
  203.                     else if (map[i, j] == emptySumbol)
  204.                     {
  205.                         map[i, j] = cherrySumbol;
  206.                         allCHerry++;
  207.                     }
  208.                 }
  209.             }
  210.  
  211.             return map;
  212.         }
  213.     }
  214. }
  215. [EndCode]
Advertisement
Add Comment
Please, Sign In to add comment