kwest87

Untitled

Nov 27th, 2022
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.58 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.             int direction = Convert.ToChar(key.Key);
  117.  
  118.             switch (direction)
  119.             {
  120.                 case 38:
  121.                     positionX = 0;
  122.                     positionY = -1;
  123.                     break;
  124.                 case 40:
  125.                     positionX = 0;
  126.                     positionY = 1;
  127.                     break;
  128.                 case 37:
  129.                     positionX = -1;
  130.                     positionY = 0;
  131.                     break;
  132.                 case 39:
  133.                     positionX = 1;
  134.                     positionY = 0;
  135.                     break;
  136.             }
  137.         }
  138.  
  139.         static void Move(char[,] map, char creature, ref int positionX, ref int positionY, int directionX, int directionY)
  140.         {
  141.             Console.SetCursorPosition(positionX, positionY);
  142.             Console.Write(map[positionY, positionX]);
  143.             Console.SetCursorPosition(positionX += directionX, positionY += directionY);
  144.             Console.Write(creature);
  145.             System.Threading.Thread.Sleep(100);
  146.         }
  147.  
  148.         static int PickUpCherry(char[,] map, int positionX, int positionY, int cherry,char cherrySumbol,char emptySumbol)
  149.         {
  150.             if (map[positionY, positionX] == cherrySumbol)
  151.             {
  152.                 cherry++;
  153.                 map[positionY, positionX] = emptySumbol;
  154.             }
  155.  
  156.             return cherry;
  157.         }
  158.  
  159.         static void DrawMap(char[,] map)
  160.         {
  161.             for (int i = 0; i < map.GetLength(0); i++)
  162.             {
  163.                 for (int j = 0; j < map.GetLength(1); j++)
  164.                 {
  165.                     Console.Write(map[i, j]);
  166.                 }
  167.  
  168.                 Console.WriteLine();
  169.             }
  170.         }
  171.  
  172.         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)
  173.         {
  174.             pacmanPositionX = 1;
  175.             pacmanPositionY = 0;
  176.             enemyPositionX = 0;
  177.             enemyPositionY = 0;
  178.             string[] newFile = File.ReadAllLines($"Maps/{mapName}.txt");
  179.             char[,] map = new char[newFile.Length, newFile[0].Length];
  180.  
  181.             for (int i = 0; i < map.GetLength(0); i++)
  182.             {
  183.                 for (int j = 0; j < map.GetLength(1); j++)
  184.                 {
  185.                     map[i, j] = newFile[i][j];
  186.  
  187.                     if (map[i, j] == pacmanSumbol)
  188.                     {
  189.                         pacmanPositionX = j;
  190.                         pacmanPositionY = i;
  191.                         map[i, j] = emptySumbol;
  192.                     }
  193.                     else if (map[i, j] == enemySumbol)
  194.                     {
  195.                         enemyPositionX = j;
  196.                         enemyPositionY = i;
  197.                         map[i, j] = emptySumbol;
  198.                     }
  199.                     else if (map[i, j] == emptySumbol)
  200.                     {
  201.                         map[i, j] = cherrySumbol;
  202.                         allCHerry++;
  203.                     }
  204.                 }
  205.             }
  206.  
  207.             return map;
  208.         }
  209.     }
  210. }
  211. [EndCode]
Advertisement
Add Comment
Please, Sign In to add comment