Advertisement
JohnJuly

Homework31

Feb 1st, 2024 (edited)
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.25 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.  
  7. namespace _31__
  8. {
  9.     internal class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.CursorVisible = false;
  14.  
  15.             char symbolPlayer = '@';
  16.             char symbolWall = '#';
  17.             char[,] map =
  18.             {
  19.                 {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
  20.                 {'#',' ',' ','#',' ',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  21.                 {'#',' ',' ','#',' ',' ','#',' ','#',' ',' ',' ','#','#','#','#','#','#','#',' ','#'},
  22.                 {'#',' ',' ','#',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  23.                 {'#',' ',' ','#',' ',' ',' ',' ','#',' ','#',' ',' ',' ',' ','#','#','#','#','#','#'},
  24.                 {'#',' ',' ','#',' ','#',' ',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  25.                 {'#',' ',' ','#',' ','#',' ',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  26.                 {'#',' ',' ',' ',' ','#',' ',' ','#',' ','#','#','#','#','#','#','#','#',' ',' ','#'},
  27.                 {'#',' ',' ',' ',' ','#',' ',' ','#',' ','#',' ',' ',' ',' ','#',' ',' ',' ',' ','#'},
  28.                 {'#',' ',' ',' ',' ','#',' ',' ','#',' ','#',' ',' ',' ',' ','#',' ',' ',' ',' ','#'},
  29.                 {'#',' ',' ',' ',' ','#',' ',' ','#',' ','#',' ',' ','#',' ','#',' ',' ','#',' ','#'},
  30.                 {'#',' ',' ',' ',' ',' ',' ',' ','#',' ','#','#',' ','#',' ','#',' ',' ','#',' ','#'},
  31.                 {'#',' ',' ',' ',' ',' ','#',' ',' ',' ','#',' ',' ','#',' ','#',' ',' ','#',' ','#'},
  32.                 {'#',' ',' ','#',' ',' ','#',' ',' ',' ','#',' ',' ','#',' ',' ',' ',' ','#',' ','#'},
  33.                 {'#',' ',' ','#',' ',' ','#',' ',' ',' ','#',' ',' ','#',' ',' ',' ',' ','#',' ','#'},
  34.                 {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
  35.             };
  36.  
  37.             int positionUserX = 1;
  38.             int positionUserY = 9;
  39.            
  40.             bool isWork = true;
  41.  
  42.             while (isWork)
  43.             {
  44.                 int directionX = 0;
  45.                 int directionY = 0;
  46.  
  47.                 Console.SetCursorPosition(0, 0);
  48.  
  49.                 DrawMap(map);
  50.  
  51.                 DrawPlayer(ref positionUserY,ref positionUserX, symbolPlayer);
  52.  
  53.                 ChangeDirection(ref directionX, ref directionY, ref isWork);
  54.  
  55.                 Move(ref map, ref positionUserY, ref positionUserX, ref directionY, ref directionX, ref symbolWall);
  56.             }
  57.         }
  58.  
  59.         static void DrawMap(char[,] map)
  60.         {
  61.             for (int i = 0; i < map.GetLength(0); i++)
  62.             {
  63.                 for (int j = 0; j < map.GetLength(1); j++)
  64.                 {
  65.                     Console.Write(map[i, j]);
  66.                 }
  67.  
  68.                 Console.WriteLine();
  69.             }
  70.         }
  71.         static void DrawPlayer(ref int userY, ref int userX, char pacman)
  72.         {
  73.             Console.SetCursorPosition(userY, userX);
  74.             Console.WriteLine(pacman);
  75.         }
  76.  
  77.         static void ChangeDirection(ref int directionX, ref int directionY, ref bool isWork)
  78.         {
  79.             ConsoleKeyInfo charKey = Console.ReadKey();
  80.  
  81.             switch (charKey.Key)
  82.             {
  83.                 case ConsoleKey.UpArrow:
  84.                     directionX--;
  85.                     break;
  86.  
  87.                 case ConsoleKey.DownArrow:
  88.                     directionX++;
  89.                     break;
  90.  
  91.                 case ConsoleKey.LeftArrow:
  92.                     directionY--;
  93.                     break;
  94.  
  95.                 case ConsoleKey.RightArrow:
  96.                     directionY++;
  97.                     break;
  98.  
  99.                 case ConsoleKey.Escape:
  100.                     isWork = false;
  101.                     break;
  102.             }
  103.         }
  104.         static void Move(ref char[,] map, ref int userY, ref int userX, ref int directionY, ref int directionX, ref char symbol)
  105.         {
  106.             if (map[userX + directionX, userY + directionY] != symbol)
  107.             {
  108.                 userX += directionX;
  109.                 userY += directionY;
  110.             }
  111.         }
  112.     }
  113. }
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement