Advertisement
Guest User

Untitled

a guest
Feb 29th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace CSLight
  9. {
  10.     class Program
  11.     {
  12.         static void Main()
  13.         {
  14.             Console.CursorVisible = false;
  15.             int userPositionX = 2;
  16.             int userPositionY = 2;
  17.  
  18.             char[,] map =
  19.             {
  20.                 {'#','#','#','#','#','#','#','#','#','#','#','#' },
  21.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
  22.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
  23.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
  24.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
  25.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
  26.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
  27.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
  28.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
  29.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
  30.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
  31.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
  32.                 {'#','#','#','#','#','#','#','#','#','#','#','#' }
  33.             };
  34.  
  35.             DrawMap(map);
  36.  
  37.             while (true)
  38.             {
  39.                 ConsoleKeyInfo consoleKeyInfo = Console.ReadKey();
  40.  
  41.                 switch (consoleKeyInfo.Key)
  42.                 {
  43.                     case ConsoleKey.UpArrow:
  44.                         if (!CheckWall(map, userPositionX, userPositionY - 1))
  45.                         {
  46.                             ErasePlayer(userPositionX, userPositionY);
  47.                             Move(ref userPositionX, ref userPositionY, 0, -1);
  48.                             DrawPlayer(userPositionX, userPositionY, '$');
  49.                         }
  50.                         break;
  51.                     case ConsoleKey.DownArrow:
  52.                         if (!CheckWall(map, userPositionX, userPositionY + 1))
  53.                         {
  54.                             ErasePlayer(userPositionX, userPositionY);
  55.                             Move(ref userPositionX, ref userPositionY, 0, 1);
  56.                             DrawPlayer(userPositionX, userPositionY, '$');
  57.                         }
  58.                         break;
  59.                     case ConsoleKey.LeftArrow:
  60.                         if (!CheckWall(map, userPositionX - 1, userPositionY))
  61.                         {
  62.                             ErasePlayer(userPositionX, userPositionY);
  63.                             Move(ref userPositionX, ref userPositionY, -1, 0);
  64.                             DrawPlayer(userPositionX, userPositionY, '$');
  65.                         }
  66.                         break;
  67.                     case ConsoleKey.RightArrow:
  68.                         if (!CheckWall(map, userPositionX + 1, userPositionY))
  69.                         {
  70.                             ErasePlayer(userPositionX, userPositionY);
  71.                             Move(ref userPositionX, ref userPositionY, 1, 0);
  72.                             DrawPlayer(userPositionX, userPositionY, '$');
  73.                         }
  74.                         break;
  75.                 }
  76.             }
  77.         }
  78.  
  79.         static bool CheckWall(char[,] map, int coordinateX, int coordinateY)
  80.         {
  81.             if (map[coordinateY, coordinateX] == '#')
  82.             {
  83.                 return true;
  84.             }
  85.             else
  86.             {
  87.                 return false;
  88.             }
  89.         }
  90.  
  91.         static void ErasePlayer(int positionX, int positionY)
  92.         {
  93.             Console.SetCursorPosition(positionX, positionY);
  94.             Console.Write(" ");
  95.         }
  96.  
  97.         static void DrawPlayer(int positionX, int positionY, char symbol)
  98.         {
  99.             Console.SetCursorPosition(positionX, positionY);
  100.             Console.Write(symbol);
  101.         }
  102.  
  103.         static void Move(ref int positionX, ref int positionY, int directionX, int directionY)
  104.         {
  105.             positionX += directionX;
  106.             positionY += directionY;
  107.         }
  108.  
  109.         static void DrawMap(char[,] map)
  110.         {
  111.             for (int i = 0; i < map.GetLength(0); i++)
  112.             {
  113.                 for (int j = 0; j < map.GetLength(1); j++)
  114.                 {
  115.                     Console.Write(map[i, j]);
  116.                 }
  117.                 Console.WriteLine();
  118.             }
  119.         }
  120.  
  121.         static int ReadInt()
  122.         {
  123.             int parse;
  124.  
  125.             while (true)
  126.             {
  127.                 string userInput = Console.ReadLine();
  128.  
  129.                 if (int.TryParse(userInput, out parse))
  130.                 {
  131.                     return parse;
  132.                 }
  133.             }
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement