Advertisement
Guest User

Untitled

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