#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 2:  
//
//        LEFT_ARROW and RIGHT_ARROW make the mote turn 90 degrees
//         to the left or to the right w.r.t. present course.
//        UP_ARROW accelerates, DOWN_ARROW decelerates

int main(){

    BigSquarePair();
    
    int x = 200, y = 200, r = 3;
    
    
    // The absolute value of the speed 
    int v = 0;
    
    // Directional components of the speed. At any given time 
    // one of these is 1 or -1, the other is 0, so there is
    // no diagonal movement.
    int vx = 2, vy = 0;    // start moving right  

    int nvx = vx, nvy = vy;          // new vx and vy 

    cout << "Direct the mote with arrow keys,\n";
    cout << "Hit SPACE to finish\n\n";

    int keycode = NO_KEY_PRESSED,  prev_keycode = NO_KEY_PRESSED;	

    while( keycode != SPACE_BAR ){
        keycode = KeyDown();       // obtain the code of the key being pressed now           

        if( prev_keycode != keycode ){

            switch( keycode ){  
            case LEFT_ARROW:      // turn counter-clockwise
                nvx = vy;
                nvy = -vx;
                break;
            case RIGHT_ARROW:     // turn clockwise
                nvx = -vy;
                nvy = vx;
                break;
            case UP_ARROW:        // accelerate
                v += 1;
                break;
            case DOWN_ARROW:      // decelerate
                v -= 1;
                if( v < 0 )       // needed so that the mote does not start back
                    v = 0;
                break;
            default:
                // do nothing for all other keys and NO_KEY_PRESSED 
                break;
            }
            	           	    
            prev_keycode = keycode;
        }

        SetForeColor( 255, 255, 255 );
        PaintCircle( x, y, r );   // erase the mote

        int x0 = x, y0 = y;

        vx = nvx;                 // update speeds
        vy = nvy;
        x += v * vx;              // compute new coords
        y += v * 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 );  
        PaintCircle( x, y, r );   // draw the mote

        Delay( 70 );
    }

    PressReturn();

    return 0;
}

 
