Simple C/C++ Rocket Animation


I have to tell you a story of my own, and it is real.

When I was little boy, I have persuaded my parents to buy a ‘expensive’ (at that time at least) famicom (with keyboard). I lied to them that I used that for study programming but in fact, I spent most of the time in playing 8-bit NES games :).

However, my parents one day asked me to show them  the programs I ‘ve made… I had to come up with any idea (any programming tasks) and demonstrated to them the programming skills, well in BASIC (most famicoms come up with the BASIC programming language).

I programmed a flying rocket at that time with the first generation of BASIC (the one that makes you to put the line number before each statement and use goto to jump to different statements).

Many years later, I’ve lost already the original BASIC program but here is the simple alternative to do it in C/C++.

First, download [here] cnt Simple C/C++ Rocket Animation animation c / c++ (windows platform) or [here] cnt Simple C/C++ Rocket Animation animation c / c++ (ubuntu platform) for this simple animation application made using G++ complier (v.4.6)

The idea is simple: go to the bottom of the console (print a couple of empty lines would do it, DO NOT USE gotoxy(), which is not platform independent); then print the ASCII pattern of the rocket (google for it); and print empty line each time to make rocket fly upward (adjust the speed by changing the time interval for next print). This is based on the scrolling in console. So be sure to print lots of lines at the beginning, which will make sure the current cursor is at the bottom of the console. Everytime a new empty line is printed, the console will scroll up a line for the current screen content, which makes it look like the rocket is flying up! A bit tricky, isn’t it? but it works perfectly!

Here is how it shows on Windows console.

windows-rocket Simple C/C++ Rocket Animation animation c / c++

And on Ubuntu (or *nix), you can edit the source in your favourite editor (vim, vi etc)

g++rocket Simple C/C++ Rocket Animation animation c / c++

and compile it using g++ -o rocket rocket.c

g++ Simple C/C++ Rocket Animation animation c / c++

Launch it!

g++rocket2 Simple C/C++ Rocket Animation animation c / c++

Try it now! Here is the complete C/C++ source code for this simple rocket!:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
    Author: http://HelloACM.com
            http://CodingForSpeed.com
*/
#include <stdio.h>
#include <unistd.h> // for usleep function
 
const char rocket[] =
"           _\n\
          /^\\\n\
          |-|\n\
          | |\n\
          |N|\n\
          |A|\n\
          |S|\n\
          |A|\n\
         /| |\\\n\
        / | | \\\n\
       |  | |  |\n\
        `-\"\"\"-`\n\
";
 
int main() {
    for (int i = 0; i < 50; i ++) printf("\n"); // jump to bottom of console
    printf("%s", rocket);
    int j = 300000;
    for (int i = 0; i < 50; i ++) {
        usleep(j); // move faster and faster,
        j = (int)(j * 0.9); // so sleep less each time
        printf("\n"); // move rocket a line upward
    }
    printf("Visit http://HelloACM.com !\n");
    return 0;
}
/*
    Author: http://HelloACM.com
            http://CodingForSpeed.com
*/
#include <stdio.h>
#include <unistd.h> // for usleep function

const char rocket[] =
"           _\n\
          /^\\\n\
          |-|\n\
          | |\n\
          |N|\n\
          |A|\n\
          |S|\n\
          |A|\n\
         /| |\\\n\
        / | | \\\n\
       |  | |  |\n\
        `-\"\"\"-`\n\
";

int main() {
    for (int i = 0; i < 50; i ++) printf("\n"); // jump to bottom of console
    printf("%s", rocket);
    int j = 300000;
    for (int i = 0; i < 50; i ++) {
        usleep(j); // move faster and faster,
        j = (int)(j * 0.9); // so sleep less each time
        printf("\n"); // move rocket a line upward
    }
    printf("Visit http://HelloACM.com !\n");
    return 0;
}

We use \ to split the string literal into multi-line.

Update: My son just has his version of launching the rocket in Python: The Simple Console Rocket Animation in Python

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
870 words
Last Post: The Matrix Printing Animation in C++
Next Post: Simple C++ Starfield Animation Based on Codepage 437

The Permanent URL is: Simple C/C++ Rocket Animation

6 Comments

  1. Leigap
    • sachin
  2. sivani patel

Leave a Reply