Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- namespace FunctionsTask5BraveNewWorld
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- const ConsoleKey KeyExit = ConsoleKey.Q;
- const char SymbolPlayer = '@';
- char[,] map = ReadMap("map.txt");
- int scorePosition = map.GetLength(0);
- int playerPositionX = 1;
- int playerPositionY = 1;
- int score = 0;
- ConsoleKeyInfo pressedKey;
- bool isRunned = true;
- Console.CursorVisible = false;
- while (isRunned)
- {
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Blue;
- DrawMap(map);
- DrawPlayer(playerPositionX, playerPositionY, SymbolPlayer);
- DrawInfoPanel(score, scorePosition, KeyExit);
- pressedKey = Console.ReadKey();
- HandleInput(pressedKey, ref playerPositionX, ref playerPositionY, map, ref score);
- if (pressedKey.Key == KeyExit)
- {
- isRunned = false;
- }
- }
- }
- private static char[,] ReadMap(string path)
- {
- string[] file = File.ReadAllLines("map.txt");
- char[,] map = new char[GetMaxLengthOfLine(file), file.Length];
- for (int x = 0; x < map.GetLength(0); x++)
- {
- for (int y = 0; y < map.GetLength(1); y++)
- {
- map[x, y] = file[y][x];
- }
- }
- return map;
- }
- private static void DrawMap(char[,] map)
- {
- for (int y = 0; y < map.GetLength(1); y++)
- {
- for (int x = 0; x < map.GetLength(0); x++)
- {
- Console.Write(map[x, y]);
- }
- Console.Write("\n");
- }
- }
- private static void DrawPlayer(int playerPositionX, int playerPositionY,char symbolPlayer)
- {
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.SetCursorPosition(playerPositionX, playerPositionY);
- Console.Write(symbolPlayer);
- }
- private static void DrawInfoPanel(int score, int scorePosition, ConsoleKey keyExit)
- {
- Console.SetCursorPosition(scorePosition, 0);
- Console.ForegroundColor = ConsoleColor.Red;
- Console.Write($"Score: {score}");
- Console.SetCursorPosition(scorePosition, 1);
- Console.Write($"Press {keyExit} for exit");
- }
- private static void HandleInput(ConsoleKeyInfo pressedKey, ref int playerPositionX, ref int playerPositionY, char[,] map, ref int score)
- {
- const char SymbolWall = '#';
- const char SymbolSpace = ' ';
- const char SymbolCherry = '.';
- const char SymbolCake = '*';
- const char SymbolBomb = 'x';
- const int ScoreCherry = 1;
- const int ScoreCake = 5;
- const int ScoreBomb = -20;
- int[] direction = GetDirection(pressedKey);
- int nextPlayerPositionX = playerPositionX + direction[0];
- int nextPlayerPositionY = playerPositionY + direction[1];
- char nextCell = map[nextPlayerPositionX, nextPlayerPositionY];
- if (nextCell != SymbolWall)
- {
- if(nextCell == SymbolCherry)
- {
- score += ScoreCherry;
- }
- else if (nextCell == SymbolCake)
- {
- score += ScoreCake;
- }
- else if (nextCell == SymbolBomb)
- {
- score += ScoreBomb;
- }
- playerPositionX = nextPlayerPositionX;
- playerPositionY = nextPlayerPositionY;
- map[nextPlayerPositionX, nextPlayerPositionY] = SymbolSpace;
- }
- }
- private static int[] GetDirection(ConsoleKeyInfo pressedKey)
- {
- const ConsoleKey KeyUp = ConsoleKey.UpArrow;
- const ConsoleKey KeyDown = ConsoleKey.DownArrow;
- const ConsoleKey KeyLeft = ConsoleKey.LeftArrow;
- const ConsoleKey KeyRight = ConsoleKey.RightArrow;
- int[] direction = { 0, 0 };
- if (pressedKey.Key == KeyUp)
- direction[1] = -1;
- else if (pressedKey.Key == KeyDown)
- direction[1] = 1;
- else if (pressedKey.Key == KeyLeft)
- direction[0] = -1;
- else if (pressedKey.Key == KeyRight)
- direction[0] = 1;
- return direction;
- }
- private static int GetMaxLengthOfLine(string[] lines)
- {
- int maxLength = lines[0].Length;
- foreach (var line in lines)
- {
- if (line.Length > maxLength)
- {
- maxLength = line.Length;
- }
- }
- return maxLength;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement