Mildly entertaining extras

Just since playing with ascii terminals isn't all that exciting, here are some functions that let you
  • get keyboard input without making the user hit enter
  • change the colour of text and background on the terminal window
  • figure out how big the terminal window currently is
  • jump the cursor to a specific point in the window
  • pause the program for a fixed number of seconds
  • clear the screen
The code is provided through a header file, effects.h

To include the set of functions in a program, just make sure you copy the effects.h file to the same directory as the rest of your code, and in your .cpp or .C file add
#include "effects.h"

The functions it provides are:

  • keyquick: to detect if a key has been pressed and what it was
  • clear: to clear the screen
  • sysPause: to pause the program for a set amount of time
  • getTermSize: to find out how big the terminal window is (how many rows/columns of text)
  • moveCursor: to jump the cursor directly to a specific row, column
  • chgText: to change the colour of (future) text
  • chgBack: to change the colour of the background
  • chgFormat: to enable/disable bold, underline, etc
 

Example:

Here are more details on the various functions:

// The sample program that produces the image at the top of the page
#include "effects.h"
#include <iostream>
using namespace std;

int main()
{
   clear();

   chgText(Red);
   cout << "\nEXPERIMENTING WITH ";
   chgFormat(Underline);
   cout << "EFFECTS";
   chgFormat(DefForm);
   cout << endl << endl;

   chgText(Blue);
   long height, width;
   getTermSize(height, width);
   cout << "The terminal size is: " << height << "," << width << endl;

   chgText(Green);
   cout << "Hit a key when you're ready" << endl;
   char ch;
   int count = 0;
   while (true) {
      if (keyQuick(ch)) break;
      cout << ".";
      cout.flush();
      sysPause(0.1);
      count++;
   }
   cout << "\n\nIt took you " << (0.1*count) << " seconds to hit " << ch << endl << endl;

   chgFormat(Bold);
   cout << "\nBYE NOW!!\n\n";

   chgText(DefCol);
   chgFormat(DefForm);
}

// And a program to throw random X's across your window
#include "effects.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

void drawBlack(long rows, long cols);
void throwDarts(long rows, long cols, long darts);

int main()
{
   long rows, cols;
   getTermSize(rows,cols);
   drawBlack(rows,cols);
   throwDarts(rows,cols, rows*cols);
   chgText(DefCol);
   chgBack(DefCol);
   chgFormat(DefForm);
}

void drawBlack(long rows, long cols)
{
   chgBack(Black);
   clear();
   for (int r = 0; r < rows; r++) {
       for (int c = 0; c < cols; c++) {
           cout << " ";
       }
       cout << "\n";
   }
}

void throwDarts(long rows, long cols, long darts)
{
   srand(time(NULL));
   chgText(Red);
   chgFormat(Bold);
   for (int d = 0; d < darts; d++) {
       sysPause(0.2);
       long r = 1 + rand() % rows;
       long c = 1 + rand() % cols;
       moveCursor(r,c);
       cout << "X";
       cout.flush();
   }
}