Thursday 9 July 2015

Traffic Light simulation program in C


C program for traffic light simulation

Graphics program in C language for traffic light simulationGraphics in C language , traffic light simulation


#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>


void main()
{
   int gd = DETECT, gm, mx, my;

   initgraph(&gd, &gm, "C:\\TurboC3\\BGI");


   mx = getmaxx()/2;
   my = getmaxy()/2;

   while(!kbhit()){

// Code for RED
   setcolor(WHITE);
   settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
   rectangle(mx-30,my-80,mx+30,my+80);
   circle(mx, my-50, 22);
   setfillstyle(SOLID_FILL,RED);
   floodfill(mx, my-50,WHITE);
   setcolor(BLUE);
   outtextxy(mx-10,my-50,"STOP");
   setcolor(WHITE);
   rectangle(mx-30,my-80,mx+30,my+80);
   circle(mx, my, 20);
   setfillstyle(SOLID_FILL,YELLOW);
   floodfill(mx, my,WHITE);
   setcolor(WHITE);
   circle(mx, my+50, 22);
   setfillstyle(SOLID_FILL,LIGHTGREEN);
   floodfill(mx, my+50,WHITE);
   delay(2000);
   graphdefaults();
   cleardevice();


//Code For yellow
   setcolor(WHITE);
   rectangle(mx-30,my-80,mx+30,my+80);
   circle(mx, my-50, 22);
   setfillstyle(SOLID_FILL,LIGHTRED);
   floodfill(mx, my-50,WHITE);
   circle(mx, my, 20);
   setfillstyle(SOLID_FILL,YELLOW);
   floodfill(mx, my,WHITE);
   setcolor(BLUE);
   outtextxy(mx-18,my-3,"READY");
   setcolor(WHITE);
   circle(mx, my+50, 22);
   setfillstyle(SOLID_FILL,LIGHTGREEN);
   floodfill(mx, my+50,WHITE);
   delay(2000);
   cleardevice();
  

//Code For Green
   setcolor(WHITE);
   rectangle(mx-30,my-80,mx+30,my+80);
   circle(mx, my-50, 22);
   setfillstyle(SOLID_FILL,LIGHTRED);
   floodfill(mx, my-50,WHITE);
   circle(mx, my, 20);
   setfillstyle(SOLID_FILL,YELLOW);
   floodfill(mx, my,WHITE);
   circle(mx, my+50, 22);
   setfillstyle(SOLID_FILL,GREEN);
   floodfill(mx, my+50,WHITE);
   setcolor(BLUE);
   outtextxy(mx-7,my+48,"GO");
   setcolor(RED);
   delay(2000);
   outtextxy(mx-150, my+100, "Press any key to exit...");
   }

   closegraph();
}

No comments:

Post a Comment