C++ program to draw a traffic light simulation using computer graphics
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++.
- 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: