Advertisement
RamGaal

Homework 4 ex.4

May 23rd, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.93 KB | None | 0 0
  1. using System;
  2.  
  3. class MainClass
  4. {
  5.     public static void Main(string[] args)
  6.     {
  7.         int userY=3;
  8.         int userX=3;
  9.         Console.CursorVisible = false;
  10.         char[,] map =
  11.             { { '#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#' },
  12.               { '#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
  13.               { '#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ','#','#','#',' ',' ',' ',' ',' ','#' },
  14.               { '#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ','#',' ',' ',' ','#' },
  15.               { '#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ','#',' ',' ',' ','#' },
  16.               { '#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#','#',' ',' ','#' },
  17.               { '#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#','#','#','#',' ',' ',' ',' ','#',' ',' ','#' },
  18.               { '#',' ','#',' ',' ','#','#','#','#',' ',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ',' ',' ','#' },
  19.               { '#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ',' ',' ','#' },
  20.               { '#',' ',' ','#','#','#','#',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#','#','#',' ',' ',' ','#' },
  21.               { '#',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
  22.               { '#',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
  23.               { '#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#' }};
  24.         while(true)
  25.         {
  26.             Map(map);
  27.             Console.SetCursorPosition(userY, userX);
  28.             Console.Write('@');
  29.             ConsoleKeyInfo charKey = Console.ReadKey(true);
  30.            
  31.             switch (charKey.Key)
  32.             {
  33.                 case ConsoleKey.LeftArrow:
  34.                     if (map[userX, userY - 1] != '#')
  35.                         userY--;
  36.                     break;
  37.                 case ConsoleKey.RightArrow:
  38.                     if (map[userX, userY + 1] != '#')
  39.                         userY++;
  40.                     break;
  41.                 case ConsoleKey.UpArrow:
  42.                     if (map[userX - 1, userY] != '#')
  43.                         userX--;
  44.                     break;
  45.                 case ConsoleKey.DownArrow:
  46.                     if (map[userX + 1, userY] != '#')
  47.                         userX++;
  48.                     break;
  49.             }
  50.            
  51.         }
  52.        
  53.     }
  54.  
  55.     public static void Map(char[,] map)
  56.     {
  57.         Console.SetCursorPosition(0, 0);
  58.         for (int x = 0; x<map.GetLength(0);x++ )
  59.         {
  60.             for (int y = 0; y < map.GetLength(1); y++)
  61.             {
  62.                 Console.Write(map[x, y]);
  63.             }
  64.             Console.WriteLine();
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement