OpenGL Ping Pong Game

The following section will deal with the creation of a basic ping-pong application which with the help of keyboard key interactions will be able to make a game. This game consists of two plate like structure and a ball.

The ball must hit one of the player’s plate and bounces to the other player. If any player misses the ball then the score will be incremented in the opponent’s score board. A scoreboard is also maintained to keep track of the score of the both players and any player first to score 10 is declared as “Winner”.

The working of the following application is based on the use keyboard interaction function in OpenGL. These mouse motion functions will be called using OpenGL library function glKeyboardFunc() and glKeyboardUpFunc().

This following program also uses esc key for the interaction between the user and the application and also to close the application.
The following program will use basic functions of OpenGL library and various c++ functions to declare and define functions and variables.

Function used in the OpenGL Ping Pong Game

The functions used in the program namely: main(), Timer(), hm(), keyboardUp(), init(), display() and keyboard().
The program also contains two classes to represent the ball and the plates for each of the player and plates to define that if it is at left or right.


The definition of this function is as follows:

void display() which means it accepts nothing as argument and returns nothing. This function is used to draw the two plates one at the left side and the other at the right side and also used to make ball. This function will also be drawing a common middle line which is a line strip.

This function also has string displayed on the top of the area for the both player which is used to display the score of the corresponding player. For the above string display this function uses OpenGL library function like glRasterPos2f() and glutBitmapCharacter().

The above two functions take care of the following things:

  • The string to be displayed on the display window.
  • The x coordinate where the string is to be positioned.
  • The y coordinate where the string is to be positioned.
  • The argument which we used for the font size.

The x=0 and y=0 will represent the origin which will be at the DOWN-LEFT corner of the display window and as we increment the value of x it move on the RIGHT line axis and similarly of y incrementation it moves UP.

For Example: x=10 y=10 means from the BOTTOM-LEFT corner 10 unit distance along x-axis and y unit distance towards y-axis.Main function for the text rendering in this is the setting of the position

In above (x,y) coordinates it sets the corner position.

The above function actually takes the text cursor and place it on the specified position then after whatever the text or character is displayed, starts to display from that cursor position. Then finally the function used to make string display is glutBitmapCharacter() which takes two arguments.

One of them is the character to be displayed and second is the text render of that character means what is its font family font size etc.

For example: glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, ‘a’); this prints the character ‘a’ with font size 18 and font family HELVETICA.

This function also has a check on the scoLeft and scoRight variables whether it is equal to 5 or not.

If any player’s score exceed 5 then he will be declared with appropriate string displaying on the particular top of the player area. This function is used to call a function named hm() which deals with the displaying of the string “Winner” for the declaration of the winner.


The definition of this function is as follows:
void Timer(int value); means that it does accept value argument but does not return any value.

This function contains first of all some lines of codes which generally means that whenever any of the player’s score hits 5 then the game will be closed or say the window will be destroyed.
For example:

if ((scoLeft == 5) || (scoRight == 5)){
 glutTimerFunc(2000, exit, 0);
 delay = 10000;

The program uses two classes to represent some of the function like the plate and the ball. The second if condition is about when the ball hits the left boundary then the scoRight will be incremented.

For Example: 
if (ball.x < left.x + pt - bsX){
 right.hold = true;

Similarly the third if condition for example:

if (ball.x > right.x - pt + bsX){
 left.hold = true;

Holds when the ball hits the right boundary then the score of the another player will be incremented.
This is a timer function which is set to be called every delay variable which using the init() function is set to 1 millisecond.

The function also uses glutPostRedisplay() to call the display function. The function also uses to call various class member function which takes care of different things which will be covered in the coming sections.


This function is used to set the class variable of the plates class.

example :

 if (key == 'q')
 left.Up = false;
 if (key == 'a')
 left.Down = false;
 if (key == 'p')
 right.Up = false;
 if (key == 'l')
 right.Down = false;

Whenever the key ‘q’ is pressed then the left plate’s Up variable will be set to the false and similarly the others. This function is called when the following keys of the keyboard has state up means user is not pressing the keys.

Whenever the user is not using (pressing) the keyboard keys ‘q’, ‘a’, ‘p’, ‘l’ then this following keyboardUp() function callback function will be fired or called by the main() function. This is also a type of keyboard interaction function.


This function is used for the user interaction with the keyboard when the user have to enter something through the keyboard. This function will be used when the user wants to close the window with the help of an appropriate key on the keyboard. The user is allowed to use esc key of the keyboard.

For Example:

if (key == 27){

This function is also related with the motion of the plates in the game field so that the ball hits the plate and bounces off. The program uses if condition to make the plate move up or down.

For Example:
if (key == 'q')
 left.Up = true;
 if (key == 'a')
 left.Down = true;

So by the following code we mean that if user tries to press key ‘q’ from the keyboard then the motion of the left plate is made upward. Similarly the motion of the left plate is made downward if user presses the key ‘a’. Similar is the condition for the right plate.
For Example:

if (key == 'p')
 right.Up = true;
 if (key == 'l')
 right.Down = true;

This function also uses another if condition for example:

if (key == 'z')
 if (left.hold){
 left.hold = false;
 ball.vx = bsX;

This above condition is used when any user scores then the default state of the ball is set to hold at particular plate and if the hold of the ball is set at the left ball then using the key ‘z’ it will set the hold variable to false and will give it a default speed to the ball.

Similar will be case for the right plate for example:

if (key == ',')
 if (right.hold){
 right.hold = false;
 ball.vx = -bsX;

This above condition is used when any user scores then the default state of the ball is set to hold at particular plate and if the hold of the ball is set at the left ball then using the key ‘,’ it will set the hold variable to false and will give it a default speed to the ball.


This function is used to initialize various class variables and also the global variables. This function is called from the main() function in the starting of the code so that all the variables gets initialized before they are used in the program.
For Example:
These are the global variables:
delay = 1;
 pt = 10;
 bs = 5;
 fy = 400;
 bt = 10;
 MLineT = 5;
 scoLeft = 0;
 scoRight = 0;
 tpY = fy + 10;
 bsX = 0.5;
 psY = 1;

These are the class variables:

left.size = 200;
 right.size = 200;
 left.x = -510;
 right.x = 510;
 while (ball.vx == 0)ball.vx = (rand() % 3 - 1)*bsX;
 ball.vy = 0;
 ball.x = 0;
 ball.y = 0;

Class used in this OpenGL Project

class ball:

This class is used for the making the configuration making related to ball and also the class members and class functions related to ball. This class has four class members.

For Example: float x, y, vx, vy;

These variables are used to define the coordinates of the ball. This class has three member functions in it.

For example:move(), mirror(), and draw().

move(): this function is used when the program has to change the speed of the ball.  This function contains logic which changes the (x,y) coordinate of the ball every time it is called from the display() function.

For example:

x += vx;
y += vy;

mirror(): This function is used to make the ball bounce off from the either the left plate of the right plate. So if the ball hits the plate or we can say that if the left coordinate of the ball is equal to the left of the right or left plate then it will bounce off.

Draw(): this function will deal with making of the ball. This function uses glVertex2f() to draw the vertices and make a small square, which is the ball.

class plate:

This function is used for making the configuration related to plates and their movement up and down with the keyboard interaction. There are many class variables in this class for example: 

float x, y = 0, vy = 0, size;
bool Up = false, Down = false, hold = false;

which are used to perform various task let say bool Up is used to set whenever user tries to press the up key (‘q’ or ‘p’) then this variable will be set to true. The variable hold is used when any of the player scores then the state of the ball will be in hold with the plate in one of the player’s plate.

This class also contains some member functions: draw(), move() and care().

draw(): This function is used draw plate in the window at left and right because this function will be called by the two objects of the class used i.e. left and right. This function uses a simple opengl library function glVertex2f() to draw vertex to draw plates.

move(): This function is used for the movement of the plates (left or right).

This function uses its class member variables which represents the coordinate of the plates and this function with some calculations changes in the coordinates and hence its position also changes.

care(): This function is used when any of the player scores a goal then after that the default state of the ball is to be sticking with the player who goaled.

This function takes care of that and it initialized all the speed parameters and also the position coordinates.


This function have some different configuration this time.
For example:
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
 glutInitWindowSize(700, 700);
 glutInitWindowPosition(100, 100);
 glutTimerFunc(delay, Timer, 0);
 glClearColor(0, 0, 0, 1.0);
 gluOrtho2D(-700, 700, -700, 700);

This function uses a new function called glutKeyboardUp() which is used or called when the keyboard is not using some particular keys.

Application of this OpenGL Project:

This program can be further implemented and make the user play with the computer as now it is two user player game.  So making the game artificial intelligence so that the user can play against the computer.

One Response

  1. Nilanjan April 6, 2017

Leave a Reply

Your email address will not be published. Required fields are marked *