Face with movement by console in C #

0

I have a problem executing a function in console, I want to make a face move from right to left but the movement does not run when I add the function of the eyes, it was my logic to put two functions apart and not only in one.

static void Main(string[] args)
    {
        while (true)
        {
            int i;
            for (i = 1; i < 60; i++)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                dibujarRectangulo(9, 6, i, 1);
                dibujarOjos(i + 1, 2);
                System.Threading.Thread.Sleep(10);
                Console.ForegroundColor = Console.BackgroundColor;
                i -= 1;
                dibujarRectangulo(9, 6, i, 1);
            }
            for (i = 60; i > 5; i--)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                dibujarRectangulo(9, 6, i, 1);
                System.Threading.Thread.Sleep(10);
                Console.ForegroundColor = Console.BackgroundColor;
                dibujarRectangulo(9, 6, i, 1);
            }
            Console.ReadKey(true);
        }
    }

        static void dibujarRectangulo(int ancho, int alto, int x, int y)
        {
            int cont = 0;
            while (cont < ancho)
            {
                Console.SetCursorPosition(x + cont, y);
                Console.Write("-");
                Console.SetCursorPosition(x + cont, y + alto);
                Console.Write("-");
                cont++;
            }
            cont = 0;
            while(cont < alto)
            {
                Console.SetCursorPosition(x, y + cont);
                Console.Write("|");
                Console.SetCursorPosition(x + ancho, y + cont);
                Console.Write("|");
                cont++;
            }
        }

        static void dibujarOjos(int x, int y)
        {

            while (true)
            {
                Console.SetCursorPosition(3, 2);
                Console.Write("■");
            }

        }

As you can see, the graphic does not move when I add the function of to draw Eyes , what problem do I have ?, in advance thank you very much for the help.

    
asked by Churri 14.01.2018 в 17:43
source

1 answer

0

The main problem is the while(true) that you have in the DibuarOjos method that makes it enter an infinite loop.

On the other hand you are drawing a single eye and always in the same position.

You also have a i -= 1; in the first loop that causes it to become another infinite loop with the variable i always having the same value.

static void Main(string[] args)
{
    while (true)
    {
        int i;
        for (i = 1; i < 60; i++)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            dibujarRectangulo(9, 6, i, 1);
            dibujarOjos(i + 3, 2);
            dibujarOjos(i + 6, 2);
            System.Threading.Thread.Sleep(100);
            Console.ForegroundColor = Console.BackgroundColor;
            dibujarRectangulo(9, 6, i, 1);
            dibujarOjos(i + 3, 2);
            dibujarOjos(i + 6, 2);
        }
        for (i = 60; i > 5; i--)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            dibujarRectangulo(9, 6, i, 1);
            dibujarOjos(i + 3, 2);
            dibujarOjos(i + 6, 2);
            System.Threading.Thread.Sleep(100);
            Console.ForegroundColor = Console.BackgroundColor;
            dibujarRectangulo(9, 6, i, 1);
            dibujarOjos(i + 3, 2);
            dibujarOjos(i + 6, 2);
        }
        Console.ReadKey(true);
    }
}

static void dibujarRectangulo(int ancho, int alto, int x, int y)
{
    int cont = 0;
    while (cont < ancho)
    {
        Console.SetCursorPosition(x + cont, y);
        Console.Write("-");
        Console.SetCursorPosition(x + cont, y + alto);
        Console.Write("-");
        cont++;
    }
    cont = 0;
    while (cont < alto)
    {
        Console.SetCursorPosition(x, y + cont);
        Console.Write("|");
        Console.SetCursorPosition(x + ancho, y + cont);
        Console.Write("|");
        cont++;
    }
}

static void dibujarOjos(int x, int y)
{
    Console.SetCursorPosition(x, y);
    Console.Write("■");
}
    
answered by 14.01.2018 / 21:35
source