Allegro Pong Collision Problems
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();