Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace FunctionsHW
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Console.CursorVisible = false;
- char[,] map = {
- { '╔', '═', '╦', '═', '═', '═', '═', '═', '═', '╗' },
- { '║', ' ', '║', '*', '*', '*', '*', '*', '*', '║' },
- { '║', '*', '║', '*', '║', '*', '╔', '═', '═', '╣' },
- { '║', '*', '*', '*', '║', '*', '║', '*', '*', '║' },
- { '║', '*', '║', '*', '║', '*', '║', '*', '╔', '╣' },
- { '║', '*', '║', '*', '║', '*', '║', '*', '╚', '╣' },
- { '║', '*', '║', '*', '║', '*', '║', '*', '*', '║' },
- { '║', '*', '║', '*', '║', '*', '*', '*', '═', '╣' },
- { '║', '*', '║', '*', '║', '*', '║', '*', '*', '║' },
- { '╚', '═', '╩', '═', '╩', '═', '╩', '═', '═', '╝' }
- };
- char player = '@';
- char star = '*';
- char road = ' ';
- int playerPosotionX = 1;
- int playerPositionY = 1;
- int score = 0;
- ConsoleKeyInfo pressedKey;
- while (true)
- {
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Blue;
- DrawMap(map);
- Console.ForegroundColor = ConsoleColor.Green;
- Console.SetCursorPosition(playerPosotionX, playerPositionY);
- Console.Write(player);
- Console.ForegroundColor = ConsoleColor.Red;
- Console.SetCursorPosition(0, 11);
- Console.Write("Очки: " + score);
- pressedKey = Console.ReadKey();
- HandleInput(pressedKey, star, road, map, ref playerPosotionX, ref playerPositionY, ref score);
- }
- }
- static void DrawMap(char[,] map)
- {
- for (int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- Console.Write(map[i, j]);
- }
- Console.WriteLine();
- }
- }
- static void HandleInput(ConsoleKeyInfo pressedKey, char star, char road, char[,] map, ref int playerX, ref int playerY, ref int score)
- {
- int[] direction = GetDirection(pressedKey);
- int nextPlayerPositionX = playerX + direction[0];
- int nextPlayerPositionY = playerY + direction[1];
- if (map[nextPlayerPositionY, nextPlayerPositionX] == star)
- {
- score += 1;
- map[nextPlayerPositionY, nextPlayerPositionX] = road;
- }
- if (map[nextPlayerPositionY, nextPlayerPositionX] == road)
- {
- playerX = nextPlayerPositionX;
- playerY = nextPlayerPositionY;
- }
- }
- static int[] GetDirection(ConsoleKeyInfo pressedKey)
- {
- ConsoleKeyInfo upArrow = new ConsoleKeyInfo('↑', ConsoleKey.UpArrow, false, false, false);
- ConsoleKeyInfo downArrow = new ConsoleKeyInfo('↓', ConsoleKey.DownArrow, false, false, false);
- ConsoleKeyInfo leftArrow = new ConsoleKeyInfo('←', ConsoleKey.LeftArrow, false, false, false);
- ConsoleKeyInfo rightArrow = new ConsoleKeyInfo('→', ConsoleKey.RightArrow, false, false, false);
- int[] direction = { 0, 0 };
- if (pressedKey.Key == upArrow.Key)
- direction[1] = -1;
- else if (pressedKey.Key == downArrow.Key)
- direction[1] = 1;
- else if (pressedKey.Key == leftArrow.Key)
- direction[0] = -1;
- else if (pressedKey.Key == rightArrow.Key)
- direction[0] = 1;
- return direction;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement