ranee

пакман

Apr 16th, 2021 (edited)
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.28 KB | None | 0 0
  1. using System;
  2. namespace CSLight
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             Console.CursorVisible = false;
  9.             Random random = new Random();
  10.             bool isPlaying = true;
  11.             bool isAlive = true;
  12.             int pacmanX, pacmanY;
  13.             int pacmanDX = 0, pacmanDY = -1;
  14.             int ghostX, ghostY;
  15.             int ghostDX = 0, ghostDY = 1;
  16.             int allDots = 0, collectDots = 0;
  17.             string[] map1 =
  18.             {
  19.                 "###########################################################",
  20.                 "#                                                         #",
  21.                 "# ####################################################### #",
  22.                 "#                                                         #",
  23.                 "# ###########################   ######################### #",
  24.                 "#                            @        ####                #",
  25.                 "# ###########   ######################################### #",
  26.                 "#                                                         #",
  27.                 "# #######################    ############################ #",
  28.                 "#                                             $           #",
  29.                 "###########################################################"
  30.             };
  31.             char[,] map = ReadMap(map1, out pacmanX, out pacmanY, out ghostX, out ghostY, ref allDots);
  32.             DrowMap(map);
  33.             while (isPlaying == true)
  34.             {
  35.                 Console.SetCursorPosition(0, 20);
  36.                 Console.WriteLine($"Собрано: {collectDots}/{allDots}");
  37.                 if (Console.KeyAvailable)
  38.                 {
  39.                     ConsoleKeyInfo key = Console.ReadKey(true);
  40.                     ChangeDirection(key, ref pacmanDX, ref pacmanDY);
  41.                 }
  42.                 if (map[pacmanX + pacmanDX, pacmanY + pacmanDY] != '#')
  43.                 {
  44.                     CollectDots(map, pacmanX, pacmanY, ref collectDots);
  45.                     Move(map, '@', ref pacmanX, ref pacmanY, pacmanDX, pacmanDY);
  46.                 }
  47.                 if (map[ghostX + ghostDX, ghostY + ghostDY] != '#')
  48.                 {
  49.                     Move(map, '$', ref ghostX, ref ghostY, ghostDX, ghostDY);
  50.                 }
  51.                 else
  52.                 {
  53.                     ChangeDirection(random, ref ghostDX, ref ghostDY);
  54.                 }
  55.                 if (ghostX == pacmanX && ghostY == pacmanY)
  56.                 {
  57.                     isAlive = false;
  58.                 }
  59.                 System.Threading.Thread.Sleep(100);
  60.                 if (collectDots == allDots || !isAlive)
  61.                 {
  62.                     isPlaying = false;
  63.                 }
  64.             }
  65.             Console.SetCursorPosition(0, 19);
  66.             if ((collectDots) == allDots)
  67.             {
  68.                 Console.WriteLine("Вы победили.");
  69.             }
  70.             else if (!isAlive)
  71.             {
  72.                 Console.WriteLine("Вас съели.");
  73.             }
  74.         }
  75.  
  76.         static void ChangeDirection(ConsoleKeyInfo key, ref int DX, ref int DY)
  77.         {
  78.             switch (key.Key)
  79.             {
  80.                 case ConsoleKey.UpArrow:
  81.                     DX = -1; DY = 0;
  82.                     break;
  83.                 case ConsoleKey.DownArrow:
  84.                     DX = 1; DY = 0;
  85.                     break;
  86.                 case ConsoleKey.LeftArrow:
  87.                     DX = 0; DY = -1;
  88.                     break;
  89.                 case ConsoleKey.RightArrow:
  90.                     DX = 0; DY = 1;
  91.                     break;
  92.             }
  93.         }
  94.  
  95.         static void ChangeDirection(Random random, ref int DX, ref int DY)
  96.         {
  97.             int ghostDir = random.Next(1, 5);
  98.             switch (ghostDir)
  99.             {
  100.                 case 1:
  101.                     DX = -1; DY = 0;
  102.                     break;
  103.                 case 2:
  104.                     DX = 1; DY = 0;
  105.                     break;
  106.                 case 3:
  107.                     DX = 0; DY = -1;
  108.                     break;
  109.                 case 4:
  110.                     DX = 0; DY = 1;
  111.                     break;
  112.             }
  113.         }
  114.  
  115.         static void Move(char[,] map, char symbol, ref int X, ref int Y, int DX, int DY)
  116.         {
  117.             Console.SetCursorPosition(Y, X);
  118.             Console.Write(map[X, Y]);
  119.             X += DX;
  120.             Y += DY;
  121.             Console.SetCursorPosition(Y, X);
  122.             Console.Write(symbol);
  123.         }
  124.  
  125.         static void CollectDots(char[,] map, int X, int Y, ref int collectDots)
  126.         {
  127.             if (map[X, Y] == '.')
  128.             {
  129.                 collectDots++;
  130.                 map[X, Y] = ' ';
  131.             }
  132.         }
  133.  
  134.         static void DrowMap(char[,] map)
  135.         {
  136.             for (int i = 0; i < map.GetLength(0); i++)
  137.             {
  138.                 for (int j = 0; j < map.GetLength(1); j++)
  139.                 {
  140.                     Console.Write(map[i, j]);
  141.                 }
  142.                 Console.WriteLine();
  143.             }
  144.         }
  145.  
  146.         static char[,] ReadMap(string[] map1, out int pacmanX, out int pacmanY, out int ghostX, out int ghostY, ref int allDots)
  147.         {
  148.             pacmanX = 0;
  149.             pacmanY = 0;
  150.             ghostX = 0;
  151.             ghostY = 0;
  152.             char[,] map = new char[map1.Length, map1[0].Length];
  153.             for (int i = 0; i < map.GetLength(0); i++)
  154.             {
  155.                 for (int j = 0; j < map.GetLength(1); j++)
  156.                 {
  157.                     map[i, j] = map1[i][j];
  158.                     if (map[i, j] == '@')
  159.                     {
  160.                         pacmanX = i;
  161.                         pacmanY = j;
  162.                         map[i, j] = '.';
  163.                     }
  164.                     else if (map[i, j] == ' ')
  165.                     {
  166.                         map[i, j] = '.';
  167.                         allDots++;
  168.                     }
  169.                     else if (map[i, j] == '$')
  170.                     {
  171.                         ghostX = i;
  172.                         ghostY = j;
  173.                         map[i, j] = '.';
  174.                     }
  175.                 }
  176.             }
  177.             return map;
  178.         }
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment