- 1). Launch the compiling program of your choice and click "File," "New" and select "Project." Choose "Console Application" for the file type and give it a name.
- 2). Type in the code to set up the player information, ball tracking and object creation. It should look like this:
#include <allegro.h>
#include <cstdlib>
#include <time.h>
int ball_x = 320;
int ball_y = 240;
int p1_x = 20;
int p1_y = 210; - 3). Create the basic elements of the game by writing if-else code statements for each player and event, deciding on what happens if the ball hits the paddle or is missed. If-else statements will look something like this:
void moveBall(){
ball_tempX = ball_x;
ball_tempY = ball_y;
if (dir == 1 && ball_x > 5 && ball_y > 5){
if( ball_x == p1_x + 15 && ball_y >= p1_y && ball_y <= p1_y + 60){
dir = rand()% 2 + 3;
}else{
--ball_x;
--ball_y;
} - 4). Write the code to give the game graphic elements like a background screen, ball color, and screen size, which will look like this:
}
acquire_screen();
circlefill ( buffer, ball_tempX, ball_tempY, 5, makecol( 0, 0, 0));
circlefill ( buffer, ball_x, ball_y, 5, makecol( 128, 255, 0));
draw_sprite( screen, buffer, 0, 0);
release_screen();
rest(5);
} - 5). Write the code for player turns using the code lines "void p1Move()" and "void p2Move()".
- 6). Write the code that informs program when a player wins and when to start a new game. The "void checkWin()" statement combined with an if-else loop will make the game continue going until one player reaches the max score. The "void startNew()" will tell the program to start over again.
- 7). Add the command "END_OF_MAIN();" to complete the program. Save and launch the program to play a game of pong.
SHARE