Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / Win32

Fun with the Console, Animating the American Flag (Visual C++ 6.0)

2.70/5 (9 votes)
5 Jun 2010CPOL2 min read 31.1K   482  
Have some fun with the console, animate the American flag
Please maximize the console window to rectify any overdraw when running the EXE.

Image 1

Introduction

This is one of those projects you did just because you thought it would be fun to do and you had some spare time left. Well, let's animate some flags, shall we...

First we create a new project and class, let's name our class CFunWithConsole and put some variables and functions in it.

The project files are:

CManCFunWithConsole_main.cppThe main file
CFunWithConsole.cppThe class definition and method body file
CFunWithConsole.hThe class declaration file
CTextMode.hThe Console textmode graphics class

The CFunWithConsole Class

Ok, our class CFunWithConsole will inherit some graphics function methods like draw lines, boxes and circles from the class CTextMode so we need to include this syntax:

C++
CMandelzoom::CMandelzoom( int i) : CTextMode()

Now after the inheritance the function methods in CTextMode.h have become "one" with the function methods in our class CMandelzoom. We can now treat those methods as if they were native to CFunWithConsole. Now we can draw lines, boxes and circles.
We have also placed variables we want to be initialized at start up in the constructor so that when our class is being instantiated, those variables get initialized.

The Class Header File CFunWithConsole.h

And now the application itself. We choose not to define and embody our member functions in the header file, we will just put the prototypes in CFunWithConsole.h.

C++
#ifndef CFunWithConsole_H
#define CFunWithConsole_H

#include "CTextMode.h"

class CFunWithConsole: public  CTextMode
{
	public:

		int shiftX[9];
		int shiftY[8];
		int addX[9];
		int addY[8];

		CFunWithConsole();
		CFunWithConsole( int i);
		~CFunWithConsole();
		void animateFlag(void);
		void VersionInfo(void);

};

#endif CFunWithConsole_H

The Class Definition File CFunWithConsole.cpp

In CFunWithConsole.cpp, we will define and embody our member functions with function calls, syntaxes and statements. Our class CFunWithConsole inherits function methods from the class CTextMode so we need to include this syntax:

C++
CFunWithConsole::CFunWithConsole( int i) : CTextMode() 

The Constructor

We have placed function calls we want to be initialized at start up in the constructor so that when our class is being instantiated, those function methods get called and executed. We have also placed a while(true) loop that never exits, so that our flag will animate indefinitely.

C++
// CFunWithConsole.cpp

CFunWithConsole::CFunWithConsole( int i) :  CTextMode()   
{       //variables to be initialized
	shiftX[0]=50;shiftX[1]=51;shiftX[2]=52;shiftX[3]=52; //curved alignment for flag
	shiftX[4]=52;shiftX[5]=52;shiftX[6]=51;shiftX[7]=50;
	shiftX[8]=53;

	shiftY[0]=15;shiftY[1]=16;shiftY[2]=17;shiftY[3]=18;
	shiftY[4]=19;shiftY[5]=20;shiftY[6]=21;shiftY[7]=21;

	addX[0]=1;addX[1]=1;addX[2]=1;addX[3]=1;
	addX[4]=1;addX[5]=1;addX[6]=1;addX[7]=1;
	addX[8]=1;

	addY[0]=1;addY[1]=1;addY[2]=1;addY[3]=1;
	addY[4]=1;addY[5]=1;addY[6]=1;addY[7]=1;

	 while(true)          //eternity loop
        {           
           animateFlag(); 
        }
   }

The animateFlag() Method

The method animateFlag() animates the flag by simply shifting a block of bytes back and forth. The bytes that are being moved have been aligned in a curved fashion in the constructor prior to this function call. The blocks have also received a color scheme which is the same color scheme as the flag.

C++
void CFunWithConsole::animateFlag(void)
{
	  for (int ii=0;ii<9;ii++)
	  {
		  shiftX[ii]=shiftX[ii]+addX[ii];
		  if (shiftX[ii]<50 || shiftX[ii]>52) addX[ii]=-addX[ii];
	  }

           txtLine(50,5,50 ,22,15);     //draw flag pole

	   txtLine(1,22,78,22,10);      //draw green grass
	   txtLine(1,23,78,23,10);

	   clrbox(shiftX[0],shiftY[0],shiftX[0]+20,shiftY[0]+1,79);
	   clrbox(shiftX[1],shiftY[1],shiftX[1]+20,shiftY[1]+1,254);
	   clrbox(shiftX[2],shiftY[2],shiftX[2]+20,shiftY[2]+1,79);
	   clrbox(shiftX[3],shiftY[3],shiftX[3]+20,shiftY[3]+1,254);
	   clrbox(shiftX[4],shiftY[4],shiftX[4]+20,shiftY[4]+1,79);
 	   clrbox(shiftX[5],shiftY[5],shiftX[5]+20,shiftY[5]+1,254);
 	   clrbox(shiftX[6],shiftY[6],shiftX[6]+20,shiftY[6]+1,79);

	   clrbox(shiftX[0]+0,shiftY[0],shiftX[0]+6,shiftY[0]+1,31);
	   clrbox(shiftX[1]+0,shiftY[1],shiftX[1]+6,shiftY[1]+1,31);
	   clrbox(shiftX[2]+0,shiftY[2],shiftX[2]+6,shiftY[2]+1,31);
	   clrbox(shiftX[3]+0,shiftY[3],shiftX[3]+6,shiftY[3]+1,254);
	   clrbox(shiftX[4]+0,shiftY[4],shiftX[4]+6,shiftY[4]+1,79);
	   clrbox(shiftX[5]+0,shiftY[5],shiftX[5]+6,shiftY[5]+1,254);
	   clrbox(shiftX[6]+0,shiftY[6],shiftX[6]+6,shiftY[6]+1,79);

	   clrbox(shiftX[0]+3,shiftY[0],shiftX[0]+4,shiftY[0]+1,31);
	   clrbox(shiftX[1]+3,shiftY[1],shiftX[1]+4,shiftY[1]+1,31);
	   clrbox(shiftX[2]+3,shiftY[2],shiftX[2]+4,shiftY[2]+1,31);
	   clrbox(shiftX[3]+3,shiftY[3],shiftX[3]+4,shiftY[3]+1,254);
	   clrbox(shiftX[4]+3,shiftY[4],shiftX[4]+4,shiftY[4]+1,79);
	   clrbox(shiftX[5]+3,shiftY[5],shiftX[5]+4,shiftY[5]+1,254);
	   clrbox(shiftX[6]+3,shiftY[6],shiftX[6]+4,shiftY[6]+1,79);

	   delay(50);
	   setcolor(15);

           clrscr();
}

The Main File

Finally the file CFunWithConsole_main.cpp is where we create an instance for our class and run our application.

C++
#include "CFunWithConsole.h"

int main()
{
  CFunWithConsole *animateFlag = new CFunWithConsole(1);

  return 0;
}

The Console's color codes are as follows:

Black0
Blue1
Green2
Cyan3
Red4
Magenta5
Yellow6
White7
Gray8
LightBlue9
LightGreen10
LightCyan11
LightRed12
LightMagenta13
LightYellow14
BrightWhite15

Thanks for reading.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)