Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace C_Sharp_Junior
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Найди выход.Цель игры: Попасть игроком на клетку обозначеной знаком << X >>");
- Console.ReadLine();
- Console.Clear();
- bool isEndGame = false;
- Console.CursorVisible = false;
- int playerX = 1, playerY = 1;
- int playerDX = 1, playerDY = 1;
- char[,] map = {{'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
- {'#',' ',' ',' ',' ',' ','#',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ','#','#','#',' ','#',' ',' ','#','#','#','#','#','#',' ','#'},
- {'#',' ',' ',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#','#','#',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ','#'},
- {'#',' ','#',' ','#','#','#',' ',' ','#','#',' ','#','#','#',' ','#'},
- {'#',' ','#',' ',' ',' ',' ',' ',' ','#',' ',' ','#','X','#',' ','#'},
- {'#',' ',' ',' ','#','#','#',' ',' ','#',' ',' ','#',' ',' ',' ','#'},
- {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'}};
- DrawMap(map);
- while (isEndGame == false)
- {
- DrawPlayer(playerX, playerY);
- GetDirection(ref playerDX, ref playerDY);
- if (CheckCollision(map, playerX, playerY, playerDX, playerDY, ref isEndGame))
- {
- Move(ref playerX, ref playerY, playerDX, playerDY);
- }
- }
- Console.Clear();
- Console.WriteLine("Поздравляю. Вы выиграли.");
- Console.ReadLine();
- }
- 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 bool CheckCollision(char[,] map, int playerX, int playerY, int playerDX, int playerDY, ref bool isEndGame)
- {
- if (map[playerX + playerDX, playerY + playerDY] == 'X')
- {
- isEndGame = true;
- }
- return map[playerX + playerDX, playerY + playerDY] != '#';
- }
- static void Move(ref int playerX, ref int playerY, int playerDX, int playerDY)
- {
- Console.SetCursorPosition(playerY, playerX);
- Console.Write(" ");
- playerX += playerDX;
- playerY += playerDY;
- Console.SetCursorPosition(playerY, playerX);
- Console.Write("@");
- }
- static void GetDirection(ref int playerDX, ref int playerDY)
- {
- bool directionDefinitely = false;
- while (directionDefinitely == false)
- {
- if (Console.KeyAvailable)
- {
- ConsoleKeyInfo key = Console.ReadKey(true);
- switch (key.Key)
- {
- case ConsoleKey.UpArrow:
- playerDX = -1; playerDY = 0;
- break;
- case ConsoleKey.DownArrow:
- playerDX = 1; playerDY = 0;
- break;
- case ConsoleKey.LeftArrow:
- playerDX = 0; playerDY = -1;
- break;
- case ConsoleKey.RightArrow:
- playerDX = 0; playerDY = 1;
- break;
- }
- directionDefinitely = true;
- }
- }
- }
- static void DrawPlayer(int playerX, int playerY)
- {
- Console.SetCursorPosition(playerY, playerX);
- Console.Write("@");
- Console.ReadKey(true);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement