Thread: Pong Clone Collision Allegro

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    26

    Pong Clone Collision Allegro

    Hi

    I've been learning some 'Allegro' (a game programming library) for c++. i decided to compile what i'd learned and to try and make a pong clone called 'bat and ball'. ive run into a problem that i have been un-able to fix. when i run this code everything works as it should but the colision on the bat to ball doesn't work. please help:

    Code:
    #include <allegro.h>
    #include <cstdlib>
    
    int BallX = 100;
    int BallY = 100;
    
    int TempX = 100;
    int TempY = 100;
    
    int Dir = 1; //This will keep track of the circles direction
                //1= up and left, 2 = down and left, 3 = up and right, 4 = down and right
    
    int PlayerX = 300;
    int PlayerY = 400;            
    
    int TempPlayerX = 300;
    int TempPlayerY = 400;
    
    void MovePlayer(){
         
         TempPlayerX = PlayerX;
         TempPlayerY = PlayerY;
         
         if (key[KEY_LEFT] && PlayerX > 10) {
                           PlayerX = PlayerX - 5;
                            }
         else if (key[KEY_RIGHT] && PlayerX < 570) {
                                 PlayerX = PlayerX + 5;
                                 }
         acquire_screen();
         rectfill( screen, TempPlayerX, TempPlayerY, (TempPlayerX + 50), (TempPlayerY + 20), makecol ( 0, 0, 0));
         rectfill( screen, PlayerX, PlayerY, (PlayerX + 50), (PlayerY + 20), makecol ( 255, 255, 255));
         release_screen();
         
         }                        
         
    
    
    void MoveBall(){
    
        TempX = BallX;
        TempY = BallY;
        
        if (Dir == 1 && BallX != 10 && BallY != 20 && (BallX < PlayerX || BallX > PlayerX + 50)) {
         
            BallX = BallX - 1;
            BallY = BallY - 1;
                  
        } else if (Dir == 2 && BallX != 20 && BallY != 460) {
    
            BallX = BallX - 1;
            BallY = BallY + 1;
    
        } else if (Dir == 3 && BallX != 620 && BallY != 20)  { 
        
            BallX = BallX + 1;
            BallY = BallY - 1;
    
        } else if (Dir == 4 && BallX != 620 && BallY != 460) {
    
            BallX = BallX + 1;
            BallY = BallY + 1;
           
        } else if ((BallX > PlayerX || BallX < (PlayerX + 50)) && BallY == 395) { 
    
            BallY = BallY - 5;
            Dir = rand() % 4 + 1;
            
        }
        
        else {
        Dir = rand() % 4 + 1;
    }     
                 
        
        acquire_screen();
        circlefill ( screen, TempX, TempY, 5, makecol( 0, 0, 0));
        circlefill ( screen, BallX, BallY, 5, makecol( 255, 255, 255));
        release_screen();
        
        rest(10);
    
    }    
    
    int main(){
    
        allegro_init();
        install_keyboard();
        set_color_depth(16);
        set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
        
        while( !key[KEY_ESC]){
         
            MoveBall();
            MovePlayer();
       
        }    
        
        return 0;
    
    }
    END_OF_MAIN();

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Same thing here http://cboard.cprogramming.com/showthread.php?t=97356
    Please don't start multiple threads on the same topic.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [sdl] help with pong collision
    By caffeinated in forum Game Programming
    Replies: 8
    Last Post: 05-14-2008, 01:39 PM
  2. Allegro Pong Collision Problems
    By gillypie in forum Game Programming
    Replies: 4
    Last Post: 12-30-2007, 06:05 AM
  3. Pixel perfect 2D collision detection with Allegro
    By Warlax in forum C++ Programming
    Replies: 1
    Last Post: 06-21-2006, 02:14 PM
  4. Try my pong clone
    By lambs4 in forum Game Programming
    Replies: 13
    Last Post: 07-13-2003, 11:01 PM
  5. Allegro bitmap collision? how?
    By elfjuice in forum Game Programming
    Replies: 5
    Last Post: 07-06-2002, 11:45 AM