////////////////////////////////////////////////////////////////////////// // // ball.h -- definition of a Ball class // /////////////////////////////////////////////////////////////////////////// // These preprocessor directives prevent the text from being // #include'd more than once from different files #ifndef MY_BALL_H #define MY_BALL_H #include "ct_emu.h" #include "utils.h" class Wall; // A forward declaration -- we need to tell the compiler // that "Wall" is not a typo, but a name of a class // whose definition will follow. class Ball { int x; // position int y; int vx; // velocity int vy; const int rad; // radius myRGB color; public: // Generate a ball randomly Ball() : x( RandomLong(100, 300) ), y( RandomLong(100, 300) ), vx( RandomLong(0, 10) ), vy( RandomLong(0, 10) ), rad( RandomLong(2, 10) ), // NOTE: the following line calls the constructor myRGB::myRGB(int,int,int) ! color ( RandomLong( 0, 255 ), RandomLong( 0, 255 ), RandomLong( 0, 255 ) ) { cout << "Created a Ball at " << x << ", " << y ; cout << " with speed " << vx << ", " << vy ; cout << " and radius " << rad << endl; } // generate a Ball with explicitly given coordinates, speed, radius and color. Ball( int x_, int y_, int vx_, int vy_, int rad_ = 10, int r=0, int g=0, int b=0 ) : x( x_ ), y( y_ ), vx( vx_ ), vy( vy_ ), rad( rad_ ), color ( r, g, b ) // calls myRGB::myRGB(int,int,int), see comment above { cout << "Created a Ball at " << x << ", " << y ; cout << " with speed " << vx << ", " << vy ; cout << " and radius " << rad << endl; } void Reset(){ // Randomly reset the coordinates of a ball x = RandomLong(100, 300); y = RandomLong(100, 300); } void Draw(){ SetForeColor( RGB( color.red, color.blue, color.blue ) ); PaintCircle( x, y, rad ); } void Erase(){ SetForeColor( RGB( 255, 255, 255 ) ); PaintCircle( x, y, rad ); } // returns the Rect describing the region occupied by the ball Rect Region() const { return MakeRect( x-rad, y-rad, x+rad, y+rad ); } // The following functions are defined outside, because they are longer // and two of them require knowledge of the class Wall's internals. void Move(); bool Collides( const Wall& w ); bool Collides( const Ball& b ); void Reflect( const Wall& w ); void Reflect( const Ball& b ); }; #endif // MY_BALL_H