How to display two generated ASCII text using inheritance in console application in C#

302 Views Asked by At

I'm trying to do a project with regard to displaying two separate text-animation in one line which shall display like this image example

I am using inheritance for this. I, however, can't display them in one line. I need assistance aligning them in the same line as the image provided.

Here is the screenshot of the code. Code screenshot I can't seem to upload the code. public void Egg() {

        Console.Write("     ___" +
            "\n    /   \\" +
            "\n   |’    |" +
            "\n    \\___/");
        //return;

    }

    public void Cat()
    {
        Console.Write("\n                                      _,'|             _.-''``-...___..--';" +
                          "\n                                     /_ \\'.      __..-' ,      ,--...--'''" +
                          "\n                                   <\\    .`--'''       `     /'" +
                          "\n                                    `-';'               ;   ; ;" +
                          "\n                            __...--''     ___...--_..'  .;.'" +
                          "\n                          (,__....----'''       (,..--''   " +
                          "");
        //return;
    }
    public class My_Calculation : Program
    {
        static void Main(string[] args)
        {
            My_Calculation demo = new My_Calculation();
            demo.Egg();
            demo.Cat();


            Console.Read();
        }
    }

I am open to suggestions if there is a better way of solving this. Thanks in advance!

1

There are 1 best solutions below

18
YungDeiza On

You won't be able to align them when you print one after the other. The egg has new line characters in it which means that something printed after it will always be underneath it.

The simple solution to this is to print the cat and the egg at the same time i.e. have the cat and egg in the same string.

Update:

To Achieve what (I think) you want to do, all you need is to have a token that you replace with whitespace to make the cat move closer to the egg e.g.:

public static class CatAndEgg
{
    private const string ReplacementString = "[SPACES]";
    private const string Image = @"        [SPACES]             _,'|             _.-''``-...___..--';
    [SPACES]          /_ \\'.      __..-' ,      ,--...--'''
___  [SPACES]        <\\    .`--'''       `     /'
/   \\[SPACES]          `-';'               ;   ; ;
|’    |[SPACES]  __...--''     ___...--_..'  .;.'
\\___/ [SPACES](,__....----'''       (,..--'' ";

    public static void PrintImageWithSpaces(int numberOfSpaces)
    {
        string spaces = new(' ', numberOfSpaces);
        Console.Write(Image.Replace(ReplacementString, spaces));
        Console.Write(Environment.NewLine);
    }

    public static void DoAnimation()
    {
        PrintImageWithSpaces(20);
        Task.Delay(200).Wait();
        PrintImageWithSpaces(15);
        Task.Delay(200).Wait();
        PrintImageWithSpaces(10);
        Task.Delay(200).Wait();
        PrintImageWithSpaces(5);
        Task.Delay(200).Wait();
        PrintImageWithSpaces(0);
    }
}

The method PrintImageWithSpaces() prints the cat and the egg with the specified number of spaces between them. By staring off with x number of spaces and then reducing the number of spaces each time you call PrintImageWithSpaces(), the cat moves closer to the egg.

Calling DoAnimation() would provide an example of the animation that you want.

i.e.:

public static void Main()
{
    //Animation Example
    CatAndEgg.DoAnimation();

    //Manually Move cat closer to egg
    CatAndEgg.PrintImageWithSpaces(10);
    Task.Delay(200).Wait();
    CatAndEgg.PrintImageWithSpaces(5);
    Task.Delay(200).Wait();
    CatAndEgg.PrintImageWithSpaces(0);
}

Note: the time delays are added in the above code so you can see the prints gradually added but you wouldn't need them if you print each time a user inputs a wrong answer.