Advertisement
holllowknight

Карта

Apr 12th, 2023 (edited)
732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.31 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace world
  5. {
  6.     class Program
  7.     {
  8.         const char CellWall = '#';
  9.         const char CellBonus = '*';
  10.         const char CellPlayer = '@';
  11.         const char CellEmpty = ' ';
  12.  
  13.         static void Main(string[] args)
  14.         {
  15.             char[,] map = LoadMap("map");
  16.             Console.CursorVisible = false;
  17.             int playerPositionX = 1;
  18.             int playerPositionY = 1;
  19.             int scorePositionY = map.GetLength(1);
  20.             int score = 0;
  21.             bool isRunning = true;
  22.  
  23.             while (isRunning)
  24.             {
  25.                 DrawMap(map);
  26.                 DrawPlayer(playerPositionX, playerPositionY);
  27.                 DrawScore(score, scorePositionY);
  28.  
  29.                 ConsoleKeyInfo pressedKey = Console.ReadKey();
  30.                 HandleInput(pressedKey, ref playerPositionX, ref playerPositionY, map, ref isRunning);
  31.                 score = CollectBonus(score, playerPositionX, playerPositionY, map);
  32.             }
  33.         }
  34.  
  35.         private static void DrawScore(int score, int scorePositionY)
  36.         {
  37.             Console.SetCursorPosition(0, scorePositionY);
  38.             Console.WriteLine($"Score: {score}");
  39.         }
  40.  
  41.         private static void DrawMap(char[,] map)
  42.         {
  43.             Console.SetCursorPosition(0, 0);
  44.  
  45.             for (int y = 0; y < map.GetLength(1); y++)
  46.             {
  47.                 for (int x = 0; x < map.GetLength(0); x++)
  48.                 {
  49.                     Console.Write(map[x, y]);
  50.                 }
  51.  
  52.                 Console.WriteLine();
  53.             }
  54.         }
  55.  
  56.         private static char[,] LoadMap(string path)
  57.         {
  58.             string[] file = File.ReadAllLines(path);
  59.             char[,] map = new char[GetMaxLengthOfLine(file), file.Length];
  60.  
  61.             for (int x = 0; x < map.GetLength(0); x++)
  62.             {
  63.                 for (int y = 0; y < map.GetLength(1); y++)
  64.                 {
  65.                     if (x < file[y].Length)
  66.                         map[x, y] = file[y][x];
  67.                     else
  68.                         map[x, y] = CellEmpty;
  69.                 }
  70.             }
  71.  
  72.             return map;
  73.         }
  74.  
  75.         private static int GetMaxLengthOfLine(string[] lines)
  76.         {
  77.             int maxLength = 0;
  78.  
  79.             foreach (string line in lines)
  80.             {
  81.                 if (line.Length > maxLength)
  82.                     maxLength = line.Length;
  83.             }
  84.  
  85.             return maxLength;
  86.         }
  87.  
  88.         private static void HandleInput(ConsoleKeyInfo pressedKey, ref int playerPositionX, ref int playerPositionY, char[,] map, ref bool isRunning)
  89.         {
  90.             int directionX = 0;
  91.             int directionY = 0;
  92.  
  93.             switch (pressedKey.Key)
  94.             {
  95.                 case ConsoleKey.UpArrow:
  96.                     directionY--;
  97.                     break;
  98.  
  99.                 case ConsoleKey.DownArrow:
  100.                     directionY++;
  101.                     break;
  102.  
  103.                 case ConsoleKey.LeftArrow:
  104.                     directionX--;
  105.                     break;
  106.  
  107.                 case ConsoleKey.RightArrow:
  108.                     directionX++;
  109.                     break;
  110.  
  111.                 case ConsoleKey.Escape:
  112.                     isRunning = false;
  113.                     break;
  114.             }
  115.  
  116.             TryMove(ref playerPositionX, ref playerPositionY, directionX, directionY, map);
  117.         }
  118.  
  119.         private static int CollectBonus(int score, int positionX, int positionY, char[,] map)
  120.         {
  121.             if (map[positionX, positionY] == CellBonus)
  122.             {
  123.                 map[positionX, positionY] = CellEmpty;
  124.                 score++;
  125.             }
  126.             return score;
  127.         }
  128.  
  129.         private static void TryMove(ref int playerPositionX, ref int playerPositionY, int directionX, int directionY, char[,] map)
  130.         {
  131.             if (map[playerPositionX + directionX, playerPositionY + directionY] != CellWall)
  132.             {
  133.                 playerPositionX += directionX;
  134.                 playerPositionY += directionY;
  135.             }
  136.         }
  137.  
  138.         private static void DrawPlayer(int playerPositionX, int playerPositionY)
  139.         {
  140.             Console.SetCursorPosition(playerPositionX, playerPositionY);
  141.             Console.Write(CellPlayer);
  142.         }
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement