You are here

traffic light program in c, traffic light simulation

Traffic light Simulation: Traffic light program in C presents what happens in our daily life at traffic light signals. Firstly user will press a key to start the traffic light simulation.

C programming code

  1. #include<graphics.h>
  2. #include<conio.h>
  3. #include<dos.h>
  4. #include<stdlib.h>
  5.  
  6. main()
  7. {
  8.    int gd = DETECT, gm, midx, midy;
  9.  
  10.    initgraph(&gd, &gm, "C:\\TC\\BGI");
  11.  
  12.    midx = getmaxx()/2;
  13.    midy = getmaxy()/2;
  14.  
  15.    setcolor(RED);
  16.    settextstyle(SCRIPT_FONT, HORIZ_DIR, 3);
  17.    settextjustify(CENTER_TEXT, CENTER_TEXT);
  18.    outtextxy(midx, midy-10, "Traffic Light Simulation");
  19.    outtextxy(midx, midy+10, "Press any key to start");
  20.    getch();
  21.    cleardevice();
  22.    setcolor(WHITE);
  23.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  24.    rectangle(midx-30,midy-80,midx+30,midy+80);
  25.    circle(midx, midy-50, 22);
  26.    setfillstyle(SOLID_FILL,RED);
  27.    floodfill(midx, midy-50,WHITE);
  28.    setcolor(BLUE);
  29.    outtextxy(midx,midy-50,"STOP");
  30.    delay(2000);
  31.    graphdefaults();
  32.    cleardevice();
  33.    setcolor(WHITE);
  34.    rectangle(midx-30,midy-80,midx+30,midy+80);
  35.    circle(midx, midy, 20);
  36.    setfillstyle(SOLID_FILL,YELLOW);
  37.    floodfill(midx, midy,WHITE);
  38.    setcolor(BLUE);
  39.    outtextxy(midx-18,midy-3,"READY");
  40.  
  41.    delay(2000);
  42.    cleardevice();
  43.    setcolor(WHITE);
  44.    rectangle(midx-30,midy-80,midx+30,midy+80);
  45.    circle(midx, midy+50, 22);
  46.    setfillstyle(SOLID_FILL,GREEN);
  47.    floodfill(midx, midy+50,WHITE);
  48.    setcolor(BLUE);
  49.    outtextxy(midx-7,midy+48,"GO");
  50.    setcolor(RED);
  51.    settextstyle(SCRIPT_FONT, HORIZ_DIR, 4);
  52.    outtextxy(midx-150, midy+100, "Press any key to exit...");
  53.  
  54.    getch();
  55.    closegraph();
  56.    return 0;
  57. }

Download Traffic Light Simulation.