Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace FifthProject
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.CursorVisible = false;
- int userY = 1;
- int userX = 1;
- int mapSizeX = 0;
- int mapSizeY = 0;
- char[,] map = new char[0, 0];
- SettingMap(ref mapSizeX, ref mapSizeY);
- GenerateMap(out map, mapSizeX, mapSizeY);
- while (true)
- {
- Console.Clear();
- DrawMap(map);
- PlayerController(ref userX, ref userY, map);
- }
- }
- static int UserInputInt()
- {
- int output;
- bool enterIsCorrect = false;
- do
- {
- string input = Console.ReadLine();
- enterIsCorrect = int.TryParse(input, out output);
- if (!enterIsCorrect)
- {
- Console.WriteLine("Error enter. Try again");
- }
- } while (!enterIsCorrect);
- return output;
- }
- static void SettingMap(ref int x, ref int y)
- {
- Console.WriteLine("Введите высоту карты");
- x = UserInputInt();
- Console.WriteLine("Введите ширину карты");
- y = UserInputInt();
- }
- static void GenerateMap(out char[,] arr, int x, int y)
- {
- char[,] tempMap = new char[x, y];
- for (int i = 0; i < tempMap.GetLength(0); i++)
- {
- for (int j = 0; j < tempMap.GetLength(1); j++)
- {
- if (i == 0 || i == tempMap.GetLength(0) - 1) tempMap[i,j] = '#';
- else if (j == 0 || j == tempMap.GetLength(1) - 1) tempMap[i,j] = '#';
- else tempMap[i,j] = ' ';
- }
- Console.WriteLine();
- }
- arr = tempMap;
- }
- static void DrawMap(char[,] map)
- {
- for (int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- Console.Write(map[i,j]);
- }
- Console.WriteLine();
- }
- }
- static void PlayerController(ref int y, ref int x,char[,] map)
- {
- Console.SetCursorPosition(y, x);
- Console.Write('@');
- ConsoleKeyInfo consoleKey = Console.ReadKey();
- switch (consoleKey.Key)
- {
- case ConsoleKey.LeftArrow:
- if (map[x, y - 1] != '#')
- {
- y--;
- }
- break;
- case ConsoleKey.RightArrow:
- if (map[x, y + 1] != '#')
- {
- y++;
- }
- break;
- case ConsoleKey.UpArrow:
- if (map[x - 1, y] != '#')
- {
- x--;
- }
- break;
- case ConsoleKey.DownArrow:
- if (map[x + 1, y] != '#')
- {
- x++;
- }
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement