Advertisement
IGRODELOFF

Brave new world

Apr 6th, 2025
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 KB | None | 0 0
  1. using System;
  2.  
  3. class Program
  4. {
  5.     static char[,] map = new char[,]
  6.     {
  7.         {'#','#','#','#','#','#','#','#','#','#'},
  8.         {'#',' ',' ','#',' ',' ',' ',' ',' ','#'},
  9.         {'#',' ',' ','#',' ','#','#','#',' ','#'},
  10.         {'#',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  11.         {'#','#','#','#',' ','#',' ','#',' ','#'},
  12.         {'#',' ',' ',' ',' ','#',' ','#',' ','#'},
  13.         {'#',' ','#','#','#','#',' ',' ',' ','#'},
  14.         {'#',' ',' ',' ',' ',' ',' ','#',' ','#'},
  15.         {'#',' ',' ','#',' ','#',' ','#',' ','#'},
  16.         {'#','#','#','#','#','#','#','#','#','#'},
  17.     };
  18.  
  19.     static void DrawMap(int playerX, int playerY)
  20.     {
  21.         Console.Clear();
  22.         for (int y = 0; y < map.GetLength(0); y++)
  23.         {
  24.             for (int x = 0; x < map.GetLength(1); x++)
  25.             {
  26.                 Console.Write(map[y, x]);
  27.             }
  28.             Console.WriteLine();
  29.         }
  30.         Console.SetCursorPosition(playerX, playerY);
  31.         Console.Write('@');
  32.         Console.SetCursorPosition(0, map.GetLength(0));
  33.     }
  34.  
  35.     static void TryMove(int deltaX, int deltaY, ref int x, ref int y)
  36.     {
  37.         int newX = x + deltaX;
  38.         int newY = y + deltaY;
  39.  
  40.         if (newX >= 0 && newX < map.GetLength(1) &&
  41.             newY >= 0 && newY < map.GetLength(0))
  42.         {
  43.             if (map[newY, newX] != '#')
  44.             {
  45.                 x = newX;
  46.                 y = newY;
  47.             }
  48.         }
  49.     }
  50.  
  51.     static void Main()
  52.     {
  53.         int playerX = 1;
  54.         int playerY = 1;
  55.         Console.CursorVisible = false;
  56.  
  57.         while (true)
  58.         {
  59.             DrawMap(playerX, playerY);
  60.             ConsoleKeyInfo keyInfo = Console.ReadKey(true);
  61.  
  62.             switch (keyInfo.Key)
  63.             {
  64.                 case ConsoleKey.UpArrow:
  65.                     TryMove(0, -1, ref playerX, ref playerY);
  66.                     break;
  67.                 case ConsoleKey.DownArrow:
  68.                     TryMove(0, 1, ref playerX, ref playerY);
  69.                     break;
  70.                 case ConsoleKey.LeftArrow:
  71.                     TryMove(-1, 0, ref playerX, ref playerY);
  72.                     break;
  73.                 case ConsoleKey.RightArrow:
  74.                     TryMove(1, 0, ref playerX, ref playerY);
  75.                     break;
  76.                 case ConsoleKey.Escape:
  77.                     return;
  78.             }
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement