Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Brave_new_world
- {
- class Program
- {
- static void Main()
- {
- Console.CursorVisible = false;
- bool isPlaying = true;
- int countElement = 0, playerDX = 0, playerDY = 1;
- char[,] map = GenerateMap();
- DrawMap(ref map, out int playerX, out int playerY);
- while (isPlaying)
- {
- Console.SetCursorPosition(0, 20);
- Console.WriteLine($"Собрано {countElement}");
- if (Console.KeyAvailable)
- {
- ConsoleKeyInfo key = Console.ReadKey(true);
- ChangeDirection(key, ref playerDX, ref playerDY);
- }
- if (map[playerX + playerDX, playerY + playerDY] != '#')
- {
- Move(map, '@', ref playerX, ref playerY, playerDX, playerDY);
- CollectMoney(map, playerX, playerY, ref countElement);
- }
- System.Threading.Thread.Sleep(300);
- }
- }
- static void CollectMoney(char[,] map, int playerX, int playerY, ref int countElement)
- {
- if (map[playerX, playerY] == '$')
- {
- countElement++;
- map[playerX, playerY] = ' ';
- }
- }
- static char[,] GenerateMap()
- {
- int money = 5, wall = 20, player = 1;
- char[,] map = new char[15, 15];
- for (int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- if (i == 0 || i == map.GetLength(0) - 1 || j == 0 || j == map.GetLength(1) - 1)
- {
- map[i, j] = '#';
- }
- else
- {
- map[i, j] = ' ';
- }
- }
- }
- DrawElement(ref map, player, '@');
- DrawElement(ref map, money, '$');
- DrawElement(ref map, wall, '#');
- return map;
- }
- static void DrawMap(ref char[,] map, out int playerX, out int playerY)
- {
- playerX = 0;
- playerY = 0;
- for (int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- Console.Write(map[i, j]);
- if (map[i, j] == '@')
- {
- playerX = i;
- playerY = j;
- map[i, j] = ' ';
- }
- }
- Console.WriteLine();
- }
- }
- static void DrawElement(ref char[,] map, int element, char symbol)
- {
- Random random = new Random();
- int positionX, positionY;
- for (int i = 0; i <= element; i++)
- {
- while (element > 0)
- {
- positionX = random.Next(1, map.GetLength(0) - 1);
- positionY = random.Next(1, map.GetLength(1) - 1);
- if (map[positionX, positionY] == ' ')
- {
- map[positionX, positionY] = symbol;
- element--;
- }
- }
- }
- }
- static void ChangeDirection(ConsoleKeyInfo key, ref int DX, ref int DY)
- {
- switch (key.Key)
- {
- case ConsoleKey.UpArrow:
- DX = -1; DY = 0;
- break;
- case ConsoleKey.DownArrow:
- DX = 1; DY = 0;
- break;
- case ConsoleKey.LeftArrow:
- DX = 0; DY = -1;
- break;
- case ConsoleKey.RightArrow:
- DX = 0; DY = 1;
- break;
- }
- }
- static void Move(char[,] map, char symbol, ref int x, ref int y, int DX, int DY)
- {
- Console.SetCursorPosition(y, x);
- Console.Write(map[x, y]); ;
- x += DX;
- y += DY;
- Console.SetCursorPosition(y, x);
- Console.Write(symbol);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement