Thread: Allegro bitmap collision? how?

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    100

    Allegro bitmap collision? how?

    I am making a pong game and this is what i got so far

    Code:
    #include <allegro.h>
    
    int x = 5;
    int y = 0;
    int counter;
    int down = 0, up = 0;
    int x2 = 615;
    int y2 = 0;
    int counter2;
    int down2 = 0, up2 = 0;
    int play1scor = 0;
    int play2scor = 0;
    int ballx = 300, bally = 190;
    
    void update_counter()
    {
         counter++;
    }
    END_OF_FUNCTION(update_counter);
    
    void update_counter2()
    {
         counter2++;
    }
    END_OF_FUNCTION(update_counter2);
    
    int main()
    {
       allegro_init();
       install_keyboard();
       
       LOCK_VARIABLE(counter);
       LOCK_FUNCTION(update_counter);
       install_int_ex( update_counter, BPS_TO_TIMER(60));
       
       LOCK_VARIABLE(counter2);
       LOCK_FUNCTION(update_counter2);
       install_int_ex( update_counter2, BPS_TO_TIMER(60));
    
       set_color_depth(32);
       set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0);
    
       BITMAP *bar = load_bitmap( "sprite/bar.bmp", NULL );
       BITMAP *logo = load_bitmap( "sprite/logo.bmp", NULL );
       BITMAP *ball = load_bitmap( "sprite/ball.bmp", NULL );
       
       while ( !key[KEY_ESC] )
       {
       
            while ( counter > 0 ) 
            { 
                if (down)  y-=7;
                if (up) y+=7;
                counter--; 
            }
            if ( y > 0 && key[KEY_UP])
                down = 1;
            else 
                down = 0; 
            if ( y < 375 && key[KEY_DOWN])
                up = 1;
            else 
                up = 0; 
            while ( counter2 > 0 ) 
            { 
                if (down2)  y2-=7;
                if (up2) y2+=7;
                counter2--; 
            }
            if ( y2 > 0 && key[KEY_A])
                down2 = 1;
            else 
                down2 = 0; 
            if ( y2 < 375 && key[KEY_Z])
                up2 = 1;
            else 
                up2 = 0;
     
            vsync();
            clear_to_color (screen, 0);
            draw_sprite ( screen, bar, x, y );
            draw_sprite ( screen, bar, x2, y2 );
            draw_sprite ( screen, logo, 245, 200 );
            draw_sprite ( screen, ball, ballx, bally );
            
       }
    
    
       destroy_bitmap(bar);
       destroy_bitmap(logo);
       allegro_exit();
       return 0;
    }
    END_OF_MAIN();
    how do i make the ball move and... you know... do what the pong ball does?

    thanks

  2. #2
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    That is not really a bitmap collision question . Umm, Your code is hard to understand, this is how I would do a pong game:

    Code:
    while(!quit)
    {
      getuserinput();    // Get the input, left, right, or up and down
      parseinput();       // Update the coordinates of the paddles
      updateball();       // Have this function update the coordinates of the ball
      drawscreen();
    }
    You could combine getuserinput and parseinput. Update ball should detect if the ball has hit a paddle, and have it change direction accordingly. I do not know about the calculations and stuff needed in pong, but it should handle all that. drawscreen should display the screen, the start the cycle over.

  3. #3
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    Just to let you know your bitmaps have to be likes this:

    BITMAP *bar = load_bitmap( "sprite//bar.bmp", NULL );

    instead of

    BITMAP *bar = load_bitmap( "sprite/bar.bmp", NULL );


    notice the extra / i have in there...well you have to have two in there.

  4. #4
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    I thought it was a backslash

    load_bitmap("c:\\images\\bitmap.bmp");

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    100
    Ok... lol... i get this a lot... but this is my <b>first</b> day of allegro... so don't expect me to know anything

    I'll try to make my code a little cleaner/better.

    thanks

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    208

    destroy

    u might want to destroy all your bitmaps too eh
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitmap Collision
    By Ti22 in forum Game Programming
    Replies: 6
    Last Post: 04-11-2005, 07:08 AM
  2. Game Programming FAQ
    By TechWins in forum Game Programming
    Replies: 5
    Last Post: 09-29-2004, 02:00 AM
  3. OpenGL -- Bitmaps
    By HQSneaker in forum Game Programming
    Replies: 14
    Last Post: 09-06-2004, 04:04 PM
  4. double buffering for allegro
    By Leeman_s in forum C++ Programming
    Replies: 6
    Last Post: 09-12-2002, 02:45 PM
  5. bitmap in allegro
    By pode in forum Game Programming
    Replies: 5
    Last Post: 03-30-2002, 10:06 AM