Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- namespace world
- {
- class Program
- {
- const char CellWall = '#';
- const char CellBonus = '*';
- const char CellPlayer = '@';
- const char CellEmpty = ' ';
- static void Main(string[] args)
- {
- char[,] map = LoadMap("map");
- Console.CursorVisible = false;
- int playerPositionX = 1;
- int playerPositionY = 1;
- int scorePositionY = map.GetLength(1);
- int score = 0;
- bool isRunning = true;
- while (isRunning)
- {
- DrawMap(map);
- DrawPlayer(playerPositionX, playerPositionY);
- DrawScore(score, scorePositionY);
- ConsoleKeyInfo pressedKey = Console.ReadKey();
- HandleInput(pressedKey, ref playerPositionX, ref playerPositionY, map, ref isRunning);
- score = CollectBonus(score, playerPositionX, playerPositionY, map);
- }
- }
- private static void DrawScore(int score, int scorePositionY)
- {
- Console.SetCursorPosition(0, scorePositionY);
- Console.WriteLine($"Score: {score}");
- }
- private static void DrawMap(char[,] map)
- {
- Console.SetCursorPosition(0, 0);
- for (int y = 0; y < map.GetLength(1); y++)
- {
- for (int x = 0; x < map.GetLength(0); x++)
- {
- Console.Write(map[x, y]);
- }
- Console.WriteLine();
- }
- }
- private static char[,] LoadMap(string path)
- {
- string[] file = File.ReadAllLines(path);
- 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++)
- {
- if (x < file[y].Length)
- map[x, y] = file[y][x];
- else
- map[x, y] = CellEmpty;
- }
- }
- return map;
- }
- private static int GetMaxLengthOfLine(string[] lines)
- {
- int maxLength = 0;
- foreach (string line in lines)
- {
- if (line.Length > maxLength)
- maxLength = line.Length;
- }
- return maxLength;
- }
- private static void HandleInput(ConsoleKeyInfo pressedKey, ref int playerPositionX, ref int playerPositionY, char[,] map, ref bool isRunning)
- {
- int directionX = 0;
- int directionY = 0;
- switch (pressedKey.Key)
- {
- case ConsoleKey.UpArrow:
- directionY--;
- break;
- case ConsoleKey.DownArrow:
- directionY++;
- break;
- case ConsoleKey.LeftArrow:
- directionX--;
- break;
- case ConsoleKey.RightArrow:
- directionX++;
- break;
- case ConsoleKey.Escape:
- isRunning = false;
- break;
- }
- TryMove(ref playerPositionX, ref playerPositionY, directionX, directionY, map);
- }
- private static int CollectBonus(int score, int positionX, int positionY, char[,] map)
- {
- if (map[positionX, positionY] == CellBonus)
- {
- map[positionX, positionY] = CellEmpty;
- score++;
- }
- return score;
- }
- private static void TryMove(ref int playerPositionX, ref int playerPositionY, int directionX, int directionY, char[,] map)
- {
- if (map[playerPositionX + directionX, playerPositionY + directionY] != CellWall)
- {
- playerPositionX += directionX;
- playerPositionY += directionY;
- }
- }
- private static void DrawPlayer(int playerPositionX, int playerPositionY)
- {
- Console.SetCursorPosition(playerPositionX, playerPositionY);
- Console.Write(CellPlayer);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement