Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace pacman
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             Console.CursorVisible = false;
  15.  
  16.             Random rnd = new Random();
  17.             bool win = false;
  18.  
  19.             int maxPoint = 0;
  20.             int point = 0;
  21.  
  22.             // Карта из файла
  23.             string[] newFile = File.ReadAllLines("map.txt");
  24.             char[,] map = new char[newFile.Length, newFile[1].Length];
  25.  
  26.             // PACMAM началные свойства
  27.             int pacX = 0, pacY = 0;
  28.             int DX = 0, DY = 0;
  29.             bool die = false;
  30.  
  31.             // GHOST начальные свойства
  32.             int ghostX = 0, ghostY = 0;
  33.             int GDX = 1, GDY = 0;
  34.             int ghostDir = 0;
  35.  
  36.             // Читаем из файла и заносим в память карту
  37.  
  38.             generateMap(ref map, ref newFile);
  39.  
  40.             // Выводим карту на экран
  41.  
  42.             renderMap(map, ref pacX, ref pacY, ref maxPoint, ref ghostX, ref ghostY);
  43.            
  44.             // УПРАВЛЕНИЕ
  45.  
  46.             mainLoop(ref win, ref die, ref DX, ref DY, ref GDX, ref GDY, map, ref pacX, ref pacY, ref ghostX, ref ghostY, ref ghostDir, ref point, ref maxPoint, ref rnd);
  47.  
  48.             if(win)
  49.             {
  50.                 Console.Clear();
  51.                 Console.Write("Вы выиграли\n");
  52.             }
  53.             else
  54.             {
  55.                 Console.Clear();
  56.                 Console.Write("Вы проиграли\n");
  57.             }
  58.         }
  59.  
  60.         static void generateMap(ref char[,] map, ref string[] newFile)
  61.         {
  62.             for (int i = 0; i < map.GetLength(0); i++)
  63.                 for (int j = 0; j < map.GetLength(1); j++)
  64.                     map[i, j] = newFile[i][j];
  65.         }
  66.         static void renderMap(char[,] map, ref int pacX, ref int pacY, ref int maxPoint, ref int ghostX, ref int ghostY)
  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.                         maxPoint++;
  82.                     }
  83.                     else if (map[i, j] == '0')
  84.                     {
  85.                         map[i, j] = ' ';
  86.                     }
  87.                     else if (map[i, j] == 'X')
  88.                     {
  89.                         map[i, j] = 'X';
  90.                     }
  91.                     else if (map[i, j] == '%')
  92.                     {
  93.                         ghostX = j;
  94.                         ghostY = i;
  95.                         map[i, j] = '.';
  96.                     }
  97.                     Console.Write(map[i, j]);
  98.                 }
  99.                 Console.WriteLine();
  100.             }
  101.         }
  102.         static void movePacman(ref int DX, ref int DY)
  103.         {
  104.             ConsoleKeyInfo key;
  105.             if (Console.KeyAvailable)
  106.             {
  107.                 key = Console.ReadKey(true);
  108.                 switch (key.Key)
  109.                 {
  110.                     case ConsoleKey.UpArrow:
  111.                         DX = 0; DY = -1;
  112.                         break;
  113.                     case ConsoleKey.DownArrow:
  114.                         DX = 0; DY = 1;
  115.                         break;
  116.                     case ConsoleKey.LeftArrow:
  117.                         DX = -1; DY = 0;
  118.                         break;
  119.                     case ConsoleKey.RightArrow:
  120.                         DX = 1; DY = 0;
  121.                         break;
  122.                 }
  123.             }
  124.         }
  125.         static void moveGhost(char[,] map, ref int ghostY, ref int ghostX, ref int GDX, ref int GDY, ref int ghostDir, int pacX, int pacY, ref bool die, ref Random rnd)
  126.         {
  127.             if (map[ghostY + GDY, ghostX + GDX] == '#')
  128.             {
  129.                 ghostDir = rnd.Next(1, 5);
  130.  
  131.                 switch (ghostDir)
  132.                 {
  133.                     case 1:
  134.                         GDX = 0; GDY = 1;
  135.                         break;
  136.                     case 2:
  137.                         GDX = 0; GDY = -1;
  138.                         break;
  139.                     case 3:
  140.                         GDX = -1; GDY = 0;
  141.                         break;
  142.                     case 4:
  143.                         GDX = 1; GDY = 0;
  144.                         break;
  145.                 }
  146.             }
  147.             else
  148.             {
  149.                 Console.SetCursorPosition(ghostX, ghostY);
  150.                 Console.Write(map[ghostY, ghostX]);
  151.                 ghostX += GDX;
  152.                 ghostY += GDY;
  153.                 Console.SetCursorPosition(ghostX, ghostY);
  154.                 Console.Write('%');
  155.                 if (ghostX == pacX && ghostY == pacY)
  156.                 {
  157.                     die = true;
  158.                 }
  159.             }
  160.         }
  161.         static void pacmanCollision( char[,] map, ref int pacY, ref int pacX, ref int DX, ref int DY, ref int point, ref int maxPoint) {
  162.             if (map[pacY + DY, pacX + DX] != '#')
  163.             {
  164.                 Console.SetCursorPosition(pacX, pacY);
  165.                 Console.Write(' ');
  166.                 pacX += DX;
  167.                 pacY += DY;
  168.                 Console.SetCursorPosition(pacX, pacY);
  169.                 Console.Write('@');
  170.                 Console.SetCursorPosition(0, map.GetLength(0) + 1);
  171.                 Console.Write($"Координаты   {pacX}   /   {pacY}  ");
  172.  
  173.  
  174.                 if (map[pacY, pacX] == '.')
  175.                 {
  176.                     point++;
  177.                     map[pacY, pacX] = ' ';
  178.  
  179.                     Console.SetCursorPosition(0, map.GetLength(0) + 2);
  180.                     Console.Write($"Вы собрали {point} / {maxPoint}");
  181.                 }
  182.                 else if (map[pacY, pacX] == 'X')
  183.                 {
  184.  
  185.                     if (pacX == 28 && pacY == 9)
  186.                     {
  187.                         Console.SetCursorPosition(28, 9);
  188.                         Console.Write('X');
  189.                         Console.SetCursorPosition(0, 9);
  190.                         Console.Write('X');
  191.                         pacX = 1;
  192.                         pacY = 9;
  193.  
  194.                     }
  195.                     else
  196.                     {
  197.                         Console.SetCursorPosition(0, 9);
  198.                         Console.Write('X');
  199.                         Console.SetCursorPosition(28, 9);
  200.                         Console.Write('X');
  201.                         pacX = 27;
  202.                         pacY = 9;
  203.                     }
  204.  
  205.                 }
  206.             }
  207.         }
  208.         static void mainLoop(ref bool win, ref bool die, ref int DX, ref int DY, ref int GDX, ref int GDY, char[,] map, ref int pacX, ref int pacY, ref int ghostX, ref int ghostY, ref int ghostDir, ref int point, ref int maxPoint, ref Random rnd)
  209.         {
  210.            
  211.             while (!win && !die)
  212.             {
  213.                 movePacman(ref DX, ref DY);
  214.  
  215.                 System.Threading.Thread.Sleep(100);
  216.  
  217.                 pacmanCollision(map, ref pacY, ref pacX, ref DX, ref DY, ref point, ref maxPoint);
  218.  
  219.                 moveGhost(map, ref ghostY, ref ghostX, ref GDX, ref GDY, ref ghostDir, pacX, pacY, ref die, ref rnd);
  220.  
  221.  
  222.                 if (point == maxPoint)
  223.                 {
  224.                     win = true;
  225.                 }
  226.             }
  227.         }
  228.     }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement