Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- namespace homework
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.Clear();
- Console.CursorVisible = false;
- char[,] map =
- {
- {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ','#','#','#','#','#','#','#','#','#',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'}
- };
- bool isPlaying = true;
- int playerX = 3,playerY = 3;
- DrawMap(map);
- while(isPlaying)
- {
- if(Console.KeyAvailable)
- {
- ConsoleKeyInfo charKey = Console.ReadKey(true);
- Console.SetCursorPosition(playerY,playerX);
- Write(" ");
- switch(charKey.Key)
- {
- case ConsoleKey.UpArrow:
- if(map[playerX -1,playerY] != '#')
- {
- playerX--;
- }
- break;
- case ConsoleKey.DownArrow:
- if(map[playerX +1,playerY] != '#')
- {
- playerX++;
- }
- break;
- case ConsoleKey.LeftArrow:
- if(map[playerX,playerY - 1] != '#')
- {
- playerY--;
- }
- break;
- case ConsoleKey.RightArrow:
- if(map[playerX,playerY + 1] != '#')
- {
- playerY++;
- }
- break;
- }
- Console.SetCursorPosition(playerY,playerX);
- Write("@");
- }
- }
- }
- private static void DrawMap(char[,] map)
- {
- for(int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- Write($"{map[i,j]}");
- }
- Write("\n");
- }
- }
- private static void Write(string text, ConsoleColor color = ConsoleColor.Yellow)
- {
- Console.ForegroundColor = color;
- Console.Write(text);
- Console.ResetColor();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment