SDL Gamer

Search this site

Simple Case‎ > ‎

Exp3. The Tracks By A Moving Object

Show a way to record the tracks.   You can use this trick on Exp 2 as a practice.

ATest_sEx3.zip 665k

Screenshot

Program Code
 // Declare & Define a class _Track as "tk"
class _Track{
    public:
    short x[100] , y[100]; // Record 100 sets of track(x[0]~x[99])
    SDL_Rect dot; // For FillRect
    } tk;


while(bRun) {
    // If the object moved , then Record the track
    if( animRect.x + 50 != tk.x[0] || animRect.y + 90 != tk.y[0] ) {
        // Push the last one track record out
        for(int i=99 ; i > 0 ; i--) {
            tk.x[i] = tk.x[i-1] ;
            tk.y[i] = tk.y[i-1] ;
            }
        tk.x[0] = animRect.x + 50; // Put the newest data in
        tk.y[0] = animRect.y + 90;
        } // if( animRect.x != tk.x[0] || animRect.y != tk.y[0] ) { END
    // Drawing the tracks on Screen
    for(int i = 0 ; i<100 ; i++) {
        tk.dot.w = tk.dot.h = 4; // Set the dot size to 4
        tk.dot.x = tk.x[i] - 2; // Make the dot in the center
        tk.dot.y = tk.y[i] - 2;
        SDL_FillRect(screen , &tk.dot , 0xFF1122);
        }
} // while(bRun) END

Did you notice something wrong in this program code?
The hint about this :
        Always initialize the variables before you using them.

This is a very good example for this !

Exp3 END

Wow , this example is so simple that I can not believe that !
Maybe I should start from some thing interesting.


Copyright © 2009 bdragon All rights reserved.
bdragong@gmail.com
Č
ċ
ď
Bdragon Ho,
Aug 17, 2009, 10:38 PM
Comments