Advertisement
bonumopus

BraveNewWorld3

May 11th, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace BraveNewWorld
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.CursorVisible = false;
  14.             int playerX = 7; int playerY = 3;
  15.             int playerDX = 0; int playerDY = 0;
  16.  
  17.             string[,] map = { { "#","#","#","#","#","#","#","#","#","#"},
  18.                 { "#"," "," "," "," "," "," "," "," ","#" },
  19.                 { "#"," "," ","#"," "," "," "," "," ","#"},
  20.                 { "#"," "," ","#"," "," "," "," "," ","#"},
  21.                 { "#"," "," ","#","#","#"," "," "," ","#"},
  22.                 { "#"," "," "," "," ","#"," "," "," ","#"},
  23.                 { "#"," "," "," "," ","#"," "," "," ","#"},
  24.                 { "#"," "," "," "," ","#"," "," "," ","#"},
  25.                 { "#"," "," "," "," "," "," "," "," ","#"},
  26.                 { "#","#","#","#","#","#","#","#","#","#"} };
  27.  
  28.             DrawMap(map, ref playerX, ref playerY);
  29.  
  30.             while (true)
  31.             {
  32.                 if (Console.KeyAvailable)
  33.                 {
  34.                     ChooseDirection(ref playerDX, ref playerDY);
  35.                     if (map[playerX + playerDX, playerY + playerDY] != "#")
  36.                     {
  37.                         MovePlayer(map, ref playerX, ref playerY, ref playerDX, ref playerDY);
  38.                     }
  39.  
  40.                 }
  41.             }
  42.  
  43.         }
  44.  
  45.         static void MovePlayer(string[,] map, ref int playerX, ref int playerY, ref int playerDX, ref int playerDY)
  46.         {
  47.             Console.SetCursorPosition(playerY, playerX);
  48.             Console.Write(" ");
  49.             playerX += playerDX; playerY += playerDY;
  50.             Console.SetCursorPosition(playerY, playerX);
  51.             Console.Write("@");
  52.         }
  53.  
  54.         static void ChooseDirection(ref int playerDX, ref int playerDY)
  55.         {
  56.             ConsoleKeyInfo key = Console.ReadKey(true);
  57.  
  58.             switch (key.Key)
  59.             {
  60.                 case ConsoleKey.UpArrow:
  61.                     playerDX = -1; playerDY = 0;
  62.                     break;
  63.                 case ConsoleKey.DownArrow:
  64.                     playerDX = 1; playerDY = 0;
  65.                     break;
  66.                 case ConsoleKey.LeftArrow:
  67.                     playerDX = 0; playerDY = -1;
  68.                     break;
  69.                 case ConsoleKey.RightArrow:
  70.                     playerDX = 0; playerDY = 1;
  71.                     break;
  72.             }
  73.         }
  74.  
  75.         static void DrawMap(string[,] map, ref int playerX, ref int playerY)
  76.         {
  77.  
  78.             for (int i = 0; i < map.GetLength(0); i++)
  79.             {
  80.                 for (int j = 0; j < map.GetLength(1); j++)
  81.                 {
  82.                     Console.Write(map[i, j]);
  83.                 }
  84.                 Console.WriteLine();
  85.             }
  86.             Console.SetCursorPosition(playerY, playerX);
  87.             Console.Write("@");
  88.             Console.SetCursorPosition(0, 12);
  89.             Console.Write("Для управления используйти клавиши со стрелками");
  90.         }
  91.  
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement