#include "ct_emu.h" // includes all ct emulator files // int KeyDown() returns (immediately) the ASCII code of the key held // down at the moment of the function was called, or NO_KEY_PRESSED // if no key was down at the moment of the call. // // These are the key codes, defined in ct_windows.h . You // should always use the uppercase symbolic names, the actual // integer values are just for your information. // const int NO_KEY_PRESSED = 0; // const int SHIFT_KEY = 16; // const int CTRL_KEY = 17; // const int SPACE_BAR = 32; // const int RIGHT_ARROW = 39; // const int UP_ARROW = 38; // const int LEFT_ARROW = 37; // const int DOWN_ARROW = 40; // // NOTE: The keys in the following example work only when the // graphics window is active. Initially, the console window // is the active one, not the graphics window. // You need to click the mouse inside the graphics window // to make it active, so that the keys work (the graphics // window will then be the one receiving the Windows events // generated by pressing the keys). // DEMO 1: // The mote's direction of movement is determined by the arrow keys, // the speed remains the same, SPACE interrups the loop. When at a // screen edge, the mote wraps around to the opposite edge. int main(){ BigSquarePair(); int x = 200, y = 200, r = 3; int vx = 0, vy = 0; cout << "Direct the mote with arrow keys,\n"; cout << "Hit SPACE to finish\n\n"; int keycode = NO_KEY_PRESSED; // will store the ASCII code of the key pressed while( keycode != SPACE_BAR ){ keycode = KeyDown(); // obtain the code of the key being pressed now switch( keycode ){ case LEFT_ARROW: vx = -2; break; case RIGHT_ARROW: vx = 2; break; case UP_ARROW: vy = -2; break; case DOWN_ARROW: vy = 2; break; default: // this case includes NO_KEY_PRESSED, so movements stops vx = vy = 0; // as soon as an arrow key is released. break; } SetForeColor( 255, 255, 255 ); // erase mote PaintCircle( x, y, r ); x += vx; // update position y += vy; if( x < 0 ) // wrap around the screen if going x = 400; // out of the borders else if( x > 400 ) x = 0; if( y < 0 ) y = 400; else if( y > 400 ) y = 0; SetForeColor( 0, 0, 255 ); // draw mote PaintCircle( x, y, r ); Delay( 50 ); } PressReturn(); return 0; }