Top Down Game CLI

 avatar
unknown
csharp
a year ago
4.1 kB
5
Indexable
internal class Program
{
	private static void Main(string[] args)
	{
		Menu menu = new Menu();
		Game game = new Game();

		switch (menu.GetMenuIndex())
		{
			case 0:
				game.Start();
				break;
			default:
				break;
		}

	}
}

public class Menu
{
	public string[] Points = new string[]
	{
		"Старт",
		"Настройки",
		"Выход"
	};

	public int SelectedIndex;

	public Menu()
	{

	}

	public int GetMenuIndex()
	{
		while (true)
		{
			Render();
			int selectedIndex = Input();

			if (selectedIndex != -1)
			{
				return selectedIndex;
			}
		}
	}

	public void Render()
	{
		Console.Clear();

		for (int i = 0; i < Points.Length; i++)
		{
			if (i == SelectedIndex)
			{
				Console.ForegroundColor = ConsoleColor.Blue;
			}

			Console.WriteLine($"{i + 1}. {Points[i]}");

			Console.ResetColor();
		}
	}

	public int Input()
	{
		switch (Console.ReadKey().Key)
		{
			case ConsoleKey.UpArrow:
				SelectedIndex--;
				return -1;
			case ConsoleKey.DownArrow:
				SelectedIndex++;
				return -1;
			case ConsoleKey.Enter:
				return SelectedIndex;
			default:
				return -1;
		}
	}
}

public class Game
{
	char playerChar = '*';

	char[,] map = new char[,]
	{
			{ '#','#','#','#','#','#','#','#','#','#', },
			{ '#',' ','#',' ',' ',' ',' ','#',' ','#', },
			{ '#',' ','#',' ',' ','$',' ','#',' ','#', },
			{ '#',' ',' ',' ',' ',' ',' ','#',' ','#', },
			{ '#',' ',' ',' ','#','#',' ','#',' ','#', },
			{ '#',' ',' ',' ','#','#',' ','#',' ','#', },
			{ '#',' ','#','$','#',' ',' ','#',' ','#', },
			{ '#',' ','#',' ','#',' ',' ',' ','$','#', },
			{ '#',' ','#',' ','#',' ',' ',' ',' ','#', },
			{ '#','#','#','#','#','#','#','#','#','#', },
	};

	int startPlayerPositionX = 1;
	int startPlayerPositionY = 1;

	int currentPlayerPositionX;
	int currentPlayerPositionY;

	int playerScore = 0;
	int playerScoreTarget = 3;

	bool isWon = false;

	public void Start()
	{
		currentPlayerPositionX = startPlayerPositionX;
		currentPlayerPositionY = startPlayerPositionY;

		while (true)
		{
			RenderFrame();

			switch (Console.ReadKey().Key)
			{
				case ConsoleKey.LeftArrow:
					if (map[currentPlayerPositionY, currentPlayerPositionX - 1] != '#')
					{
						currentPlayerPositionX--;
					}
					break;
				case ConsoleKey.RightArrow:
					if (map[currentPlayerPositionY, currentPlayerPositionX + 1] != '#')
					{
						currentPlayerPositionX++;
					}
					break;
				case ConsoleKey.UpArrow:
					if (map[currentPlayerPositionY - 1, currentPlayerPositionX] != '#')
					{
						currentPlayerPositionY--;
					}
					break;
				case ConsoleKey.DownArrow:
					if (map[currentPlayerPositionY + 1, currentPlayerPositionX] != '#')
					{
						currentPlayerPositionY++;
					}
					break;
				case ConsoleKey.Enter:
					if (isWon)
					{
						return;
					}
					break;
				default:
					break;
			}

			if (map[currentPlayerPositionY, currentPlayerPositionX] == '$')
			{
				playerScore++;
				map[currentPlayerPositionY, currentPlayerPositionX] = 'X';
			}
		}
	}

	void RenderFrame()
	{
		Console.Clear();

		for (int i = 0; i < map.GetLength(0); i++)
		{
			for (int j = 0; j < map.GetLength(1); j++)
			{
				if (i == currentPlayerPositionY && j == currentPlayerPositionX)
				{
					Console.ForegroundColor = ConsoleColor.Green;
					Console.Write(playerChar + " ");
				}
				else
				{
					if (map[i, j] == '$')
					{
						Console.ForegroundColor = ConsoleColor.Yellow;
					}

					Console.Write(map[i, j] + " ");
				}

				Console.ResetColor();
			}

			Console.WriteLine();
		}


		if (playerScore >= playerScoreTarget)
		{
			Console.WriteLine();
			Console.WriteLine("ВЫ СОБРАЛИ СКОЛЬКО НУЖНО. ДЛЯ ПРОДОЛЖЕНИЯ НАЖМИТЕ ENTER");

			isWon = true;
		}
		else
		{
			Console.WriteLine();
			Console.WriteLine("СОБРАННО: " + playerScore);
		}
	}
}
Leave a Comment