Advertisement
nikitaTheSlayer

PacMan

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