Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Les3
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.CursorVisible = false;
- Console.Title = "4.4 Brave new world";
- char[,] map = GetMap(30, 10);
- int userX = 3, userY = 3;
- while (true)
- {
- DrawMap(ref map);
- Console.SetCursorPosition(userY, userX);
- Console.Write('@');
- ConsoleKeyInfo charKey = Console.ReadKey(true);
- switch (charKey.Key)
- {
- case ConsoleKey.LeftArrow:
- if (map[userX, userY - 1] != '#')
- userY--;
- break;
- case ConsoleKey.RightArrow:
- if (map[userX, userY + 1] != '#')
- userY++;
- break;
- case ConsoleKey.UpArrow:
- if (map[userX - 1, userY] != '#')
- userX--;
- break;
- case ConsoleKey.DownArrow:
- if (map[userX + 1, userY] != '#')
- userX++;
- break;
- }
- }
- }
- static char[,] GetMap(int y, int x)
- {
- char[,] map = new char[x, y];
- for (int h = 1; h < map.GetLength(0); h++)
- {
- for (int w = 0; w < map.GetLength(1); w++)
- {
- map[h, w] = '.';
- }
- }
- for (int i = 0; i < map.GetLength(1); i++)
- {
- map[0, i] = '#';
- map[map.GetLength(0) - 1, i] = '#';
- }
- for (int i = 0; i < map.GetLength(0); i++)
- {
- map[i, 0] = '#';
- map[i, map.GetLength(1) - 1] = '#';
- }
- return map;
- }
- static void DrawMap(ref char[,] map)
- {
- Console.SetCursorPosition(0, 0);
- 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();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement