Skip to content
Related Articles

Related Articles

C++ program to draw a traffic light simulation using computer graphics

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 21 Sep, 2021

In computers, graphics.h can be used to provide direct functions to draw different coordinates, like circles, rectangles, etc. Using these shapes, different objects can be drawn. This article focuses on how to make a traffic light simulation in Turbo C++.

Functions Used:

  • delay(n): It is used to hold the same screen for N milliseconds. As the execution of the program is too fast, therefore, to observe the program, the delay function is used.
  • setcolor(n): This function is used to set the color of the cursor. Here N is the color that is predefined in graphics.h header file
  • settextstyle()/ settextjustify(): This function is used for styling purposes of the text pointer.
  • rectangle(x1, y1, x2, y2): This function is used to draw a rectangle where x1, y1, and x2, y2 are the coordinates of a line.
  • circle(x, y, r): This is a representation of a circle where x, y are the coordinates of the center of the circle and r is the radius of the circle.


  • First, initialize graphic mode using function initgraph().
  • Create a rectangle using the rectangle() function and fill the middle part of two lines using the setcolor() function.
  • Draw 3 circles and color/style them accordingly by using functions like setcolor(), floodfill(), and setfillstyle().
  • Repeat the process until the traffic light is not complete.
  • Use delay() function in between to hold the screen where we want.

Below is the program for the above approach:


// C++ program for the above approach
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <iostream.h>
#include <stdlib.h>
// Driver Code
void main()
    int gd = DETECT, gm, midx, midy;
    // Passed three arguments to the
    // initgraph function to initialize
    // graphics mode
    initgraph(&gd, &gm, "C:\\TC\\BGI");
    midx = getmaxx() / 2;
    midy = getmaxy() / 2;
    // Set color of intro text as cyan
    // Below just styling the text
    settextstyle(TRIPLEX_FONT, HORIZ_DIR, 4);
    settextjustify(CENTER_TEXT, CENTER_TEXT);
    outtextxy(midx, midy - 10, "TRAFFIC LIGHT SIMULATION");
    outtextxy(midx, midy + 10, "PRESS ANY KEY TO START");
    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
    // rectangle lines
    rectangle(midx - 30, midy - 80, midx + 30, midy + 80);
    // Circle for red light
    circle(midx, midy - 50, 22);
    setfillstyle(SOLID_FILL, RED);
    floodfill(midx, midy - 50, 22);
    // Text inside red light
    outtextxy(midx, midy - 50, "STOP");
    // Delay of 2 sec
    // Drawing lines of  rectangle
    // for  traffic light
    rectangle(midx - 30, midy - 80,
              midx + 30, midy + 80);
    // Drawing yellow circle for light
    circle(midx, midy - 50, 22);
    setfillstyle(SOLID_FILL, YELLOW);
    floodfill(midx, midy, WHITE);
    // Setting inner text to ready
    outtextxy(midx - 18, midy - 3, "READY");
    // Drawing lines of  rectangle
    // for  traffic light
    rectangle(midx - 30, midy - 80,
              midx + 30, midy + 80);
    // Circle for green light
    circle(midx, midy + 50, 22);
    setfillstyle(SOLID_FILL, GREEN);
    floodfill(midx, midy + 50, WHITE);
    // Setting inner text to GO
    outtextxy(midx - 7, midy + 48, "GO");
                 HORIZ_DIR, 4);
    outtextxy(midx - 150, midy + 100,



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!