Code:
#include <allegro.h>
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
void Check_Null(BITMAP* Picture, string Type, string Error);
int Get_Rand(void);
int main(void){
cout << "This is a Pong game by Lorgon Jortle. Mar 31, 2009.\n";
cin.get();
allegro_init();
install_keyboard();
install_timer();
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
/**Declare variables**/
//Bitmaps
BITMAP* buffer = NULL;
BITMAP* player1 = NULL;
BITMAP* player2 = NULL;
BITMAP* ball = NULL;
//Integers
int player1_x, player1_y, player2_x, player2_y;
int player1_score, player2_score;
int ball_x, ball_y;
int ball_speed, ball_direction; //Ball direction 1 = left, 2 = left up, 3 = left down, 4 = right, 5 = right up, 6 = right down
int player1_bb_bottom, player1_bb_right, player1_bb_top, player1_bb_left;
int player2_bb_bottom, player2_bb_left, player2_bb_top, player2_bb_right; //The lines on the side of the paddle to check for collisions
int ball_bb_top, ball_bb_right, ball_bb_left, ball_bb_bottom;
//Booleans
bool Collision = false;
bool Show_Box = false; //For error testing with collision
/**Assign variables**/
//Bitmaps
buffer = create_bitmap(640, 480);
Check_Null(buffer, "buffer", "buffer failed to load.");
player1 = load_bitmap("player1.bmp", NULL);
Check_Null(player1, "player1.bmp", "player1.bmp failed to load.");
player2 = load_bitmap("player2.bmp", NULL);
Check_Null(player2, "player2.bmp", "player2.bmp failed to load.");
ball = load_bitmap("ball.bmp", NULL);
Check_Null(ball, "ball.bmp", "ball.bmp failed to load.");
//Integers
player1_x = 0;
player1_y = 240;
player2_x = 630;
player2_y = 240;
ball_x = 300;
ball_y = 300;
player1_score = 0;
player2_score = 0;
player1_bb_bottom = (player1_y + player1->h);
player1_bb_right = (player1_x + player1->w);
player1_bb_left = player1_x;
player1_bb_top = player1_y;
player2_bb_bottom = (player2_y + player2->h);
player2_bb_left = player2_x;
player2_bb_top = player2_y;
player2_bb_right = (player2_x + player2->w);
ball_bb_left = ball_x;
ball_bb_bottom = (ball_y + ball->h);
ball_bb_top = ball_y;
ball_bb_right = (ball_x + ball->w);
ball_speed = 1;
ball_direction = Get_Rand();
while(!key[KEY_ESC]){
//Draw the ball 1 = left, 2 = left up, 3 = left down, 4 = right, 5 = right up, 6 = right down
if(ball_direction == 0){
ball_x = ball_y = 300;
}
if(ball_direction == 1){
ball_x -= ball_speed;
}
if(ball_direction == 2){
ball_x -= ball_speed;
ball_y -= ball_speed;
}
if(ball_direction == 3){
ball_x -= ball_speed;
ball_y += ball_speed;
}
if(ball_direction == 4){
ball_x += ball_speed;
}
if(ball_direction == 5){
ball_x += ball_speed;
ball_y -= ball_speed;
}
if(ball_direction == 6){
ball_x += ball_speed;
ball_y += ball_speed;
}
draw_sprite(buffer, ball, ball_x, ball_y);
//Accept input
if(key[KEY_UP]){
if(player2_y < 0){
player2_y = 480;
}
player2_y -= 3;
}
if(key[KEY_DOWN]){
if(player2_y > 480){
player2_y = 0;
}
player2_y += 3;
}
if(key[KEY_W]){
if(player1_y < 0){
player1_y = 480;
}
player1_y -= 3;
}
if(key[KEY_S]){
if(player1_y > 480){
player1_y = 0;
}
player1_y += 3;
}
if(key[KEY_SPACE]){
Show_Box = true;
}
else if (!key[KEY_SPACE]){
Show_Box = false;
}
if(key[KEY_R]){
ball_x = ball_y = 300;
ball_direction = Get_Rand();
}
/*
LEAVE ROOM FOR FURTHER KEY PRESSES
*/
//Update bounding boxes
player1_bb_bottom = (player1_y + player1->h);
player1_bb_right = (player1_x + player1->w);
player2_bb_bottom = (player2_y + player2->h);
player2_bb_left = player2_x;
ball_bb_left = ball_x;
ball_bb_bottom = (ball_y + ball->h);
ball_bb_top = ball_y;
ball_bb_right = (ball_x + ball->w);
//Done
//Draw players and ball
draw_sprite(buffer, player1, player1_x, player1_y);
draw_sprite(buffer, player2, player2_x, player2_y);
draw_sprite(buffer, ball, ball_x, ball_y);
//Done
if(Show_Box == true){
line(buffer, player1_bb_right, player1_y, player1_bb_right, player1_bb_bottom, makecol(255, 0, 0)); //Player1
line(buffer, player2_bb_left, player2_y, player2_bb_left, player2_bb_bottom, makecol(255, 0, 0)); //Player2
line(buffer, ball_bb_right, ball_y, ball_bb_right, ball_bb_bottom, makecol(255, 0, 0)); //Player1
line(buffer, ball_bb_left, ball_y, ball_bb_left, ball_bb_bottom, makecol(255, 0, 0));
}
/**Collision testing**/
Collision = false;
if((ball_y - 25) >= (player2_y - 50) && ball_y <= player2_y){
if(ball_bb_right >= player2_bb_left){
Collision = true;
}
}
else if((ball_y - 25) >= (player1_y - 25) && ball_y <= player1_y){
if(ball_x <= player1_bb_right){
Collision = true;
}
}
if(Collision == true){
if(ball_direction >= 4){
ball_direction = 1;
}
else if(ball_direction <= 3){
ball_direction = 4;
}
}
/**Collision with top and bottom**/ //1 = left, 2 = left up, 3 = left down, 4 = right, 5 = right up, 6 = right down
if(ball_y >= 430){
if(ball_direction == 6){
ball_direction = 5;
}
if(ball_direction == 3){
ball_direction = 2;
}
}
if(ball_y <= 0){
if(ball_direction == 2){
ball_direction == 3;
}
if(ball_direction == 5){
ball_direction = 6;
}
}
/**Check for win**/
if(ball_x > 640){
player1_score += 1;
ball_x = ball_y = 300;
ball_direction = Get_Rand();
}
if(ball_x < 0){
player2_score += 1;
ball_x = ball_y = 300;
ball_direction = Get_Rand();
}
textprintf_ex(buffer, font, 10, 10, makecol(255, 100, 200), -1, "Position: %d, %d", ball_x, ball_y);
textprintf_ex(buffer, font, 500, 10, makecol(255, 100, 200), -1, "Direction: %d", ball_direction);
textprintf_ex(buffer, font, 10, 470, makecol(255, 100, 200), -1, "Score: %d", player1_score);
textprintf_ex(buffer, font, 500, 470, makecol(255, 100, 200), -1, "Score: %d", player2_score);
blit(buffer, screen, 0, 0, 0, 0, 640, 480);
clear_bitmap(buffer);
}
destroy_bitmap(buffer);
destroy_bitmap(player1);
destroy_bitmap(player2);
destroy_bitmap(ball);
return 0;
}
END_OF_MAIN();
void Check_Null(BITMAP* Picture, string Type, string Error){
if(Picture == NULL){
cout << "Error: " << Error << endl;
}
else if(Picture != NULL){
cout << Type << " loaded successfully.\n";
}
}
END_OF_FUNCTION(Check_Null);
int Get_Rand(void){
int goodness;
srand(time(NULL));
goodness = (rand()%6) + 1;
return goodness;
}
END_OF_FUNCTION(Get_Rand);
sometimes the ball changes direction like it's supposed to, but often times it doesn't. I've add the text at the top to show the variables when it's moving, and everything is as it should be... Player2 is the same image as player1, just rename it.