rendered paste body#include <allegro.h>
BITMAP *ball, *bar, *bar2, *buffer;
int BAR_Y = 180, BAR2_Y = 180;
int BALL_X = 315, BALL_Y = 235, P1, P2, speed, play=1;
double MOV_X, MOV_Y;
void update_screen(void)//-----------------------------------------------------------------------------------------------------------
{
blit(buffer,screen,0,0,0,0,640,480);
}//----------------------------------------------------------------------------------------------------------------------------------
void load_images(void)//-------------------------------------------------------------------------------------------------------------
{
buffer = create_bitmap(640,480);
ball=load_bitmap("ball.pcx",NULL);
bar=load_bitmap("bar.pcx",NULL);
bar2=load_bitmap("bar2.pcx",NULL);
}//----------------------------------------------------------------------------------------------------------------------------------
void ball_move(void)//---------------------------------------------------------------------------------------------------------------
{
BALL_X += MOV_X; //modifies the ball's X and Y coordinates for the next draw
BALL_Y += MOV_Y;
if(BALL_X > 625) ++P1, MOV_X = MOV_X*-1; //reverses on right side
if(BALL_X < 5) ++P2, MOV_X = MOV_X*-1; //reverses on left side
if(BALL_Y < 5 || BALL_Y > 465) MOV_Y = MOV_Y*-1; //reverses on top and bottom
if(BALL_Y+15 == BAR_Y && BALL_X >= 5 && BALL_X <= 5+30) MOV_Y = MOV_Y*-1; //collision with top and bottom of left paddle
if(BALL_Y == BAR_Y+120 && BALL_X >= 5 && BALL_X <= 5+30) MOV_Y = MOV_Y*-1;
if(BALL_X == 35 && BALL_Y >= BAR_Y && BALL_Y <= BAR_Y+49) MOV_X = MOV_X*-1, MOV_Y = -1; //collision with top of left paddle
if(BALL_X == 35 && BALL_Y >= BAR_Y+50 && BALL_Y <= BAR_Y+70) MOV_X = MOV_X*-1, MOV_Y = 0; //collision with centre of left paddle
if(BALL_X == 35 && BALL_Y >= BAR_Y+71 && BALL_Y <= BAR_Y+120) MOV_X = MOV_X*-1, MOV_Y = 1; //collision with bottom of left paddle
if(BALL_Y+15 == BAR2_Y && BALL_X >= 605 && BALL_X <= 605+30) MOV_Y = MOV_Y*-1; //collision with top and bottom of left paddle
if(BALL_Y == BAR2_Y+120 && BALL_X >= 605 && BALL_X <= 605+30) MOV_Y = MOV_Y*-1;
if(BALL_X+15 == 605 && BALL_Y >= BAR2_Y && BALL_Y <= BAR2_Y+49) MOV_X = MOV_X*-1, MOV_Y = -1; //collision with top of right paddle
if(BALL_X+15 == 605 && BALL_Y >= BAR2_Y+50 && BALL_Y <= BAR2_Y+70) MOV_X = MOV_X*-1, MOV_Y=0; //collision with centre of right paddle
if(BALL_X+15==605 && BALL_Y >= BAR2_Y+71 && BALL_Y <= BAR2_Y+120) MOV_X = MOV_X*-1, MOV_Y=1; //collision with bottom of right paddle
}//----------------------------------------------------------------------------------------------------------------------------------
void bar_move(void)//----------------------------------------------------------------------------------------------------------------
{
poll_joystick();
if (joy[0].stick[0].axis[1].pos/40 > 0 && BAR_Y < 355) ++BAR_Y; //if key is pressed, changes paddle's Y coordinates for next draw
if (joy[0].stick[0].axis[1].pos/40 < 0 && BAR_Y > 5) --BAR_Y;
}//----------------------------------------------------------------------------------------------------------------------------------
void bar2_move(void)//---------------------------------------------------------------------------------------------------------------
{
poll_joystick();
if (joy[1].stick[0].axis[1].pos/40 > 0 && BAR2_Y < 355) ++BAR2_Y;
if (joy[1].stick[0].axis[1].pos/40 < 0 && BAR2_Y > 5) --BAR2_Y;
}//----------------------------------------------------------------------------------------------------------------------------------
void bufferdraw(void)//--------------------------------------------------------------------------------------------------------------
{
draw_sprite(buffer,bar2,605,BAR2_Y);
draw_sprite(buffer,ball,BALL_X,BALL_Y);
draw_sprite(buffer,bar,5,BAR_Y);
}//----------------------------------------------------------------------------------------------------------------------------------
void random_move(void)//-------------------------------------------------------------------------------------------------------------
{
do //generates a random number from -360 to 360 for MOV_X and MOV_Y to randomly set the ball's direction, will keep randomizing until both are not equal to -1 or 1
{
MOV_X=rand()%3-1;
MOV_Y=rand()%3-1;
}
while(MOV_X==0||MOV_Y==0);
}//----------------------------------------------------------------------------------------------------------------------------------
void score_call(void)//--------------------------------------------------------------------------------------------------------------
{
textprintf_ex(buffer,font,300,5, makecol(255,0,0), -1, "PONG");
textprintf_ex(buffer,font,5,5, makecol(255,255,255), -1, "Player 1: %d", P1);
textprintf_ex(buffer,font,545,5, makecol(255,255,255), -1, "Player 2: %d", P2);
if(P1==10)
{
textprintf_ex(buffer,font,270,200, makecol(255,255,255), -1, "Player 1 wins!"); //displays "Player 1 wins" if P1 gets 10 points
update_screen();
rest(5000);
P1=0, P2=0, BALL_X = 315, BALL_Y = 235;
random_move();
}
if(P2==10)
{
textprintf_ex(buffer,font,270,200, makecol(255,255,255), -1, "Player 2 wins!"); //displays "Player 2 wins" if P2 gets 10 points
update_screen();
rest(5000);
P1=0, P2=0, BALL_X = 315, BALL_Y = 235;
random_move();
}
}//----------------------------------------------------------------------------------------------------------------------------------
int main()//-------------------------------------------------------------------------------------------------------------------------
{
allegro_init(); //starts allegro, installs the joystick and keyboard, sets colour and graphics mode
install_keyboard();
install_joystick(JOY_TYPE_AUTODETECT);
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640,480, 0,0);
srand(time(0));
random_move();//sets ball direction
load_images();//loads the images
acquire_screen;
while(!key[KEY_ESC]) //keeps cycling until ESC is pressed
{
clear(buffer); //clears the buffer
if(speed%2==0) //slows movement to every other cycle
{
bar_move();
bar2_move();
ball_move();
}
bufferdraw(); //draws coordinates to the buffer
score_call(); //brings up the score
update_screen(); //blits the buffer to the screen
speed++;
}
release_screen;
readkey();
return 0;
}//----------------------------------------------------------------------------------------------------------------------------------
END_OF_MAIN();