C code for Rocket Animation

Simple C code for Rocket Animation

In this article, I will write a  C code for Rocket Animation which will pretend the launching of a rocket on the console. There are many ways to animate the rocket launching in C programming but here I write simple code using character array. I am using the code block to compiling and writing the code.

Let see the  C code for Rocket Animation,

 

#include <stdio.h>

//Giving some delay
void delay( unsigned int value)
{
    unsigned int count1 =0;
    unsigned int count2 = 0;

    for(count1 = 0; count1 < value ; count1++ )
    {
        for(count2 = 0; count2 < count1 ; count2++ )
        {

        }
    }
}


// string to display Rocket
const char rocket[] =
    "           ^ \n\
          /^\\\n\
          |-|\n\
          | |\n\
          |I|\n\
          |S|\n\
          |R|\n\
          |O|\n\
         /| |\\\n\
        / | | \\\n\
       |  | |  |\n\
        `-\"\"\"-`\n\
";

int main()
{

    int jumpControlAtBottom = 0;
    const int someDelay = 6000;
    int shifControl = 0;


    //jump to bottom of console

    for (jumpControlAtBottom = 0; jumpControlAtBottom < 30; ++jumpControlAtBottom)
    {
        printf("\n");
    }

    //Print rocket
    fputs(rocket,stdout);

    for (shifControl = 0; shifControl < 30; ++shifControl)
    {
        // Rocket move on the basis of delay
        delay(someDelay);
        
        // move rocket a line upward
        printf("\n");
    }
    return 0;
}

Output:

Code Analysis:

We have to first jump to the bottom of the console, so in the for loop, I am executing printf with a new line (‘\n’).

   for (jumpControlAtBottom = 0; jumpControlAtBottom < 30; ++jumpControlAtBottom)
    {
        printf("\n");
    }

 

Now times to display the rocket, so using the fputs I am printing the rocket.

fputs(rocket,stdout);

 

You can see the articles,

 

After displaying the rocket, I am using a for loop in which I have given some delay. You can change the delay as per your requirement. I have also displayed the new line using the printf to move the rocket upward,

    for (shifControl = 0; shifControl < 30; ++shifControl)
    {
        // Rocket move on the basis of delay
        delay(someDelay);
        
        // move rocket a line upward
        printf("\n");
    }