Advertisement
dxoraxs

Untitled

Apr 20th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.89 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4.  
  5. namespace homework
  6. {
  7.     class Program
  8.     {
  9.         static Random rand = new Random();
  10.  
  11.         static void ChangeMapBackground(int[,] array, int posX, int posY, ConsoleColor[] colorsArray)
  12.         {
  13.             for (int i = 0; i < array.GetLength(0); i++)
  14.             {
  15.                 for (int k = 0; k < array.GetLength(1); k++)
  16.                 {
  17.                     if (array[i, k] >= 0)
  18.                     {
  19.                         Console.SetCursorPosition(posX + k, posY + i);
  20.                         Console.BackgroundColor = colorsArray[array[i, k]];
  21.                         Console.Write(" ");
  22.                     }
  23.                 }
  24.                 Console.WriteLine();
  25.                 Console.ResetColor();
  26.             }
  27.         }
  28.  
  29.         static int[,] ArrayBackground(int posY, int posX, int[,] originalArray, int[,] scaleArray)
  30.         {
  31.             int[,] array = new int[scaleArray.GetLength(0), scaleArray.GetLength(1)];
  32.             for (int i = 0; i < array.GetLength(0); i++)
  33.             {
  34.                 for (int k=0;k<array.GetLength(1); k++)
  35.                 {
  36.                     array[i, k] = originalArray[posX + i, posY + k];
  37.                 }
  38.             }
  39.             return array;
  40.         }
  41.  
  42.         static int[,] RandomColorMachine(int[,] originalArray, ref int posArray)
  43.         {
  44.             int[,] array = new int[originalArray.GetLength(0), originalArray.GetLength(1)];
  45.             if (posArray<0) posArray = rand.Next(9, 16);
  46.             for (int i = 0; i < array.GetLength(0); i++)
  47.             {
  48.                 for (int k = 0; k < array.GetLength(1); k++)
  49.                 {
  50.                     array[i, k] = originalArray[i, k];
  51.                     if (array[i, k] < 0) array[i, k] = posArray;
  52.                 }
  53.             }
  54.             return array;
  55.         }
  56.  
  57.         static void ChangeColorsPlayerMachine(int[,] originalArray, int posX, int posY, ConsoleColor[] colorsArray, int numberColor)
  58.         {
  59.             for (int i = 0; i < originalArray.GetLength(0); i++)
  60.             {
  61.                 for (int k = 0; k < originalArray.GetLength(1); k++)
  62.                 {
  63.                     Console.SetCursorPosition(posX + k, posY + i);
  64.                     if (originalArray[i, k] < 0) Console.BackgroundColor = colorsArray[9 + numberColor];
  65.                     else Console.BackgroundColor = colorsArray[originalArray[i,k]];
  66.                     Console.Write(" ");
  67.                 }
  68.                 Console.WriteLine();
  69.                 Console.ResetColor();
  70.             }
  71.         }
  72.         static void Main(string[] args)
  73.         {
  74.             Console.SetWindowSize(150, 51);
  75.             Console.CursorVisible = false;
  76.             const string youLose = "YOU DIED";
  77.             int consoleWidth = Console.WindowWidth, consoleHeight = Console.WindowHeight;
  78.             ConsoleColor[] _colors = (ConsoleColor[])ConsoleColor.GetValues(typeof(ConsoleColor));
  79.             int[,] map = new int[50, 54];
  80.             float score = 0;
  81.             string[] lines = File.ReadAllLines("map.txt").Take(50).ToArray();
  82.  
  83.             int[,] playerMachine = {
  84.             { -1, -1, -1,-1 },
  85.             { -1, -1, -1,-1 },
  86.             { -1, 1, 1,-1 },
  87.             { -1, 1, 1,-1 },
  88.             { -1, -1, -1,-1 },
  89.             };
  90.             int[,] aboutMachine = {
  91.             { -1, -1, -1, -1},
  92.             { -1, -1, -1, -1},
  93.             { -1, 0, 0, -1,},
  94.             { -1, 0, 0, -1,},
  95.             { -1, -1, -1, -1},
  96.             };
  97.  
  98.             int[,] coorX = { { 0, 4, 9 }, { 1, 25, 10 }, { 2, 5, 12 }, { 3, 34, 13 }, { 4, 40, 11 }};
  99.             int[,] startCoorX = { { 0, 4, 9 }, { 1, 25, 10 }, { 2, 5, 12 }, { 3, 34, 13 }, { 4, 40, 11 } };
  100.  
  101.             for (int i = 0; i < map.GetLength(0); i++)
  102.             {
  103.                 int[] row = lines[i].Split(new char[] { '   ' }, StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToArray();
  104.                 for (int j = 0; j < map.GetLength(1); j++)
  105.                 {
  106.                     map[i, j] = row[j];
  107.                 }
  108.             }
  109.  
  110.             int userY = 11+7*2, userX = 42;
  111.  
  112.             Console.WriteLine("\n    Выберите цвет\n вашего автомобиля:");
  113.             bool setColor = false;
  114.             int playerColor = 0;
  115.             Console.SetCursorPosition(0, playerMachine.GetLength(1) + 7);
  116.             Console.Write("Enter для сохранения...");
  117.             ConsoleKeyInfo charKey;
  118.             while (!setColor)
  119.             {
  120.                 ChangeColorsPlayerMachine(playerMachine, 6,4, _colors, playerColor);
  121.                 Console.SetCursorPosition(5, playerMachine.GetLength(1) + 6);
  122.                 Console.BackgroundColor = ConsoleColor.White;
  123.                 Console.ForegroundColor = ConsoleColor.Black;
  124.                 Console.Write(" < " + playerColor + " > ");
  125.                 Console.ResetColor();
  126.  
  127.                 charKey = Console.ReadKey();
  128.                 switch (charKey.Key)
  129.                 {
  130.                     case ConsoleKey.LeftArrow:
  131.                         playerColor--;
  132.                         if (playerColor < 0) playerColor = 6;
  133.                         break;
  134.                     case ConsoleKey.RightArrow:
  135.                         playerColor++;
  136.                         if (playerColor >= 7) playerColor = 0;
  137.                         break;
  138.                     case ConsoleKey.Enter:
  139.                         setColor = true;
  140.                         break;
  141.  
  142.                 }
  143.             }
  144.  
  145.             Console.Clear();
  146.             for (int i =0;i< playerMachine.GetLength(0); i++)
  147.             {
  148.                 for (int k = 0; k < playerMachine.GetLength(1); k++)
  149.                     if (playerMachine[i, k] < 0) playerMachine[i, k] = 9+playerColor;
  150.             }
  151.             Console.Clear();
  152.             ChangeMapBackground(map, 0, 0, _colors);
  153.             ChangeMapBackground(playerMachine, userY, userX, _colors);
  154.  
  155.             bool isPlay = true;
  156.             while (true)
  157.             {
  158.                 System.Threading.Thread.Sleep(100);
  159.                 if (isPlay)
  160.                 {
  161.                     if (Console.KeyAvailable)
  162.                     {
  163.                         charKey = Console.ReadKey();
  164.                         switch (charKey.Key)
  165.                         {
  166.                             case ConsoleKey.LeftArrow:
  167.                                 if (userY > 10)
  168.                                 {
  169.                                     ChangeMapBackground(ArrayBackground(userY, userX, map, playerMachine), userY, userX, _colors);
  170.                                     userY--;
  171.                                     ChangeMapBackground(playerMachine, userY, userX, _colors);
  172.                                 }
  173.                                 break;
  174.                             case ConsoleKey.RightArrow:
  175.                                 if (userY < 40)
  176.                                 {
  177.                                     ChangeMapBackground(ArrayBackground(userY, userX, map, playerMachine), userY, userX, _colors);
  178.                                     userY++;
  179.                                     ChangeMapBackground(playerMachine, userY, userX, _colors);
  180.                                 }
  181.                                 break;
  182.                         }
  183.                     }
  184.                     for (int i = 0; i < coorX.GetLength(0); i++)
  185.                     {
  186.                         ChangeMapBackground(ArrayBackground(11 + 7 * coorX[i, 0], coorX[i, 1], map, aboutMachine), 11 + 7 * coorX[i, 0], coorX[i, 1], _colors);
  187.                         coorX[i, 1]++;
  188.                         if (coorX[i, 1] >= 46)
  189.                         {
  190.                             coorX[i, 1] = 0;
  191.                             coorX[i, 2] = -1;
  192.                             score += 1f;
  193.                         }
  194.                         if (coorX[i, 1] >= 38 && (11 + 7 * coorX[i, 0] >= userY-3 && 11 + 7 * coorX[i, 0] <= userY + 3))
  195.                         {
  196.                             isPlay = false;
  197.                         }
  198.                         ChangeMapBackground(RandomColorMachine(aboutMachine, ref coorX[i, 2]), 11 + 7 * coorX[i, 0], coorX[i, 1], _colors);
  199.                     }
  200.                     Console.SetCursorPosition(consoleWidth / 2 - ("Очки: " + (int)score).Length / 2, consoleHeight / 2);
  201.                     Console.Write("Очки: " + (int)score);
  202.                 }
  203.                 else
  204.                 {
  205.                     Console.SetCursorPosition(consoleWidth / 2 - youLose.Length / 2, consoleHeight / 2);
  206.                     Console.WriteLine(youLose);
  207.                     Console.SetCursorPosition(consoleWidth / 2 - ("Очки: " + (int)score).Length / 2, consoleHeight / 2 + 1);
  208.                     Console.Write("Очки: " + (int)score);
  209.                     Console.SetCursorPosition(consoleWidth / 2 - "Хотите начать заново? Y/другой знак, чтобы выйти".Length / 2/2, consoleHeight / 2 + 2);
  210.                     Console.Write("Хотите начать заново? Y/другой знак, чтобы выйти");
  211.                     Console.SetCursorPosition(consoleWidth / 2 - 2, consoleHeight / 2 + 3);
  212.                     string str = Console.ReadLine().ToLower();
  213.                     if (str == "y")
  214.                     {
  215.                         Console.Clear();
  216.                         isPlay = true;
  217.                         for (int i = 0; i < coorX.GetLength(0); i++)
  218.                             for (int j = 0; j < coorX.GetLength(1); j++)
  219.                                 coorX[i, j] = startCoorX[i, j];
  220.  
  221.                         ChangeMapBackground(map, 0, 0, _colors);
  222.                         ChangeMapBackground(playerMachine, userY, userX, _colors);
  223.                     }
  224.                     else return;
  225.                     Console.ReadKey();
  226.                 }
  227.             }
  228.         }
  229.     }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement