Advertisement
bonumopus

BraveNewWorld2

May 11th, 2020
55
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.                     MovePlayer(map, ref playerX, ref playerY, ref playerDX, ref playerDY);
  36.                 }
  37.             }
  38.  
  39.         }
  40.  
  41.         static void MovePlayer(string[,] map, ref int playerX, ref int playerY, ref int playerDX, ref int playerDY)
  42.         {
  43.             if (map[playerX + playerDX, playerY + playerDY] != "#")
  44.             {
  45.                 Console.SetCursorPosition(playerY, playerX);
  46.                 Console.Write(" ");
  47.                 playerX += playerDX; playerY += playerDY;
  48.                 Console.SetCursorPosition(playerY, playerX);
  49.                 Console.Write("@");
  50.             }
  51.         }
  52.  
  53.         static void ChooseDirection(ref int playerDX, ref int playerDY)
  54.         {
  55.             ConsoleKeyInfo key = Console.ReadKey(true);
  56.  
  57.             switch (key.Key)
  58.             {
  59.                 case ConsoleKey.UpArrow:
  60.                     playerDX = -1; playerDY = 0;
  61.                     break;
  62.                 case ConsoleKey.DownArrow:
  63.                     playerDX = 1; playerDY = 0;
  64.                     break;
  65.                 case ConsoleKey.LeftArrow:
  66.                     playerDX = 0; playerDY = -1;
  67.                     break;
  68.                 case ConsoleKey.RightArrow:
  69.                     playerDX = 0; playerDY = 1;
  70.                     break;
  71.             }
  72.         }
  73.  
  74.         static void DrawMap(string[,] map, ref int playerX, ref int playerY)
  75.         {
  76.  
  77.             for (int i = 0; i < map.GetLength(0); i++)
  78.             {
  79.                 for (int j = 0; j < map.GetLength(1); j++)
  80.                 {
  81.                     Console.Write(map[i, j]);
  82.                 }
  83.                 Console.WriteLine();
  84.             }
  85.             Console.SetCursorPosition(playerY, playerX);
  86.             Console.Write("@");
  87.             Console.SetCursorPosition(0, 12);
  88.             Console.Write("Для управления используйти клавиши со стрелками");
  89.         }
  90.  
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement