Advertisement
MrVeiran

packman

Mar 16th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Threading;
  8.  
  9. namespace Task6
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             Thread thr = new Thread(new ThreadStart(MissionImp));
  16.             thr.Start();
  17.             int height =Console.WindowHeight;
  18.             Console.WindowHeight=height+10;
  19.            
  20.             Console.CursorVisible = false;
  21.             string[] NewFile = File.ReadAllLines("map.txt");
  22.             char[,] map = new char[NewFile.Length, NewFile[1].Length];
  23.             Random rand = new Random();
  24.  
  25.             int packmanY = 1, packmanX = 1;
  26.             int DX = 0, DY = 0;
  27.             int point = 0, pointMax = 0;
  28.             bool endGame = false;
  29.  
  30.             int ghostY = 10, ghostX = 10;
  31.             int DXG = 1, DYG = 0;
  32.  
  33.             for (int i = 0; i < map.GetLength(0); i++)
  34.                 for (int j = 0; j < map.GetLength(1); j++)
  35.                 {
  36.                     map[i, j] = NewFile[i][j];
  37.                 }
  38.  
  39.             for (int i = 0; i < map.GetLength(0); i++)
  40.             {
  41.                 for (int j = 0; j < map.GetLength(1); j++)
  42.                 {
  43.                     if (map[i, j] == '@')
  44.                     {
  45.                         packmanY = j;
  46.                         packmanX = i;
  47.                         map[i, j] = ' ';
  48.                     }
  49.                     else if (map[i, j] == ' ')
  50.                     {
  51.                         map[i, j] = '·';
  52.                         pointMax++;
  53.                     }
  54.                     else if (map[i, j] == '%')
  55.                     {
  56.                         ghostX = i;
  57.                         ghostY = j;
  58.                         map[i, j] = '.';
  59.                     }
  60.                     Console.Write(map[i, j]);
  61.                 }
  62.                 Console.WriteLine();
  63.             }
  64.  
  65.            
  66.             ConsoleKeyInfo Key;
  67.  
  68.             while (!endGame)
  69.             {
  70.                 if (Console.KeyAvailable)
  71.                 {
  72.                     Key = Console.ReadKey(true);
  73.                     switch (Key.Key)
  74.                     {
  75.                         case ConsoleKey.LeftArrow:
  76.                             DX = 0; DY = -1;
  77.                             break;
  78.                         case ConsoleKey.RightArrow:
  79.                             DX = 0; DY = 1;
  80.                             break;
  81.                         case ConsoleKey.UpArrow:
  82.                             DX = -1; DY = 0;
  83.                             break;
  84.                         case ConsoleKey.DownArrow:
  85.                             DX = 1; DY = 0;
  86.                             break;
  87.                     }
  88.                 }
  89.  
  90.                 System.Threading.Thread.Sleep(125);
  91.  
  92.  
  93.                 if (map[packmanX + DX, packmanY + DY] != '#')
  94.                 {
  95.  
  96.                     // pproverka()
  97.                     proverka(ref DX, ref DY,ref packmanY,ref packmanX, '@',map);
  98.  
  99.                     //
  100.                     if (map[packmanX, packmanY] == '·')
  101.                     {
  102.                         point++;
  103.                         map[packmanX, packmanY] = ' ';
  104.                         Console.SetCursorPosition(0, map.GetLength(0) + 5);
  105.                         Console.Write("Собрано очков: " + point + "/" + pointMax);
  106.                         if (point == pointMax) endGame = true;
  107.                     }
  108.                 }
  109.                 if (map[ghostX + DXG, ghostY + DYG] == '#')
  110.                 {
  111.                     int ghostDir = rand.Next(1, 5);
  112.                     switch (ghostDir)
  113.                     {
  114.                         case 1:
  115.                             DXG = 0; DYG = -1;
  116.                             break;
  117.                         case 2:
  118.                             DXG = 0; DYG = 1;
  119.                             break;
  120.                         case 3:
  121.                             DXG = -1; DYG = 0;
  122.                             break;
  123.                         case 4:
  124.                             DXG = 1; DYG = 0;
  125.                             break;
  126.                     }
  127.                 }
  128.                 else
  129.                 {
  130.                    
  131.                    
  132.                     proverka(ref DXG, ref DYG, ref ghostY, ref ghostX, '%',map);
  133.                     if (ghostX == packmanX && ghostY == packmanY) endGame = true;
  134.                 }
  135.                 // переход сверху вниз слево направо
  136.                 perehod(ref DX, ref DY, ref packmanY, ref packmanX, map,'@');
  137.                 perehod(ref DXG, ref DYG, ref ghostY, ref ghostX, map,'%');
  138.                
  139.  
  140.             }
  141.  
  142.             Console.SetCursorPosition(0, map.GetLength(0) + 8);
  143.         }
  144.  
  145.         static void perehod(ref int DX,ref int DY,ref int Y, ref int X, char[,]map, char chra)
  146.         {
  147.  
  148.             if (chra == '@')
  149.             {
  150.                 if (DY == 1 && Y == (map.GetLength(1) - 1))
  151.                 {
  152.                    
  153.                     Console.SetCursorPosition(Y, X);
  154.                     Console.Write(' ');
  155.                     Y = 0;
  156.                     Console.SetCursorPosition(Y, X);
  157.                     Console.Write('@');
  158.                 }
  159.                 if (DY == -1 && Y == 0)
  160.                 {
  161.                     Console.SetCursorPosition(Y, X);
  162.                     Console.Write(' ');
  163.                     Y = map.GetLength(1) - 1;
  164.                     Console.SetCursorPosition(Y, X);
  165.                     Console.Write('@');
  166.                 }
  167.                 if (DX == -1 && X == 0)
  168.                 {
  169.                     Console.SetCursorPosition(Y, X);
  170.                     Console.Write(' ');
  171.                     X = map.GetLength(0) - 1;
  172.                     Console.SetCursorPosition(Y, X);
  173.                     Console.Write('@');
  174.                 }
  175.                 if (DX == 1 && X == (map.GetLength(0) - 1))
  176.                 {
  177.                     Console.SetCursorPosition(Y, X);
  178.                     Console.Write(' ');
  179.                     X = 0;
  180.                     Console.SetCursorPosition(Y, X);
  181.                     Console.Write('@');
  182.                 }
  183.             }
  184.             if (chra == '%')
  185.             {
  186.                 if (DY == 1 && Y == (map.GetLength(1) - 1))
  187.                 {
  188.                     Console.SetCursorPosition(Y, X);
  189.                     Console.Write(map[X, Y]);
  190.                     Y = 0;
  191.                 }
  192.                 if (DY == -1 && Y == 0)
  193.                 {
  194.                     Console.SetCursorPosition(Y, X);
  195.                     Console.Write(map[X, Y]);
  196.                     Y = map.GetLength(1) - 1;
  197.                 }
  198.                 if (DX == -1 && X == 0)
  199.                 {
  200.                     Console.SetCursorPosition(Y, X);
  201.                     Console.Write(map[X, Y]);
  202.                     X = map.GetLength(0) - 1;
  203.                 }
  204.                 if (DX == 1 && X == (map.GetLength(0) - 1))
  205.                 {
  206.                     Console.SetCursorPosition(Y, X);
  207.                     Console.Write(map[X, Y]);
  208.                     X = 0;
  209.                 }
  210.             }
  211.  
  212.         }
  213.  
  214.         static void proverka(ref int DiX,ref int DiY,ref int Y, ref int X, char chra,char[,] map)
  215.         {
  216.             Console.ForegroundColor = ConsoleColor.White;
  217.             if (chra =='@')
  218.                 {
  219.                 Console.SetCursorPosition(Y, X);
  220.                
  221.                 Console.Write(' ');
  222.                 Console.ForegroundColor = ConsoleColor.Yellow;
  223.                 X += DiX;
  224.                 Y += DiY;
  225.                 Console.SetCursorPosition(Y, X);
  226.                 Console.Write(chra);
  227.                
  228.             }
  229.             if (chra =='%')
  230.                 {
  231.                 Console.SetCursorPosition(Y, X);
  232.                
  233.                 Console.Write(map[X, Y]);
  234.                 X += DiX;
  235.                 Y += DiY;
  236.                 Console.ForegroundColor = ConsoleColor.Red;
  237.                 Console.SetCursorPosition(Y, X);
  238.                 Console.Write(chra);
  239.             }
  240.             Console.ForegroundColor = ConsoleColor.White;
  241.  
  242.  
  243.         }
  244.  
  245.         public static void MissionImp()
  246.         {
  247.             while (true)
  248.             {
  249.                 Console.Beep(784, 150);
  250.                 Thread.Sleep(300);
  251.                 Console.Beep(784, 150);
  252.                 Thread.Sleep(300);
  253.                 Console.Beep(932, 150);
  254.                 Thread.Sleep(150);
  255.                 Console.Beep(1047, 150);
  256.                 Thread.Sleep(150);
  257.                 Console.Beep(784, 150);
  258.                 Thread.Sleep(300);
  259.                 Console.Beep(784, 150);
  260.                 Thread.Sleep(300);
  261.                 Console.Beep(699, 150);
  262.                 Thread.Sleep(150);
  263.                 Console.Beep(740, 150);
  264.                 Thread.Sleep(150);
  265.                 Console.Beep(784, 150);
  266.                 Thread.Sleep(300);
  267.                 Console.Beep(784, 150);
  268.                 Thread.Sleep(300);
  269.                 Console.Beep(932, 150);
  270.                 Thread.Sleep(150);
  271.                 Console.Beep(1047, 150);
  272.                 Thread.Sleep(150);
  273.                 Console.Beep(784, 150);
  274.                 Thread.Sleep(300);
  275.                 Console.Beep(784, 150);
  276.                 Thread.Sleep(300);
  277.                 Console.Beep(699, 150);
  278.                 Thread.Sleep(150);
  279.                 Console.Beep(740, 150);
  280.                 Thread.Sleep(150);
  281.                 Console.Beep(932, 150);
  282.                 Console.Beep(784, 150);
  283.                 Console.Beep(587, 1200);
  284.                 Thread.Sleep(75);
  285.                 Console.Beep(932, 150);
  286.                 Console.Beep(784, 150);
  287.                 Console.Beep(554, 1200);
  288.                 Thread.Sleep(75);
  289.                 Console.Beep(932, 150);
  290.                 Console.Beep(784, 150);
  291.                 Console.Beep(523, 1200);
  292.                 Thread.Sleep(150);
  293.                 Console.Beep(466, 150);
  294.                 Console.Beep(523, 150);
  295.             }
  296.         }
  297.  
  298.     }
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement