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.
Approach:
- 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++
// 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()
{ clrscr();
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
setcolor(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" );
getch();
cleardevice();
setcolor(WHITE);
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);
setcolor(BLUE);
// Text inside red light
outtextxy(midx, midy - 50, "STOP" );
// Delay of 2 sec
delay(2000);
graphdefaults();
cleardevice();
setcolor(WHITE);
// 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);
setcolor(BLUE);
// Setting inner text to ready
outtextxy(midx - 18, midy - 3, "READY" );
delay(2000);
cleardevice();
setcolor(WHITE);
// 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);
setcolor(BLUE);
// Setting inner text to GO
outtextxy(midx - 7, midy + 48, "GO" );
setcolor(RED);
settextstyle(TRIPLEX_FONT,
HORIZ_DIR, 4);
outtextxy(midx - 150, midy + 100,
"Brrrmmm" );
getch();
} |
Output: