Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Linq;
- namespace homework
- {
- class Program
- {
- static Random rand = new Random();
- static void ChangeMapBackground(int[,] array, int posX, int posY, ConsoleColor[] colorsArray)
- {
- for (int i = 0; i < array.GetLength(0); i++)
- {
- for (int k = 0; k < array.GetLength(1); k++)
- {
- if (array[i, k] >= 0)
- {
- Console.SetCursorPosition(posX + k, posY + i);
- Console.BackgroundColor = colorsArray[array[i, k]];
- Console.Write(" ");
- }
- }
- Console.WriteLine();
- Console.ResetColor();
- }
- }
- static int[,] ArrayBackground(int posY, int posX, int[,] originalArray, int[,] scaleArray)
- {
- int[,] array = new int[scaleArray.GetLength(0), scaleArray.GetLength(1)];
- for (int i = 0; i < array.GetLength(0); i++)
- {
- for (int k=0;k<array.GetLength(1); k++)
- {
- array[i, k] = originalArray[posX + i, posY + k];
- }
- }
- return array;
- }
- static int[,] RandomColorMachine(int[,] originalArray, ref int posArray)
- {
- int[,] array = new int[originalArray.GetLength(0), originalArray.GetLength(1)];
- if (posArray<0) posArray = rand.Next(9, 16);
- for (int i = 0; i < array.GetLength(0); i++)
- {
- for (int k = 0; k < array.GetLength(1); k++)
- {
- array[i, k] = originalArray[i, k];
- if (array[i, k] < 0) array[i, k] = posArray;
- }
- }
- return array;
- }
- static void ChangeColorsPlayerMachine(int[,] originalArray, int posX, int posY, ConsoleColor[] colorsArray, int numberColor)
- {
- for (int i = 0; i < originalArray.GetLength(0); i++)
- {
- for (int k = 0; k < originalArray.GetLength(1); k++)
- {
- Console.SetCursorPosition(posX + k, posY + i);
- if (originalArray[i, k] < 0) Console.BackgroundColor = colorsArray[9 + numberColor];
- else Console.BackgroundColor = colorsArray[originalArray[i,k]];
- Console.Write(" ");
- }
- Console.WriteLine();
- Console.ResetColor();
- }
- }
- static void Main(string[] args)
- {
- Console.SetWindowSize(150, 51);
- Console.CursorVisible = false;
- const string youLose = "YOU DIED";
- int consoleWidth = Console.WindowWidth, consoleHeight = Console.WindowHeight;
- ConsoleColor[] _colors = (ConsoleColor[])ConsoleColor.GetValues(typeof(ConsoleColor));
- int[,] map = new int[50, 54];
- float score = 0;
- string[] lines = File.ReadAllLines("map.txt").Take(50).ToArray();
- int[,] playerMachine = {
- { -1, -1, -1,-1 },
- { -1, -1, -1,-1 },
- { -1, 1, 1,-1 },
- { -1, 1, 1,-1 },
- { -1, -1, -1,-1 },
- };
- int[,] aboutMachine = {
- { -1, -1, -1, -1},
- { -1, -1, -1, -1},
- { -1, 0, 0, -1,},
- { -1, 0, 0, -1,},
- { -1, -1, -1, -1},
- };
- int[,] coorX = { { 0, 4, 9 }, { 1, 25, 10 }, { 2, 5, 12 }, { 3, 34, 13 }, { 4, 40, 11 }};
- int[,] startCoorX = { { 0, 4, 9 }, { 1, 25, 10 }, { 2, 5, 12 }, { 3, 34, 13 }, { 4, 40, 11 } };
- for (int i = 0; i < map.GetLength(0); i++)
- {
- int[] row = lines[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToArray();
- for (int j = 0; j < map.GetLength(1); j++)
- {
- map[i, j] = row[j];
- }
- }
- int userY = 11+7*2, userX = 42;
- Console.WriteLine("\n Выберите цвет\n вашего автомобиля:");
- bool setColor = false;
- int playerColor = 0;
- Console.SetCursorPosition(0, playerMachine.GetLength(1) + 7);
- Console.Write("Enter для сохранения...");
- ConsoleKeyInfo charKey;
- while (!setColor)
- {
- ChangeColorsPlayerMachine(playerMachine, 6,4, _colors, playerColor);
- Console.SetCursorPosition(5, playerMachine.GetLength(1) + 6);
- Console.BackgroundColor = ConsoleColor.White;
- Console.ForegroundColor = ConsoleColor.Black;
- Console.Write(" < " + playerColor + " > ");
- Console.ResetColor();
- charKey = Console.ReadKey();
- switch (charKey.Key)
- {
- case ConsoleKey.LeftArrow:
- playerColor--;
- if (playerColor < 0) playerColor = 6;
- break;
- case ConsoleKey.RightArrow:
- playerColor++;
- if (playerColor >= 7) playerColor = 0;
- break;
- case ConsoleKey.Enter:
- setColor = true;
- break;
- }
- }
- Console.Clear();
- for (int i =0;i< playerMachine.GetLength(0); i++)
- {
- for (int k = 0; k < playerMachine.GetLength(1); k++)
- if (playerMachine[i, k] < 0) playerMachine[i, k] = 9+playerColor;
- }
- Console.Clear();
- ChangeMapBackground(map, 0, 0, _colors);
- ChangeMapBackground(playerMachine, userY, userX, _colors);
- bool isPlay = true;
- while (true)
- {
- System.Threading.Thread.Sleep(100);
- if (isPlay)
- {
- if (Console.KeyAvailable)
- {
- charKey = Console.ReadKey();
- switch (charKey.Key)
- {
- case ConsoleKey.LeftArrow:
- if (userY > 10)
- {
- ChangeMapBackground(ArrayBackground(userY, userX, map, playerMachine), userY, userX, _colors);
- userY--;
- ChangeMapBackground(playerMachine, userY, userX, _colors);
- }
- break;
- case ConsoleKey.RightArrow:
- if (userY < 40)
- {
- ChangeMapBackground(ArrayBackground(userY, userX, map, playerMachine), userY, userX, _colors);
- userY++;
- ChangeMapBackground(playerMachine, userY, userX, _colors);
- }
- break;
- }
- }
- for (int i = 0; i < coorX.GetLength(0); i++)
- {
- ChangeMapBackground(ArrayBackground(11 + 7 * coorX[i, 0], coorX[i, 1], map, aboutMachine), 11 + 7 * coorX[i, 0], coorX[i, 1], _colors);
- coorX[i, 1]++;
- if (coorX[i, 1] >= 46)
- {
- coorX[i, 1] = 0;
- coorX[i, 2] = -1;
- score += 1f;
- }
- if (coorX[i, 1] >= 38 && (11 + 7 * coorX[i, 0] >= userY-3 && 11 + 7 * coorX[i, 0] <= userY + 3))
- {
- isPlay = false;
- }
- ChangeMapBackground(RandomColorMachine(aboutMachine, ref coorX[i, 2]), 11 + 7 * coorX[i, 0], coorX[i, 1], _colors);
- }
- Console.SetCursorPosition(consoleWidth / 2 - ("Очки: " + (int)score).Length / 2, consoleHeight / 2);
- Console.Write("Очки: " + (int)score);
- }
- else
- {
- Console.SetCursorPosition(consoleWidth / 2 - youLose.Length / 2, consoleHeight / 2);
- Console.WriteLine(youLose);
- Console.SetCursorPosition(consoleWidth / 2 - ("Очки: " + (int)score).Length / 2, consoleHeight / 2 + 1);
- Console.Write("Очки: " + (int)score);
- Console.SetCursorPosition(consoleWidth / 2 - "Хотите начать заново? Y/другой знак, чтобы выйти".Length / 2/2, consoleHeight / 2 + 2);
- Console.Write("Хотите начать заново? Y/другой знак, чтобы выйти");
- Console.SetCursorPosition(consoleWidth / 2 - 2, consoleHeight / 2 + 3);
- string str = Console.ReadLine().ToLower();
- if (str == "y")
- {
- Console.Clear();
- isPlay = true;
- for (int i = 0; i < coorX.GetLength(0); i++)
- for (int j = 0; j < coorX.GetLength(1); j++)
- coorX[i, j] = startCoorX[i, j];
- ChangeMapBackground(map, 0, 0, _colors);
- ChangeMapBackground(playerMachine, userY, userX, _colors);
- }
- else return;
- Console.ReadKey();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement