Advertisement
Fle2x

Untitled

May 4th, 2020
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.34 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Brave_new_world
  4. {
  5.     class Program
  6.     {
  7.         static void Main()
  8.         {
  9.             Console.CursorVisible = false;
  10.             bool isPlaying = true;
  11.             int countElement = 0, playerDX = 0, playerDY = 1;
  12.             char[,] map = GenerateMap();
  13.  
  14.             DrawMap(ref map, out int playerX, out int playerY);
  15.  
  16.             while (isPlaying)
  17.             {
  18.                 Console.SetCursorPosition(0, 20);
  19.                 Console.WriteLine($"Собрано {countElement}");
  20.  
  21.                 if (Console.KeyAvailable)
  22.                 {
  23.                     ConsoleKeyInfo key = Console.ReadKey(true);
  24.                     ChangeDirection(key, ref playerDX, ref playerDY);
  25.                 }
  26.  
  27.                 if (map[playerX + playerDX, playerY + playerDY] != '#')
  28.                 {
  29.                     Move(map, '@', ref playerX, ref playerY, playerDX, playerDY);
  30.                     CollectMoney(map, playerX, playerY, ref countElement);
  31.                 }
  32.                 System.Threading.Thread.Sleep(300);
  33.             }
  34.         }
  35.  
  36.         static void CollectMoney(char[,] map, int playerX, int playerY, ref int countElement)
  37.         {
  38.             if (map[playerX, playerY] == '$')
  39.             {
  40.                 countElement++;
  41.                 map[playerX, playerY] = ' ';
  42.             }
  43.         }
  44.  
  45.         static char[,] GenerateMap()
  46.         {
  47.             int money = 5, wall = 20, player = 1;
  48.             char[,] map = new char[15, 15];
  49.  
  50.             for (int i = 0; i < map.GetLength(0); i++)
  51.             {
  52.                 for (int j = 0; j < map.GetLength(1); j++)
  53.                 {
  54.                     if (i == 0 || i == map.GetLength(0) - 1 || j == 0 || j == map.GetLength(1) - 1)
  55.                     {
  56.                         map[i, j] = '#';
  57.                     }
  58.                     else
  59.                     {
  60.                         map[i, j] = ' ';
  61.                     }
  62.                 }
  63.             }
  64.             DrawElement(ref map, player, '@');
  65.             DrawElement(ref map, money, '$');
  66.             DrawElement(ref map, wall, '#');
  67.             return map;
  68.         }
  69.  
  70.         static void DrawMap(ref char[,] map, out int playerX, out int playerY)
  71.         {
  72.             playerX = 0;
  73.             playerY = 0;
  74.  
  75.             for (int i = 0; i < map.GetLength(0); i++)
  76.             {
  77.                 for (int j = 0; j < map.GetLength(1); j++)
  78.                 {
  79.                     Console.Write(map[i, j]);
  80.                     if (map[i, j] == '@')
  81.                     {
  82.                         playerX = i;
  83.                         playerY = j;
  84.                         map[i, j] = ' ';
  85.                     }
  86.                 }
  87.                 Console.WriteLine();
  88.             }
  89.         }
  90.  
  91.         static void DrawElement(ref char[,] map, int element, char symbol)
  92.         {
  93.             Random random = new Random();
  94.             int positionX, positionY;
  95.             for (int i = 0; i <= element; i++)
  96.             {
  97.                 while (element > 0)
  98.                 {
  99.                     positionX = random.Next(1, map.GetLength(0) - 1);
  100.                     positionY = random.Next(1, map.GetLength(1) - 1);
  101.  
  102.                     if (map[positionX, positionY] == ' ')
  103.                     {
  104.                         map[positionX, positionY] = symbol;
  105.                         element--;
  106.                     }
  107.                 }
  108.             }
  109.         }
  110.  
  111.         static void ChangeDirection(ConsoleKeyInfo key, ref int DX, ref int DY)
  112.         {
  113.             switch (key.Key)
  114.             {
  115.                 case ConsoleKey.UpArrow:
  116.                     DX = -1; DY = 0;
  117.                     break;
  118.                 case ConsoleKey.DownArrow:
  119.                     DX = 1; DY = 0;
  120.                     break;
  121.                 case ConsoleKey.LeftArrow:
  122.                     DX = 0; DY = -1;
  123.                     break;
  124.                 case ConsoleKey.RightArrow:
  125.                     DX = 0; DY = 1;
  126.                     break;
  127.             }
  128.         }
  129.  
  130.         static void Move(char[,] map, char symbol, ref int x, ref int y, int DX, int DY)
  131.         {
  132.             Console.SetCursorPosition(y, x);
  133.             Console.Write(map[x, y]); ;
  134.  
  135.             x += DX;
  136.             y += DY;
  137.  
  138.             Console.SetCursorPosition(y, x);
  139.             Console.Write(symbol);
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement