Advertisement
capitan007

N4_1 pacman

Apr 19th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.84 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace pac_man
  5. {
  6.     class Program
  7.     {
  8.         static int pacX = 0, pacY = 0;
  9.         static int DX = 0, DY = 0;
  10.         static int ghostX = 0, ghostY = 0;
  11.         static int GDX = 1, GDY = 0;
  12.         static char[,] map;
  13.         static string[] NewFile;
  14.         static int points = 0;
  15.         static int allDots = 0;
  16.         static ConsoleKeyInfo key;
  17.         static bool kill = false;
  18.  
  19.         static void Main(string[] args)
  20.         {
  21.             Console.CursorVisible = false;
  22.  
  23.             MapWrite();
  24.  
  25.             while (!kill)
  26.             {
  27.                 System.Threading.Thread.Sleep(120);
  28.  
  29.                 SetMoving();
  30.  
  31.                 if (map[pacY + DY, pacX + DX] != '#')
  32.                 {
  33.                     PacGo();
  34.                     if (allDots == points) break;
  35.                 }
  36.  
  37.                 if (map[ghostY + GDY, ghostX + GDX] == '#')
  38.                     GhostToPac();
  39.                 else
  40.                 {
  41.                     GhostToPac();
  42.                     GhostGo();
  43.                 }
  44.  
  45.                 if (ghostX == pacX && ghostY == pacY) kill = true;
  46.             }
  47.  
  48.             Console.SetCursorPosition(0, map.GetLength(0) + 7);
  49.  
  50.             if (kill)
  51.                 Console.Write("Ты умер.");
  52.             else
  53.             {
  54.                 Console.Write("ВЫ ПОБЕДИЛИ!");
  55.                 Console.ReadKey();
  56.             }
  57.         }
  58.  
  59.         private static void MapWrite()
  60.         {
  61.             NewFile = File.ReadAllLines("D:\\map.txt");
  62.             map = new char[NewFile.Length, NewFile[0].Length];
  63.  
  64.             for (int i = 0; i < map.GetLength(0); i++)
  65.                 for (int j = 0; j < map.GetLength(1); j++)
  66.                     map[i, j] = NewFile[i][j];
  67.  
  68.             for (int i = 0; i < map.GetLength(0); i++)
  69.             {
  70.                 for (int j = 0; j < map.GetLength(1); j++)
  71.                 {
  72.                     if (map[i, j] == '@')
  73.                     {
  74.                         pacX = j;
  75.                         pacY = i;
  76.                         map[i, j] = ' ';
  77.                     }
  78.                     else if (map[i, j] == ' ')
  79.                     {
  80.                         map[i, j] = '.';
  81.                         allDots++;
  82.                     }
  83.                     else if (map[i, j] == 'M')
  84.                     {
  85.                         ghostX = j;
  86.                         ghostY = i;
  87.                         map[i, j] = ' ';
  88.                     }
  89.                     Console.Write(map[i, j]);
  90.                 }
  91.  
  92.                 Console.Write('\n');
  93.             }
  94.         }
  95.  
  96.         private static void SetMoving()
  97.         {
  98.             if (Console.KeyAvailable)
  99.             {
  100.                 key = Console.ReadKey(true);
  101.                 switch (key.Key)
  102.                 {
  103.                     case ConsoleKey.UpArrow:
  104.                         DX = 0; DY = -1;
  105.                         break;
  106.                     case ConsoleKey.DownArrow:
  107.                         DX = 0; DY = 1;
  108.                         break;
  109.                     case ConsoleKey.LeftArrow:
  110.                         DX = -1; DY = 0;
  111.                         break;
  112.                     case ConsoleKey.RightArrow:
  113.                         DX = 1; DY = 0;
  114.                         break;
  115.                 }
  116.             }
  117.         }
  118.  
  119.         private static void PacGo()
  120.         {
  121.             TelePort();
  122.  
  123.             Console.SetCursorPosition(pacX, pacY);
  124.             Console.Write(' ');
  125.             pacX += DX;
  126.             pacY += DY;
  127.             Console.SetCursorPosition(pacX, pacY);
  128.             Console.Write('@');
  129.             if (map[pacY, pacX] == '.')
  130.             {
  131.                 points++;
  132.                 map[pacY, pacX] = ' ';
  133.                 Console.SetCursorPosition(0, map.GetLength(0) + 5);
  134.                 Console.Write("У вас " + points + " очков");
  135.             }
  136.         }
  137.  
  138.         private static void GhostToPac()
  139.         {
  140.             if (pacX > ghostX && map[ghostY, ghostX + 1] == ' ' ||
  141.                pacX > ghostX && map[ghostY, ghostX + 1] == '.')
  142.             {
  143.                 GDX = 1;
  144.                 GDY = 0;
  145.             }
  146.  
  147.             if (pacX < ghostX && map[ghostY, ghostX - 1] == ' ' ||
  148.                 pacX < ghostX && map[ghostY, ghostX - 1] == '.')
  149.             {
  150.                 GDX = -1;
  151.                 GDY = 0;
  152.             }
  153.  
  154.             if (pacY > ghostY && map[ghostY + 1, ghostX] == ' ' ||
  155.                 pacY > ghostY && map[ghostY + 1, ghostX] == '.')
  156.             {
  157.                 GDX = 0;
  158.                 GDY = 1;
  159.             }
  160.  
  161.             if (pacY < ghostY && map[ghostY - 1, ghostX] == ' ' ||
  162.                pacY < ghostY && map[ghostY - 1, ghostX] == '.')
  163.             {
  164.                 GDX = 0;
  165.                 GDY = -1;
  166.             }
  167.         }
  168.  
  169.         private static void GhostGo()
  170.         {
  171.             Console.SetCursorPosition(ghostX, ghostY);
  172.             Console.Write(map[ghostY, ghostX]);
  173.             ghostX += GDX;
  174.             ghostY += GDY;
  175.             Console.SetCursorPosition(ghostX, ghostY);
  176.             Console.Write('M');
  177.         }
  178.  
  179.         private static void TelePort()
  180.         {
  181.             if (map[pacY, pacX] == '%')
  182.             {
  183.                 Console.SetCursorPosition(pacX, pacY);
  184.                 Console.Write('%');
  185.                 pacX = 1;
  186.                 pacY = 1;
  187.                 Console.SetCursorPosition(pacX, pacY);
  188.                 Console.Write('@');
  189.             }
  190.             else if (map[pacY, pacX] == '$')
  191.             {
  192.                 Console.SetCursorPosition(pacX, pacY);
  193.                 Console.Write('$');
  194.                 pacX = 25;
  195.                 pacY = 1;
  196.                 Console.SetCursorPosition(pacX, pacY);
  197.                 Console.Write('@');
  198.             }
  199.         }
  200.     }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement