gamecode

This is a discussion on gamecode within the Game Programming forums, part of the General Programming Boards category; Sure. This example demonstrates cleaning up behind the pixel and not cleaning up behind the pixel. The principle is the ...

  1. #16
    Registered User
    Join Date
    Oct 2001
    Posts
    375
    Sure. This example demonstrates cleaning up behind the pixel and not cleaning up behind the pixel. The principle is the same for a line. It also has a delay in the loop so that the animation slows down. Try playing around with it to see the differences.

    Red pixel moves, blue pixel moves but leaves a tail. Press ESCape to quit.

    PHP Code:
    #include <allegro.h>

    int x;  // X coordinate of our pixels.

    int main() 

     
    // Initialize Allegro.        
     
    allegro_init();      

     
    // Set the color depth to 32 bit.
     
    set_color_depth(32);

     
    // Set the resolution to 640 by 480 with SAFE autodetection.
     
    set_gfx_mode(GFX_SAFE64048000);

     
    // Installing the keyboard handler.
     
    install_keyboard();

     
    // Printing text to the screen.
     
    textout(screenfont"Moving Pixel:"11makecol(255,0,0));
     
    textout(screenfont"Growing line:"122makecol(0,0,255));

     
    // Looping until the ESCape key is pressed.
     
    while(! key[KEY_ESC])
       {
       
    // Erase old moving pixel.
       
    putpixel(screenx-1120);
       
    // Draw new moving pixel.
       
    putpixel(screenx12makecol(255,50,50));
       
    // Draw growing line from pixels.
       
    putpixel(screenx32makecol(50,50,255));
       
    x++;
       if(
    x>640)
        
    x=0;
       
    rest(2); // Slow down animation.
       
    }
     
    // Exit program.
     
    allegro_exit();
     return 
    0;     
    }     

    // Some Allegro magic to deal with WinMain().
    END_OF_MAIN(); 
    Allegro precompiled Installer for Dev-C++, MSVC, and Borland: http://galileo.spaceports.com/~springs/

  2. #17
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    i tried something like that with if(key[KEY_UP])

    putpixel(....);
    but the pixel wont move by itself
    if i hold down the key then it will start moving
    u know how to make it move with just one hit on the key

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    375
    Here's some rough code to do the trick. Mind you this has the opposite effect that once it starts moving, it doesn't stop. Not difficult to see how to modify it to stop after a while though, or once they press another key, but I'll leave that to your imagination.

    PHP Code:
    bool ShouldPixelMove=0// Pixel should not move at first.

    if(key[KEY_UP])
     
    ShouldPixelMove=1// Pixel should move now.

    if(ShouldPixelMove)
     
    pixelY++; 
    Allegro precompiled Installer for Dev-C++, MSVC, and Borland: http://galileo.spaceports.com/~springs/

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21