Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- /*
- * Сделать игровую карту с помощью двухмерного массива.
- * Сделать метод рисования карты.
- * Помимо этого, дать пользователю возможность перемещаться по карте и взаимодействовать с элементами
- * (например пользователь не может пройти сквозь стену)
- * Все элементы являются обычными символами
- */
- namespace ConsoleApplication
- {
- class Program
- {
- static int playerPosX;
- static int playerPosY;
- static int teleportPosX;
- static int teleportPosY;
- static char player = '!';
- static char teleport = 'O';
- static int [,] mapSize = new int [20,10];
- static Random spawnPosition = new Random ();
- static bool start = false;
- static bool win = false;
- static void Main ( string [] args )
- {
- Console.CursorVisible = false;
- while ( !start && !win )
- {
- Menu1 ();
- DrawMap ();
- PlayerMovement ();
- TeleportSpawn ();
- Win ();
- }
- while ( start && !win )
- {
- Menu2 ();
- DrawMap ();
- PlayerMovement ();
- Win ();
- }
- if ( win )
- {
- Console.Clear ();
- Menu3 ();
- }
- }
- static void DrawMap ()
- {
- for ( int x = 0 ; x <= mapSize.GetLength ( 0 ) ; x++ )
- {
- for ( int y = 0 ; y <= mapSize.GetLength ( 1 ) ; y++ )
- {
- if ( x == 0 || x == 10 || x == mapSize.GetLength ( 0 ) )
- {
- Console.SetCursorPosition ( x, y );
- Console.WriteLine ( "I" );
- }
- else if ( y == 0 || y == 5 || y == mapSize.GetLength ( 1 ) )
- {
- Console.SetCursorPosition ( x, y );
- Console.WriteLine ( "-" );
- }
- }
- }
- }
- static void PlayerSpawn ()
- {
- bool availiable = false;
- int prewPosX;
- int prewPosY;
- prewPosX = playerPosX;
- prewPosY = playerPosY;
- do
- {
- prewPosX = playerPosX;
- prewPosY = playerPosY;
- playerPosX = spawnPosition.Next ( 1, 19 );
- playerPosY = spawnPosition.Next ( 1, 9 );
- }
- while ( availiable );
- if ( playerPosX != 10 & playerPosY != 5 )
- {
- availiable = true;
- DrawPlayer ();
- }
- }
- static void TeleportSpawn ()
- {
- bool availiable = false;
- do
- {
- teleportPosX = spawnPosition.Next ( 1, 19 );
- teleportPosY = spawnPosition.Next ( 1, 9 );
- }
- while ( availiable );
- if ( teleportPosX != 10 & teleportPosY != 5 )
- {
- availiable = true;
- DrawTeleport ();
- }
- }
- static void PlayerMovement ()
- {
- ConsoleKeyInfo key = Console.ReadKey ( true );
- switch ( key.Key )
- {
- case ConsoleKey.W:
- if ( playerPosY > 1 & playerPosY != 6 )
- {
- ClearPrewPlayer ();
- playerPosY -= 1;
- DrawPlayer ();
- }
- else
- DrawPlayer ();
- break;
- case ConsoleKey.S:
- if ( playerPosY < 9 & playerPosY != 4 )
- {
- ClearPrewPlayer ();
- playerPosY += 1;
- DrawPlayer ();
- }
- else
- DrawPlayer ();
- break;
- case ConsoleKey.A:
- if ( playerPosX > 1 & playerPosX != 11 )
- {
- ClearPrewPlayer ();
- playerPosX -= 1;
- DrawPlayer ();
- }
- else
- DrawPlayer ();
- break;
- case ConsoleKey.D:
- if ( playerPosX < 19 & playerPosX != 9 )
- {
- ClearPrewPlayer ();
- playerPosX += 1;
- DrawPlayer ();
- }
- else
- DrawPlayer ();
- break;
- case ConsoleKey.E:
- ClearPrewPlayer ();
- start = true;
- break;
- }
- }
- static void DrawPlayer ()
- {
- Console.SetCursorPosition ( playerPosX, playerPosY );
- Console.WriteLine ( player );
- }
- static void DrawTeleport ()
- {
- Console.SetCursorPosition ( teleportPosX, teleportPosY );
- Console.WriteLine ( teleport );
- }
- static void ClearPrewPlayer ()
- {
- Console.SetCursorPosition ( playerPosX, playerPosY );
- Console.WriteLine ( " " );
- PlayerSpawn ();
- }
- static void Win ()
- {
- if ( playerPosX == teleportPosX & playerPosY == teleportPosY )
- {
- win = true;
- }
- }
- static void Menu1 ()
- {
- Console.SetCursorPosition ( 25, 2 );
- Console.WriteLine ( "Для старта нажмите \"E\"".PadLeft ( 30 ) );
- }
- static void Menu2 ()
- {
- Console.SetCursorPosition ( 25, 2 );
- Console.WriteLine ( "Для передвижения используйте клавиши W,A,S,D".PadLeft ( 30 ) );
- Console.SetCursorPosition ( 25, 4 );
- Console.WriteLine ( "Для случайной телепортации нажмите клавишу E".PadLeft ( 30 ) );
- Console.SetCursorPosition ( 25, 6 );
- Console.WriteLine ( "Для победы зайдите в телепорт отмеченный знаком \"O\"".PadLeft ( 30 ) );
- }
- static void Menu3 ()
- {
- Console.BackgroundColor = ConsoleColor.White;
- Console.ForegroundColor = ConsoleColor.Blue;
- Console.SetCursorPosition ( 15, 1 );
- Console.WriteLine ( " ".PadLeft ( 49 ) );
- Console.SetCursorPosition ( 15, 2 );
- Console.WriteLine ( "\t\t\t ПОЗДРАВЛЯЮ!\t\t\t" );
- Console.SetCursorPosition ( 15, 3 );
- Console.WriteLine ( "\t\t\t ВЫ ПОБЕДИЛИ\t\t\t" );
- Console.SetCursorPosition ( 15, 4 );
- Console.WriteLine ( " ".PadLeft ( 49 ) );
- Console.BackgroundColor = ConsoleColor.Black;
- Console.ForegroundColor = ConsoleColor.White;
- }
- }
- }
Add Comment
Please, Sign In to add comment