Points Game

 avatar
unknown
csharp
a year ago
3.6 kB
3
Indexable
using System.Drawing;
using System.Globalization;

char playerChar = '*';
ConsoleColor playerColor = ConsoleColor.Green;

char pointChar = '$';

int startPlayerPositionX = 1;
int startPlayerPositionY = 1;

int currentPlayerPositionX = startPlayerPositionX;
int currentPlayerPositionY = startPlayerPositionY;

int pointAmount = 0;
int targetPoints = 3;

bool isWon = false;

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


while (true)
{
    Console.Clear();    

    Render(map);
    Input();

    if (isWon)
    {
        Console.WriteLine("Поздравляем! Вы победили!");
        break;
    }
}

void Render(char[,] map)
{
    for (int i = 0; i < map.GetLength(0); i++)
    {
        for (int j = 0; j < map.GetLength(1); j++)
        {
            if (i == currentPlayerPositionY && j == currentPlayerPositionX)
            {
                if (map[i, j] == '$')
                {
                    pointAmount++;
                    map[i, j] = ' ';
                }

                Console.ForegroundColor = playerColor;
                Console.Write(playerChar + " ");
            }
            else
            {
                if (map[i, j] == '$')
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                }

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

    Console.WriteLine();
    Console.WriteLine($"СОБРАНО ОЧКОВ: {pointAmount}/{targetPoints}");

    if (pointAmount >= targetPoints)
    {
        Console.WriteLine();
        Console.WriteLine("ВЫ СОБРАЛИ ДОСТАТОЧНО ОЧКОВ ДЛЯ ПРОДОЛЖЕНИЯ, НАЖМИТЕ ENTER");
    }
}

void Input()
{
    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 (pointAmount >= targetPoints)
            {
                isWon = true;
            }
            break;
        default:
            break;
    }

    
}
Editor is loading...
Leave a Comment