Advertisement
pol9na

Untitled

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