/////////////////////////////////////////////////////////////////////////// // // Program: curses_intro.cpp // Description: Introduction to the use of the pdcurses // text interface // /////////////////////////////////////////////////////////////////////////// /* INSTRUCTIONS: END OF INSTRUCTIONS */ // Header Files ///////////////////////////////////////////////////////// // #include // for console I/O #include // for curses interface using namespace std; // // Global Constant Definitions ////////////////////////////////////////// // const char BALL = 'O'; const char POUND_SIGN = '#'; const char SPACE = ' '; // // Class Definitions //////////////////////////////////////////////////// // ///// NONE // // Global Function Prototypes /////////////////////////////////////////// // void startCurses(); void endCurses(); // // Main Program Definition ////////////////////////////////////////////// // int main() { // // Initialize Program //////////////////////////////////////////////// // // initialize variables int response; int xPos = 0, yPos = 0, xCtr, yCtr, xMax, yMax; int minXPos, minYPos, maxXPos, maxYPos; // initialize curses startCurses(); // get the y and x xscreen limits getmaxyx( stdscr, yMax, xMax ); // paint the perimeter for( xCtr = 0; xCtr < xMax; xCtr++ ) { mvaddch( 0, xCtr, POUND_SIGN ); mvaddch( yMax - 1, xCtr, POUND_SIGN ); } for( yCtr = 0; yCtr < yMax; yCtr++ ) { mvaddch( yCtr, 0, POUND_SIGN ); mvaddch( yCtr, xMax - 1, POUND_SIGN ); } // find the center of the screen xPos = xMax / 2; yPos = yMax / 2; // set the y and x minima and maxima minYPos = 1; minXPos = 1; maxYPos = yMax - 2; maxXPos = xMax - 2; // place the first character mvaddch( yPos, xPos, BALL ); // // Input Data //////////////////////////////////////////////////////// // while( response != 'Q' && response != 'q' ) { response = getch(); switch( response ) { case 'w': case 'W': if( yPos > minYPos ) { mvaddch( yPos, xPos, SPACE ); yPos--; } break; case 'a': case 'A': if( xPos > minXPos ) { mvaddch( yPos, xPos, SPACE ); xPos--; } break; case 's': case 'S': if( yPos < maxYPos ) { mvaddch( yPos, xPos, SPACE ); yPos++; } break; case 'd': case 'D': if( xPos < maxXPos ) { mvaddch( yPos, xPos, SPACE ); xPos++; } break; } mvaddch( yPos, xPos, BALL ); } // // Close Program ///////////////////////////////////////////////////// // endCurses(); return 0; // Return 0 to the operating system // to indicate succesful program completion // } // end of main function // // Supporting Function Definition/Implementation ///////////////////////// // void startCurses() { // Initialize the curses library initscr(); // // Enable keyboard mapping keypad(stdscr, TRUE); // // Inhibit converting a newline into a carriage return // and a newline on output nonl(); // // Accept input characters without the [ENTER] key cbreak(); // // Inhibit input echoing // (i.e., key pressed will not be shown on the screen) noecho(); // // Forces wait until available character // or time delay (of parameter - 0.1 second), // whichever comes first halfdelay(1); } void endCurses() { // Shuts down curses interface endwin(); /* do your non-curses wrapup here */ system("clear"); cout << "All Done!" << endl; system("pause"); exit(0); } // // Preprocessor Instructions //////////////////////////////////////////// //