illiden

BraveNewWorld

Dec 11th, 2019
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.88 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. using System.IO;
  7.  
  8. namespace BraveNewWorld
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             Console.CursorVisible = false;
  15.             int maxLevels = 2; // по хорошему тут бы считать, сколько файлов в папке
  16.             string lvlName = "";
  17.             int level = 1;
  18.             bool isPlaying = true;
  19.             int playerX, playerY;
  20.             int playerDX = 0, playerDY = 0;
  21.             int endX, endY;
  22.             int[,] enemies = new int[0, 0];
  23.             int[] enemyDirections;
  24.             int maxDots, collectDots;
  25.             int enemySpeed = 1500;
  26.             bool alive = true;
  27.  
  28.             while (level < maxLevels && alive)
  29.             {
  30.                 lvlName = "map" + (level + 1);
  31.                 isPlaying = true;
  32.                 maxDots = 0;
  33.                 collectDots = 0;
  34.  
  35.                 char[,] map = ReadMap(lvlName, out playerY, out playerX, out endX, out endY, ref enemies, ref maxDots);
  36.                 DrawMap(map);
  37.                 enemyDirections = new int[enemies.GetLength(0)];
  38.                 for (int d = 0; d < enemyDirections.Length; d++)
  39.                 {
  40.                     enemyDirections[d] = 1;
  41.                 }
  42.  
  43.                 while (isPlaying)
  44.                 {
  45.                     Console.SetCursorPosition(0, 20);
  46.                     Console.WriteLine($"Собрано {collectDots} / {maxDots}");
  47.                     if (Console.KeyAvailable)
  48.                     {
  49.                         ConsoleKeyInfo key = Console.ReadKey(true);
  50.                         ChangeDirection(key, ref playerDX, ref playerDY);
  51.  
  52.                         if (map[playerX + playerDX, playerY + playerDY] != '#' && map[playerX + playerDX, playerY + playerDY] != '+')
  53.                         {
  54.                             Move(ref playerX, ref playerY, ref playerDX, ref playerDY);
  55.  
  56.                             CollectDots(map, playerX, playerY, ref collectDots);
  57.                         }
  58.                     }
  59.                     if (enemySpeed == 0 )
  60.                     {
  61.                         EnemyMove(map, enemies, enemyDirections);
  62.                         enemySpeed = 1500;
  63.                     }
  64.                     Win(map, enemies,ref collectDots, ref maxDots, endX, endY, playerX, playerY, ref isPlaying, ref alive);
  65.                     enemySpeed--;
  66.                 }
  67.                 level++;
  68.             }
  69.  
  70.             if (alive)
  71.             {
  72.                 Console.Clear();
  73.                 Console.Write("Вы победили!");
  74.                 Console.ReadKey();
  75.             }
  76.             Console.ReadKey();
  77.         }
  78.  
  79.         static void Win(char[,] map, int[,] enemies, ref int collectDots, ref int maxDots, int endX, int endY, int x, int y, ref bool playing, ref bool alive)
  80.         {
  81.             for (int i = 0; i < enemies.GetLength(0); i++)
  82.             {
  83.                 if (enemies[i, 0] == y && enemies[i, 1] == x)
  84.                 {
  85.                     playing = false;
  86.                     alive = false;
  87.                     Console.Clear();
  88.                     Console.WriteLine("Вас убили!");
  89.                 }
  90.             }
  91.             if (collectDots == maxDots)
  92.             {
  93.                 Console.SetCursorPosition(endX, endY);
  94.                 Console.Write("=");
  95.                 map[endY, endX] = '=';
  96.                 if (map[x, y] == '=')
  97.                 {
  98.                     playing = false;
  99.                 }
  100.             }
  101.         }
  102.  
  103.         static void CollectDots(char[,] map, int x, int y, ref int collectDots)
  104.         {
  105.             if (map[x, y] == '.')
  106.             {
  107.                 collectDots++;
  108.                 map[x, y] = ' ';
  109.             }
  110.         }
  111.  
  112.         static void ChangeDirection(ConsoleKeyInfo key, ref int dx, ref int dy)
  113.         {
  114.             switch (key.Key)
  115.             {
  116.                 case ConsoleKey.UpArrow:
  117.                     dx = -1; dy = 0;
  118.                     break;
  119.                 case ConsoleKey.DownArrow:
  120.                     dx = 1; dy = 0;
  121.                     break;
  122.                 case ConsoleKey.RightArrow:
  123.                     dx = 0; dy = 1;
  124.                     break;
  125.                 case ConsoleKey.LeftArrow:
  126.                     dx = 0; dy = -1;
  127.                     break;
  128.             }
  129.         }
  130.  
  131.         static void EnemyMove(char[,] map, int[,] enemies, int[] directions)
  132.         {
  133.             int x = 0;
  134.             int y = 0;
  135.  
  136.             for (int i = 0; i < directions.Length; i++)
  137.             {
  138.                 x = enemies[i, 1];
  139.                 y = enemies[i, 0];
  140.                 if (map[x + directions[i], y] == '#')
  141.                 {
  142.                     directions[i] *= -1;
  143.                 }
  144.                 Console.SetCursorPosition(y, x);
  145.                 if (map[x, y] == '$')
  146.                 {
  147.                     map[x, y] = '.';
  148.                 }
  149.                 Console.Write(map[x, y]);
  150.                 enemies[i, 1] += directions[i];
  151.                 y = enemies[i, 0];
  152.                 x = enemies[i, 1];
  153.                 Console.SetCursorPosition(y, x);
  154.                 Console.Write('$');
  155.             }
  156.         }
  157.  
  158.         static void Move( ref int x, ref int y, ref int dx, ref int dy)
  159.         {
  160.             Console.SetCursorPosition(y, x);
  161.             Console.Write(' ');
  162.             x += dx;
  163.             y += dy;
  164.             Console.SetCursorPosition(y, x);
  165.             Console.Write('@');
  166.         }
  167.  
  168.         static void DrawMap(char[,] map)
  169.         {
  170.             Console.SetCursorPosition(0, 0);
  171.             for(int i = 0; i < map.GetLength(0); i++)
  172.             {
  173.                 for (int j = 0; j < map.GetLength(1); j++)
  174.                 {
  175.                     Console.Write(map[i, j]);
  176.                 }
  177.                 Console.WriteLine();
  178.             }
  179.         }
  180.  
  181.         static char[,] ReadMap(string mapName, out int x, out int y, out int endX, out int endY, ref int[,] enemies, ref int dots)
  182.         {
  183.             x = 0;
  184.             y = 0;
  185.             endX = 0;
  186.             endY = 0;
  187.             string[] newFile = File.ReadAllLines($"Maps/{mapName}.txt");
  188.             char[,] file = new char[newFile.Length,newFile[0].Length];
  189.             for(int i = 0; i < file.GetLength(0); i++)
  190.             {
  191.                 for (int j = 0; j < file.GetLength(1); j++)
  192.                 {
  193.  
  194.                     file[i, j] = newFile[i][j];
  195.                     if (file[i, j] == '@')
  196.                     {
  197.                         x = j;
  198.                         y = i;
  199.                     }
  200.                     else if (file[i, j] == '$')
  201.                     {
  202.                         AddEnemy(ref enemies, j, i);
  203.                     }
  204.                     else if (file[i, j] == ' ')
  205.                     {
  206.                         file[i, j] = '.';
  207.                         dots++;
  208.                     }
  209.                     else if (file[i, j] == '=')
  210.                     {
  211.                         file[i, j] = '+';
  212.                         endX = j;
  213.                         endY = i;
  214.                     }
  215.                 }
  216.             }
  217.             return file;
  218.         }
  219.  
  220.         static void AddEnemy(ref int[,] oldMass, int newPositionX, int newPositionY)
  221.         {
  222.             int[,] newMass = new int[oldMass.GetLength(0) + 1, 2];
  223.             int[] newPosition = { newPositionX, newPositionY };
  224.  
  225.             for (int i = 0; i < oldMass.GetLength(0); i++)
  226.             {
  227.                 for (int j = 0; j < 2; j++)
  228.                 {
  229.                     newMass[i, j] = oldMass[i, j];
  230.                 }
  231.             }
  232.             for (int i = 0; i < 2; i++)
  233.             {
  234.                 newMass[newMass.GetLength(0) - 1, i] = newPosition[i];
  235.             }
  236.             oldMass = newMass;
  237.         }
  238.     }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment