ranee

пакман

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