How to Use graphics.h for Making Simple Games

What is graphics.h?

Graphics.h is a header file that provides access to basic functions such as line(), circle(), rectangle(), drawpoly(), ellipse() ,etc for drawing various lines and geometrical shapes. It provides 16 color options for colouring an object with different colors and patterns. It is used for creating simple graphical applications.

Where can graphics.h be used?

Graphics.h header file can be included in programs written C, C++ or C# language. It is a specific library for Turbo C++ compiler. It can be compiled in Visual Studio Code, CodeBlocks or Ubuntu, but graphics.h is required to be downloaded seperately. It is ideal to use Turbo C++.

How to use?

Graphics.h header file can be used by including it in the beginning of the source code.

#include<graphics.h>

After that to draw any graphics on the screen, graphics should be initialized. It can be done using initgraph() function. The initgraph() function takes three arguments the graphics driver, graphics mode and driver path.

#include "graphics.h"
int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, NULL);

    return 0;
}

After initialize graphics functions such as line(), circle() , rectangle() can be used to render various images and patterns on the screen. After using various graphics.h functions to draw images and patterns on the screen, graphics window can be closed by calling closegraph() function.

#include "graphics.h"
int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, NULL);
    setcolor(10);
    line(50, 200, 180, 200);
    circle(300, 200, 100);
    rectangle(450, 100, 550, 300);
    delay(100);
    return 0;
}

Output of above code is :-

How to Move Objects using Controls?

Graphics.h has some keys defined such as KEY_PGUP, KEY_LEFT, KEY_RIGHT, KEY_PGDN , and more. These keys have a value assigned to them. getch() can be used to detect which key is pressed. When these keys are pressed, getch will return a zero followed by one of these values. Applying this enables interaction between user and the images rendered on the screen.

windows.h header file can be included for access to to keys that are not defined in graphics.h. Here GetAsyncKeyState() function can be used to recognize the keys pressed.

The following program is used to move a circle :-

#include "graphics.h"
#pragma comment(lib,"graphics.lib")
#include<iostream>
void main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, NULL);
    int i = 250, j = 250, x = 0, y = -1;
    while (1)
    {
        circle(i, j, 30);
        if (GetAsyncKeyState(VK_UP))
        {
            x = 0;
            y = -1;
        }

        else  if (GetAsyncKeyState(VK_LEFT))
        {
            x = -1;
            y = 0;

        }

        else if (GetAsyncKeyState(VK_RIGHT))
        {
            x = 1;
            y = 0;
        }

        else if (GetAsyncKeyState(VK_DOWN))
        {
            x = 0;
            y = 1;
        }

        if (GetAsyncKeyState(VK_RETURN))
            break;

        i = i + x;
        j = j + y;

        delay(10);
        cleardevice();
    }
}

This can be used to give movement to the characters in the game such as controlling the movement of mouse in the maze.

How to Display Text on the Graphics window?

Graphics.h provides functions such as outtextxy() to display message on the graphics window. outtextxy() takes three arguments x coordinate, y coordinate and an array of characters.

The first argument specifies x coordinate of position of text on the screen, second argument specifies y coordinate of position of text on the screen and third argument is the text to be displayed.

#include "graphics.h"
int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, NULL);
    outtextxy(200, 150,"Hello World");
    return 0;
}

This can be used to display score board on the screen.

Conclusion

Graphics.h is a beginner friendly option to make simple games. There are many modern graphics libraries such as OpenGl, DirectX, SFML(Simple and Fast Multimedia Library) or SDL (Simple DirectMedia Layer) that can also be used.