Creating a console animation with C++

Evans Ehiorobo
How-Tos
Published in
4 min readOct 9, 2019
Star Wars

A long time ago in a galaxy far, far away….

I was inspired to work on this project after watching a Star Wars animation in my terminal. Apparently some folks had gone through the trouble of creating an animation of Star Wars using only characters in the terminal. You can watch it by typing in telnet towel.blinkenlights.nl 23.

In my quest to learn C++, I decided to do a little project where I would create an animation in the console (no, not a movie). The animation would be something simple — just some characters blinking on the screen and changing colours. I call it Blinking Lights.

In order to create this animation, I would have to figure out two major things:

  • How to make characters blink.
  • How to change the colours of characters.

It turned out they were not as difficult as I expected.

Making characters blink

To make the text on the screen blink, I had to:

  • First write the text to the console output.
  • Make the current thread sleep for a second.
  • Clear the text using the backspace character ( '\b' ). If there are five characters on the screen, I would have to write the backspace character five times to clear them all.
  • Write the new text to the screen.

To make the thread sleep, I had to make an OS call. I am using Windows so I had to #include <windows.h> , then call Sleep(1000); to make the thread sleep for 1 second (1000 milliseconds).

Here’s the function I wrote to clear the screen:

clearScreen() function

It accepts an argument which specifies the number of characters to clear, then it prints the backspace character the same number of times in order to clear all the characters.

Changing the colour of the console text

To change the colour of text in the console, you need to set the console text attribute to a numerical value between 0 and 15, with 0 for black, 1 for blue, 2 for green and so on to 15 for white. The colours I used in this animation were:

Colour codes

and this is the function I wrote to change the colour of the console text:

changeColour() function

It accepts a colour value as an integer and sets the console text attribute to the corresponding colour.

Putting everything together

Once I had figured out how to make the text blink and change the console text colour, the rest of the code was easy to write. I wrote a function called showLoadingScreen() to show the text “- - -” followed by the text “* * *” three times. Each piece of text would stay on the screen for one second, then disappear to allow the other show. Also, I alternated the colour of the “* * *” text from red to green to blue, while keeping the “- - -” text white. This made the text blink and change colours at the same time. The function showLoadingScreen() is shown below:

showLoadingScreen() function

Next, I wrote a function called showBlinkingLights() to show the text “BLINKING LIGHTS” with “BLINK” appearing first in red, then “ING” added to it in green, then “ LIGHTS” added to the console output in blue:

showBlinkingLights() function

Finally, I put it all together in the main() function:

main() function

The output is:

Blinking Lights animation

I have successfully created a console animation. Not on the level of the Star Wars movie, yes, but it’s a start. The full code for this project is available here.

Thanks for following this far. If you have any comments or feedback on the project or this article, please drop them below.

--

--