Advertisement
SPavelA

BraveNewWorld

Sep 11th, 2024 (edited)
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.16 KB | Gaming | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace FunctionsTask5BraveNewWorld
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             const ConsoleKey KeyExit = ConsoleKey.Q;
  11.             const char SymbolPlayer = '@';
  12.            
  13.             char[,] map = ReadMap("map.txt");
  14.             int scorePosition = map.GetLength(0);
  15.             int playerPositionX = 1;
  16.             int playerPositionY = 1;
  17.             int score = 0;
  18.             ConsoleKeyInfo pressedKey;
  19.             bool isRunned = true;
  20.  
  21.             Console.CursorVisible = false;
  22.  
  23.             while (isRunned)
  24.             {
  25.                 Console.Clear();
  26.                 Console.ForegroundColor = ConsoleColor.Blue;
  27.                 DrawMap(map);
  28.                 DrawPlayer(playerPositionX, playerPositionY, SymbolPlayer);
  29.                 DrawInfoPanel(score, scorePosition, KeyExit);
  30.                 pressedKey = Console.ReadKey();
  31.                 HandleInput(pressedKey, ref playerPositionX, ref playerPositionY, map, ref score);
  32.  
  33.                 if (pressedKey.Key == KeyExit)
  34.                 {
  35.                     isRunned = false;
  36.                 }
  37.             }
  38.         }
  39.  
  40.         private static char[,] ReadMap(string path)
  41.         {
  42.             string[] file = File.ReadAllLines("map.txt");
  43.             char[,] map = new char[GetMaxLengthOfLine(file), file.Length];
  44.  
  45.             for (int x = 0; x < map.GetLength(0); x++)
  46.             {
  47.                 for (int y = 0; y < map.GetLength(1); y++)
  48.                 {
  49.                     map[x, y] = file[y][x];
  50.                 }
  51.             }
  52.  
  53.             return map;
  54.         }
  55.  
  56.         private static void DrawMap(char[,] map)
  57.         {
  58.             for (int y = 0; y < map.GetLength(1); y++)
  59.             {
  60.                 for (int x = 0; x < map.GetLength(0); x++)
  61.                 {
  62.                     Console.Write(map[x, y]);
  63.                 }
  64.  
  65.                 Console.Write("\n");
  66.             }
  67.         }
  68.  
  69.         private static void DrawPlayer(int playerPositionX, int playerPositionY,char symbolPlayer)
  70.         {
  71.             Console.ForegroundColor = ConsoleColor.Yellow;
  72.             Console.SetCursorPosition(playerPositionX, playerPositionY);
  73.             Console.Write(symbolPlayer);
  74.         }
  75.  
  76.         private static void DrawInfoPanel(int score, int scorePosition, ConsoleKey keyExit)
  77.         {
  78.             Console.SetCursorPosition(scorePosition, 0);
  79.             Console.ForegroundColor = ConsoleColor.Red;
  80.             Console.Write($"Score: {score}");
  81.             Console.SetCursorPosition(scorePosition, 1);
  82.             Console.Write($"Press {keyExit} for exit");
  83.         }
  84.  
  85.         private static void HandleInput(ConsoleKeyInfo pressedKey, ref int playerPositionX, ref int playerPositionY, char[,] map, ref int score)
  86.         {
  87.             const char SymbolWall = '#';
  88.             const char SymbolSpace = ' ';
  89.             const char SymbolCherry = '.';
  90.             const char SymbolCake = '*';
  91.             const char SymbolBomb = 'x';
  92.             const int ScoreCherry = 1;
  93.             const int ScoreCake = 5;
  94.             const int ScoreBomb = -20;
  95.  
  96.             int[] direction = GetDirection(pressedKey);
  97.             int nextPlayerPositionX = playerPositionX + direction[0];
  98.             int nextPlayerPositionY = playerPositionY + direction[1];
  99.             char nextCell = map[nextPlayerPositionX, nextPlayerPositionY];
  100.  
  101.             if (nextCell != SymbolWall)
  102.             {
  103.                 if(nextCell == SymbolCherry)
  104.                 {
  105.                     score += ScoreCherry;
  106.                 }
  107.                 else if (nextCell == SymbolCake)
  108.                 {
  109.                     score += ScoreCake;
  110.                 }
  111.                 else if (nextCell == SymbolBomb)
  112.                 {
  113.                     score += ScoreBomb;
  114.                 }
  115.  
  116.                 playerPositionX = nextPlayerPositionX;
  117.                 playerPositionY = nextPlayerPositionY;
  118.                 map[nextPlayerPositionX, nextPlayerPositionY] = SymbolSpace;
  119.             }
  120.         }
  121.  
  122.         private static int[] GetDirection(ConsoleKeyInfo pressedKey)
  123.         {
  124.             const ConsoleKey KeyUp = ConsoleKey.UpArrow;
  125.             const ConsoleKey KeyDown = ConsoleKey.DownArrow;
  126.             const ConsoleKey KeyLeft = ConsoleKey.LeftArrow;
  127.             const ConsoleKey KeyRight = ConsoleKey.RightArrow;
  128.  
  129.             int[] direction = { 0, 0 };
  130.  
  131.             if (pressedKey.Key == KeyUp)
  132.                 direction[1] = -1;
  133.             else if (pressedKey.Key == KeyDown)
  134.                 direction[1] = 1;
  135.             else if (pressedKey.Key == KeyLeft)
  136.                 direction[0] = -1;
  137.             else if (pressedKey.Key == KeyRight)
  138.                 direction[0] = 1;
  139.  
  140.             return direction;
  141.         }
  142.  
  143.         private static int GetMaxLengthOfLine(string[] lines)
  144.         {
  145.             int maxLength = lines[0].Length;
  146.  
  147.             foreach (var line in lines)
  148.             {
  149.                 if (line.Length > maxLength)
  150.                 {
  151.                     maxLength = line.Length;
  152.                 }
  153.             }
  154.  
  155.             return maxLength;
  156.         }
  157.     }
  158. }
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement