Advertisement
Vapio

task21

Apr 11th, 2021 (edited)
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.27 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.IO;
  4. using System.Threading;
  5.  
  6. public class Program
  7. {
  8.     public static void Main()
  9.     {
  10.         ConsoleKey[] consoleKeys = {ConsoleKey.UpArrow, ConsoleKey.LeftArrow,
  11.                                     ConsoleKey.RightArrow, ConsoleKey.DownArrow};
  12.         Random random = new Random();
  13.         char[,] gameMap = null;
  14.        
  15.         int playerX = 1;
  16.         int playerY = 1;
  17.         int allDots = 0;
  18.         int collectDots = 0;
  19.  
  20.         int enemyX = 5;
  21.         int enemyY = 1;
  22.         int direction = 0;
  23.  
  24.         bool isRun = true;
  25.  
  26.         Console.CursorVisible = false;
  27.         ReadFile(ref gameMap, "map", playerX, playerY, ref allDots);
  28.         DrawMap(gameMap);
  29.  
  30.         int scoreX = gameMap.GetLength(1) + 4;
  31.         int scoreY = 4;
  32.  
  33.         DrawScore(scoreX, scoreY, allDots, collectDots);
  34.  
  35.         while (isRun)
  36.         {
  37.             DrawPlayer(ref playerX, ref playerY, gameMap, ref collectDots);
  38.             DrawEnemy(ref enemyX, ref enemyY, ref direction, gameMap, consoleKeys, random);
  39.             DrawScore(scoreX, scoreY, allDots, collectDots);
  40.  
  41.             if (collectDots == allDots || (playerX == enemyX && playerY == enemyY))
  42.             {
  43.                 Console.Clear();
  44.                 isRun = false;
  45.             }
  46.         }
  47.     }
  48.  
  49.     public static void ReadFile(ref char[,] map, string fileName, int playerX, int playerY, ref int allDots)
  50.     {
  51.         string[] fileArray = File.ReadAllLines($"../../../{fileName}.txt");
  52.         map = new char[fileArray.Length, fileArray[0].Length];
  53.  
  54.         for (int i = 0; i < fileArray.Length; ++i)
  55.         {
  56.             for (int j = 0; j < fileArray[0].Length; ++j)
  57.             {
  58.                 if (fileArray[i][j] == ' ')
  59.                 {
  60.                     map[i, j] = '*';
  61.                     ++allDots;
  62.                 }
  63.                 else
  64.                 {
  65.                     map[i, j] = '#';
  66.                 }
  67.             }
  68.         }
  69.  
  70.         --allDots;
  71.         map[playerY, playerX] = '@';
  72.     }
  73.  
  74.     public static void DrawMap(char[,] map)
  75.     {
  76.         for (int i = 0; i < map.GetLength(0); ++i)
  77.         {
  78.             for (int j = 0; j < map.GetLength(1); ++j)
  79.                 Console.Write(map[i, j]);
  80.             Console.WriteLine();
  81.         }
  82.         Console.WriteLine();
  83.     }
  84.  
  85.     public static void DrawScore(int x, int y, int allDots, int collectDots)
  86.     {
  87.         Console.SetCursorPosition(x, y);
  88.         Console.WriteLine($"All Dots : {allDots}");
  89.         Console.SetCursorPosition(x, y + 1);
  90.         Console.WriteLine($"Collect Dots : {collectDots}");
  91.     }
  92.  
  93.     public static void DrawEnemy(ref int positionX, ref int positionY, ref int direction,
  94.                                  char[,] map, ConsoleKey[] keys, Random random)
  95.     {
  96.         int directionX = 0;
  97.         int directionY = 0;
  98.  
  99.         ChangePosition(keys[direction], ref directionX, ref directionY);
  100.        
  101.         if (map[positionY + directionY, positionX + directionX] != '#')
  102.         {
  103.             Console.SetCursorPosition(positionX, positionY);
  104.             Console.Write(map[positionY, positionX]);
  105.  
  106.             positionX += directionX;
  107.             positionY += directionY;
  108.  
  109.             Console.SetCursorPosition(positionX, positionY);
  110.             Console.Write('$');
  111.         }
  112.         else
  113.         {
  114.             direction = random.Next(0, keys.Length);
  115.         }
  116.  
  117.         Thread.Sleep(50);
  118.     }
  119.  
  120.     public static void DrawPlayer(ref int positionX, ref int positionY, char[,] map, ref int collectDots)
  121.     {
  122.         if (Console.KeyAvailable)
  123.         {
  124.             int directionX = 0;
  125.             int directionY = 0;
  126.  
  127.             ConsoleKey key = Console.ReadKey(true).Key;
  128.  
  129.             ChangePosition(key, ref directionX, ref directionY);
  130.  
  131.             if (map[positionY + directionY, positionX + directionX] != '#')
  132.             {
  133.                 Console.SetCursorPosition(positionX, positionY);
  134.                 map[positionY, positionX] = ' ';
  135.                 Console.Write(" ");
  136.  
  137.                 positionX += directionX;
  138.                 positionY += directionY;
  139.  
  140.                 if (map[positionY, positionX] == '*')
  141.                 {
  142.                     ++collectDots;
  143.                     map[positionY, positionX] = ' ';
  144.                 }
  145.  
  146.                 Console.SetCursorPosition(positionX, positionY);
  147.                 map[positionY, positionX] = '@';
  148.                 Console.Write('@');
  149.             }
  150.         }
  151.     }
  152.  
  153.     public static void ChangePosition(ConsoleKey key, ref int directionX, ref int directionY)
  154.     {
  155.         switch (key)
  156.         {
  157.             case ConsoleKey.UpArrow:
  158.             case ConsoleKey.W:
  159.                 directionX = 0;
  160.                 directionY = -1;
  161.                 break;
  162.             case ConsoleKey.RightArrow:
  163.             case ConsoleKey.D:
  164.                 directionX = 1;
  165.                 directionY = 0;
  166.                 break;
  167.             case ConsoleKey.LeftArrow:
  168.             case ConsoleKey.A:
  169.                 directionX = -1;
  170.                 directionY = 0;
  171.                 break;
  172.             case ConsoleKey.DownArrow:
  173.             case ConsoleKey.S:
  174.                 directionX = 0;
  175.                 directionY = 1;
  176.                 break;
  177.         }
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement