kwest87

Untitled

Nov 25th, 2022
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.51 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.             int pacmanX, pacmanY;
  28.             int enemyX, enemyY;
  29.             int enemyDirectionX = 1, enemyDirectionY = 0;
  30.             int pacmanDirectionX = 0, pacmanDirectionY = 0;
  31.             int allCherry = 0, harvestedCherry = 0;
  32.             char[,] map = ReadMap("MapPacMan", out enemyX, out enemyY, out pacmanX, out pacmanY, ref allCherry);
  33.             DrawMap(map);
  34.  
  35.             while (isPlay)
  36.             {
  37.  
  38.                 harvestedCherry = PickUpCherry(map, pacmanX, pacmanY, harvestedCherry);
  39.  
  40.                 if (Console.KeyAvailable)
  41.                 {
  42.                     ConsoleKeyInfo key = Console.ReadKey(true);
  43.                     ChangeDireсtion(key, ref pacmanDirectionX, ref pacmanDirectionY);
  44.                 }
  45.  
  46.                 if (map[pacmanY + pacmanDirectionY, pacmanX + pacmanDirectionX] != '#')
  47.                 {
  48.                     Move(map, '@', ref pacmanX, ref pacmanY, pacmanDirectionX, pacmanDirectionY);
  49.                 }
  50.  
  51.                 if (map[enemyY + enemyDirectionY, enemyX + enemyDirectionX] != '#')
  52.                 {
  53.                     Move(map, '&', ref enemyX, ref enemyY, enemyDirectionX, enemyDirectionY);
  54.                 }
  55.                 else
  56.                 {
  57.                     Random random = new Random();
  58.                     int randomDirection = random.Next(0, 4);
  59.                     ChangeDireсtion(randomDirection, ref enemyDirectionX, ref enemyDirectionY);
  60.                 }
  61.  
  62.                 if (enemyX == pacmanX && enemyY == pacmanY)
  63.                 {
  64.                     Console.SetCursorPosition(0, 22);
  65.                     Console.WriteLine("Вы попались !");
  66.                     isPlay = false;
  67.                 }
  68.  
  69.                 if (harvestedCherry == allCherry)
  70.                 {
  71.                     Console.SetCursorPosition(0, 22);
  72.                     Console.WriteLine("Вы победили !");
  73.                     isPlay = false;
  74.                 }
  75.  
  76.                 Console.SetCursorPosition(0, 20);
  77.                 Console.WriteLine($"Собрано : {harvestedCherry}/{allCherry}");
  78.             }
  79.         }
  80.  
  81.  
  82.         static void ChangeDireсtion(int direction, ref int x, ref int y)
  83.         {
  84.             switch (direction)
  85.             {
  86.                 case 0:
  87.                     x = 0;
  88.                     y = -1;
  89.                     break;
  90.                 case 1:
  91.                     x = 0;
  92.                     y = 1;
  93.                     break;
  94.                 case 2:
  95.                     x = -1;
  96.                     y = 0;
  97.                     break;
  98.                 case 3:
  99.                     x = 1;
  100.                     y = 0;
  101.                     break;
  102.             }
  103.         }
  104.  
  105.         static void ChangeDireсtion(ConsoleKeyInfo key, ref int x, ref int y)
  106.         {
  107.             switch (key.Key)
  108.             {
  109.                 case ConsoleKey.UpArrow:
  110.                     x = 0;
  111.                     y = -1;
  112.                     break;
  113.                 case ConsoleKey.DownArrow:
  114.                     x = 0;
  115.                     y = 1;
  116.                     break;
  117.                 case ConsoleKey.LeftArrow:
  118.                     x = -1;
  119.                     y = 0;
  120.                     break;
  121.                 case ConsoleKey.RightArrow:
  122.                     x = 1;
  123.                     y = 0;
  124.                     break;
  125.             }
  126.         }
  127.  
  128.         static void Move(char[,] map, char creature, ref int x, ref int y, int directionX, int directionY)
  129.         {
  130.             Console.SetCursorPosition(x, y);
  131.             Console.Write(map[y, x]);
  132.  
  133.             Console.SetCursorPosition(x += directionX, y += directionY);
  134.             Console.Write(creature);
  135.             System.Threading.Thread.Sleep(100);
  136.         }
  137.  
  138.         static int PickUpCherry(char[,] map, int x, int y, int cherry)
  139.         {
  140.             if (map[y, x] == '.')
  141.             {
  142.                 cherry++;
  143.                 map[y, x] = ' ';
  144.             }
  145.  
  146.             return cherry;
  147.         }
  148.  
  149.         static void DrawMap(char[,] map)
  150.         {
  151.             for (int i = 0; i < map.GetLength(0); i++)
  152.             {
  153.                 for (int j = 0; j < map.GetLength(1); j++)
  154.                 {
  155.                     Console.Write(map[i, j]);
  156.                 }
  157.  
  158.                 Console.WriteLine();
  159.             }
  160.         }
  161.  
  162.         static char[,] ReadMap(string mapName, out int enemyX, out int enemyY, out int pacmanX, out int pacmanY, ref int allCHerry)
  163.         {
  164.             pacmanX = 1;
  165.             pacmanY = 0;
  166.             enemyX = 0;
  167.             enemyY = 0;
  168.             string[] newFile = File.ReadAllLines($"Maps/{mapName}.txt");
  169.             char[,] map = new char[newFile.Length, newFile[0].Length];
  170.  
  171.             for (int i = 0; i < map.GetLength(0); i++)
  172.             {
  173.                 for (int j = 0; j < map.GetLength(1); j++)
  174.                 {
  175.                     map[i, j] = newFile[i][j];
  176.  
  177.                     if (map[i, j] == '@')
  178.                     {
  179.                         pacmanX = j;
  180.                         pacmanY = i;
  181.                         map[i, j] = ' ';
  182.                     }
  183.                     else if (map[i, j] == '&')
  184.                     {
  185.                         enemyX = j;
  186.                         enemyY = i;
  187.                         map[i, j] = ' ';
  188.                     }
  189.                     else if (map[i, j] == ' ')
  190.                     {
  191.                         map[i, j] = '.';
  192.                         allCHerry++;
  193.                     }
  194.                 }
  195.             }
  196.  
  197.             return map;
  198.         }
  199.     }
  200. }
  201. [EndCode]
Advertisement
Add Comment
Please, Sign In to add comment