Advertisement
MaoChessy

Task 22

Oct 30th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2.  
  3. namespace C_sharp_Light
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             char[,] map = {
  10.             {'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W'},
  11.             {'W', ' ', ' ', ' ', 'W', ' ', ' ', 'W'},
  12.             {'W', ' ', ' ', ' ', 'W', ' ', ' ', 'W'},
  13.             {'W', ' ', ' ', ' ', 'W', ' ', 'W', 'W'},
  14.             {'W', ' ', ' ', ' ', ' ', ' ', ' ', 'W'},
  15.             {'W', ' ', 'W', 'W', 'W', ' ', ' ', 'W'},
  16.             {'W', ' ', ' ', ' ', 'W', ' ', ' ', 'W'},
  17.             {'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W'}};
  18.             int playerX = 1, playerY = 1;
  19.             char playerChar = '@';
  20.             bool isPlaying = true;
  21.             Console.CursorVisible = false;
  22.  
  23.             DrawMap(map);
  24.             Move(ref playerX, ref playerY, 0, 0, playerChar, map);
  25.  
  26.             while (isPlaying)
  27.             {
  28.                 Console.SetCursorPosition(20, 20);
  29.                 ConsoleKeyInfo consoleKeyInfo = Console.ReadKey();
  30.                 switch (consoleKeyInfo.Key)
  31.                 {
  32.                     case ConsoleKey.UpArrow:
  33.                         Move(ref playerX, ref playerY, -1, 0, playerChar, map);
  34.                         break;
  35.                     case ConsoleKey.DownArrow:
  36.                         Move(ref playerX, ref playerY, 1, 0, playerChar, map);
  37.                         break;
  38.                     case ConsoleKey.RightArrow:
  39.                         Move(ref playerX, ref playerY, 0, 1, playerChar, map);
  40.                         break;
  41.                     case ConsoleKey.LeftArrow:
  42.                         Move(ref playerX, ref playerY, 0, -1, playerChar, map);
  43.                         break;
  44.                 }
  45.                
  46.             }
  47.         }
  48.         public static void Move(ref int x, ref int y, int dX, int dY, char playerChar, char[,] map)
  49.         {
  50.             if (map[x + dX, y + dY] != 'W')
  51.             {
  52.                 Console.ForegroundColor = ConsoleColor.White;
  53.                 Console.SetCursorPosition(y, x);
  54.                 Console.Write(' ');
  55.                 x += dX;
  56.                 y += dY;
  57.                 Console.ForegroundColor = ConsoleColor.DarkGreen;
  58.                 Console.SetCursorPosition(y, x);
  59.                 Console.Write(playerChar);
  60.             }
  61.         }
  62.         public static void DrawMap(char[,] map)
  63.         {
  64.             Console.SetCursorPosition(0, 0);
  65.             for (int x = 0; x < map.GetLength(0); x++)
  66.             {
  67.                 for (int y = 0; y < map.GetLength(1); y++)
  68.                 {
  69.                     Console.ForegroundColor = ConsoleColor.Yellow;
  70.                     Console.Write(map[x, y]);
  71.                 }
  72.                 Console.WriteLine();
  73.             }
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement