OpenGL Car Game in C++

The following program is a game which is made using the opengl library in C++.

The following program also deals with the various functionality and the working of the following program is based on the keyboard interactions.

These keyboard functions will be called using opengl library function glKeyboardFunc().

This following program uses keyboad’s many keys for the interaction between the user and the application like moving the car from left to right.

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 Program

The functions used in the program namely: main(), textPrint(), numbericTextPrint(), display(), makeVehicle(), makeOtherVehicle(),init() and SpecialKey().


This function is called prior to the display() function so that the initial requirements of the window before drawing any object on the display window can be done.

This function is also used to set the default matrix mode for the display window to the GL_PROJECTION.

For Example:


The next line in the program specifies to load the identity matrix into the current matrix for the operation.

The program also uses glOrtho2D() function which is used to define the boundary of the object within drawing.

Syntax of glOrtho() is:  void glOrtho2D( GLdouble left, GLdouble right, GLdouble bottom, GLdouble top);

The arguments of the function will be clear from the name itself the Gldouble left decides the left most boundary and so on.

The argument GLdouble zNear and GLdouble zFar decides the width of the text of the boundary along the z-axis which we do not consider here.

For Example: gluOrtho2D(-500, 500, -500, 500);

This line of code explains that a that a region of cube will be formed which will have left x axis maximum range at -500 and the right x axis maximum range at 500.

Similarly it will have top y axis maximum range at 500 and the down/bottom y axis maximum range at -500.

This function is also used to initialize the position of the other vehicle to either of the position (left or right) remember it only sets the position not the drawing is made in this function.

The initialization of the position is done using a rand() function generator in C++.

The left x position has x-cordinate as 200 and the right has x-cordinate 300 and there are 4 randomly generated position for vehicles.

textPrint() and numericTextPrint():

The definition of this function is as follows:

void textPrint(char a[], int x, int y) which means it accepts char array and two int arguments and returns nothing.

This function uses two important functions of the opengl library like glRasterPos2f() and glutBitmapCharacter() which are used to draw string on the display window.

The first function glRasterPos2f() is used to set the position where the string will be printed.

This position is set to the position which are received by the two integer arguments.

The other function glutBitmapCharacter() is used to print the  character by character on the display window as the position which is specified by the above function.


The definition of this function is as follows:

void display() which means it accepts nothing as argument and returns nothing.

This function in this program is used to initialize and clear the color of the display window using the opengl library function glClearColor().

This function is used to draw the boundary where the car will race which is a QUADRILATERAL.

This function is using opengl library function glTranslatef() to keep moving the racing track as to make illusion that the car are moving.

Then this function uses two important functions makeVehicle() and makeOtherVehicle() to draw our and other different cars.

The definition of our and other car is defined in these functions.

Along with all of this there is a “score” variable which keeps track of the score untill our car is in the race and not hit to the other car.

The display() function also uses textPrint() function to display or print the score on the display window.

We know that the increamentation in the int type score is there and for that different function is used to print it on the display window that is using numericTextPrint().

For Example:

score = score + 1;

glColor3f(1, 1, 1);

textPrint(“Score :”, 360, 455);

//This function is used to convert the integer value to char array here “score” to “buffer”

_itoa_s(score, storeCharArray, 10);

numericTextPrint(storeCharArray, 6, 420, 455);

For which there is conversion from integer to string or charArray using the function _itoa_s().

The display() function is also having a check constraint for the hasCollide variable to check that whether our car has hit the other car on the track or not.

If the above condition is true then the Message “Game Over” will be printed on the display mode and the Game window will be destroyed.

For Example:

if (hasCollide){

//choosing the white color for the Text Displaying

glColor3f(1, 1, 1);

//output on the display when the game is over

textPrint(“Will Exit…….”, 200, 220);

textPrint(“GAME-OVER”, 200, 250);


//the below loop will work as a sleep function

for (int i = 0; i<1000000000; i++);



This above example states the game over mode displaying of the messages on the display window.


This function is used for the user interation with the keyboard when the user have to enter something through the mouse.

The user is allowed to use the left and the right button keys of the keyboard arrow button.

For Example:

switch (key){


//setting the x vertex of the car

vX = 200;



vX = 300;




This line of code specifies that if the left button of the keyboard is pressed and the position of our car will be on the left side of the racing track.

And the same works if the user tries to press the right key of the keyboard then the position of our car will be shifted to the right side of the track.


The main function is as follows:

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);

glutInitWindowSize(600, 400);

glutInitWindowPosition(30, 10);




glutTimerFunc(100, timer, 0);


glutMainLoop(); This main() is used to set the width and height of the display window initially using the library function glutInitWindowSize()  and is also used to set the initial position of the display window using the library function glutInitWindowPosition().

Many help strings are displayed on the console window for the keyboard interaction by the user.

This function also uses mouse interaction function which is called by the library function glutMouseFunc().

Application of the Program: This program can be implemented in the village simulation of various problems with the relation to the technologies.

Leave a Reply

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