Technology Software

How to Make a Pong Game in C++

    • 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
RELATED POSTS on "Technology"
How Do I Print Business Cards With Microsoft Publisher?
How Do I Print Business Cards With Microsoft Publisher?
How to Reduce the Size of Photos Using Adobe Photoshop Elements 3.0
How to Reduce the Size of Photos Using Adobe Photoshop Elements 3.0
Features and Benefits of Project Portfolio Management
Features and Benefits of Project Portfolio Management
Fix Registry - Fix Registry Errors Easily
Fix Registry - Fix Registry Errors Easily
Business Intelligence Solutions and Services
Business Intelligence Solutions and Services
AutoCAD Architecture
AutoCAD Architecture
Best Social Networking Apps
Best Social Networking Apps
IKE
IKE
Help Desk Software by help-desk-software
Help Desk Software by help-desk-software
What's Better - Building a BPM Solution Or Buying One?
What's Better - Building a BPM Solution Or Buying One?
How to Make Music Fade out in Moviemaker
How to Make Music Fade out in Moviemaker
How to Shrink a DVD With Nero Instructions
How to Shrink a DVD With Nero Instructions
Live Chat Software: Four Tips To Help You Get the Most Out of Your Software
Live Chat Software: Four Tips To Help You Get the Most Out of Your Software
How to Stop Rootkits
How to Stop Rootkits
3 First-Rate Foundations for Making Movies
3 First-Rate Foundations for Making Movies
How to Build Game Downloads
How to Build Game Downloads
The Linux Modem How-To
The Linux Modem How-To
Text-Terminals on Linux - 11.6 Terminal Server Connection
Text-Terminals on Linux - 11.6 Terminal Server Connection
The Linux Loadable Kernel Module How-To
The Linux Loadable Kernel Module How-To
How to Create Photo Albums With CSS
How to Create Photo Albums With CSS

Leave Your Reply

*