How to Draw Car Move Across the Traffic Signal by C Language

cars moving across the traffic signal using C or C++ graphics programming language

Introduction :

Drawing graphics using C or C++ programming languages allows you to imagine and simulate various real-world scenarios, including cars moving across a traffic signal. In this article, I shall explore you how to create a program in the C or C++ graphics programming language where cars moving across the traffic signal. By utilizing graphics.h library of Turbo C++ IDE, I show you how to implement a graphical representation of cars and a traffic signal.

Here, you see the movement of cars from one side of the screen to the other, passing by a traffic signal and also see the changing lights of the traffic signal by C or C++ graphics programming language. You also learn how to use malloc() function which allocates memory block that dynamically uses in C or C++ programing language.

About traffic signal :

A traffic signal or traffic light is a device used to control the orderly movement of vehicles or cars. It consists of a set of lights in different colors such as red, yellow and green. The lights of traffic signal arranged vertically or horizontally. Traffic signals help regulate traffic flow to prevent accidents and improve the road safety.

There are three colors of light (red, yellow and green) you can see in traffic signal with different meaning. In traffic signal, red light indicates that drivers have to stop behind the designated stop line or crosswalk. The yellow light indicates that the signal is about to change to red. The green light indicates that drivers have to proceed safely to move forward.

About the program :

In this program, you see how to bring cars to life as it moves across the traffic signal in C or C++ programming language. Here, I create a program which shows the road crossing with four cars and traffic signal with two lights (red and green). Two cars move horizontally and other two moves vertically with opposite direction.

When the cars on the horizontal road movie across the traffic according to see a green light, the cars on the vertical road were stopped at that time because they see the red light. When the cars on the horizontal road will be stop to see red color, the cars on the vertical road will be start their journey. This process is run continues until the user presses any key.

Explanation of the program :

Here, I give an explanation about how draw cars moving across the traffic signal by C or C++ graphics programming language. At the beginning of the program, include the necessary header files. You need to include the graphics.h library, which provides functions for graphics programming in C or C++ language. For malloc() and free() function include the stdlib.h library which also helps to convert an int variable to void * in my program. Next, include conio.h and dos.h libraries for use kbhit() and delay() functions respectively.

In the main function, you need to initialize the graphics mode using the initgraph() function. To draw the cars and traffic signal, you can use the graphics functions provided by the graphics.h library. The setbkcolor() function uses to set the background of graphics windows and setcolor() for set the color of different shapes. Then draw the car body using line() and arc() functions and colored the body of car by floodfill() function. I also use the fillellipse() function to draw wheels at the appropriate positions. The setfillstyle() function is used for the color of the wheels.

After that, using imagesize() function calculates the size of memory will be used to create the car. Now malloc() function use for allocating a block of memory from the memory heap which is used in the program where needed. Here malloc() use to store the car image for future used. The getimage() function uses to copy the image of car from screen to memory.

Next draw a red rectangle by the rectangle() and floodfill() functions which allocate and copy the image by similar process. This will be used to redraw the road after the car move. In the graphics screen, I write some text like how stop the program and my name using settextstyle() and outtextxy() functions. Now in the while loop check that any key press or not by kbhit() function. There are two for loop in the while.

Here I create the vertical and horizontal road by rectangle() function and color the road by floodfill() function. The crosswalk and the divider of the road create by the bar() function. The rectangle() function is used to draw the structure of the traffic signal on the screen, while the fillellipse() function is used to draw the red and green light. I set the color of the light using the setfillstyle() function.

Now put the save image of cars and background of road on the screen by putimage() function. To simulate the car moving across the traffic signal, you need to update the position of cars at regular intervals. Here, I use delay() function to control the speed of the car which make the animation more realistic. Finally, you call the free() function to deallocate the memory block allocated by malloc() and also call the closegraph() function to close the graphics mode.

How run the program :

At first, you install the Turbo C++ IDE on your pc because graphics.h is included in the Turbo C++ IDE. After that,open the Turbo C++ IDE and create a C or C++ file with .c or .cpp extension. Now copy the below source code of drawing cars moving across the traffic signal using C or C++ graphics programming language and paste in your C or C++ file which you just created. Do you know how to copy paste in the Turbo C++ IDE, then click here. You have to also know how to use graphics.h in Turbo C++ IDE otherwise follow my link.

Source code of drawing cars moving across the traffic signal :

You can copy the below source code of drawing cars moving across the traffic signal using C or C++ graphics programming language and use in your own program.

/*Developed by Puskar Jasu*/
#include <graphics.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
int main(void)
{
    int x, y, i = 0, j = 0, k = 1, a = 4, b = 1, r, l = 10;
    void *car, *bac, *car1, *bac1;
    unsigned int siz, size, size1, siz1;
    int graphic_driver = DETECT, graphic_mode;
    initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
    x = getmaxx() / 2;
    y = getmaxy() / 2;
    setbkcolor(1);
    setcolor(13);
    setfillstyle(1, 13);
    fillellipse(450, 100, 5, 5);
    fillellipse(450, 140, 5, 5);
    setcolor(14);
    setfillstyle(1, 14);
    line(450, 85, 450, 90);
    arc(450, 100, 270, 90, 10);
    line(450, 110, 450, 130);
    arc(450, 140, 270, 90, 10);
    line(450, 150, 450, 155);
    arc(450, 120, 270, 320, 35);
    line(475, 95, 475, 145);
    arc(450, 120, 40, 90, 35);
    floodfill(460, 120, 14);
    size1 = imagesize(445, 85, 475, 155);
    car1 = malloc(size1);
    getimage(445, 85, 475, 155, car1);
    setcolor(4);
    setfillstyle(1, 4);
    rectangle(445, 160, 475, 230);
    floodfill(450, 170, 4);
    siz1 = imagesize(445, 160, 475, 230);
    bac1 = malloc(siz1);
    getimage(445, 160, 475, 230, bac1);
    setcolor(1);
    setfillstyle(1, 1);
    rectangle(444, 84, 476, 231);
    floodfill(450, 100, 1);
    setcolor(6);
    setfillstyle(1, 6);
    fillellipse(100, 100, 5, 5);
    fillellipse(140, 100, 5, 5);
    setcolor(14);
    setfillstyle(1, 14);
    line(85, 100, 90, 100);
    arc(100, 100, 0, 180, 10);
    line(110, 100, 130, 100);
    arc(140, 100, 0, 180, 10);
    line(150, 100, 155, 100);
    line(155, 100, 155, 87);
    arc(125, 115, 40, 80, 42);
    line(97, 75, 135, 75);
    arc(120, 100, 130, 180, 35);
    floodfill(100, 80, 14);
    size = imagesize(85, 75, 155, 105);
    car = malloc(size);
    getimage(85, 75, 155, 105, car);
    setcolor(4);
    setfillstyle(1, 4);
    rectangle(185, 75, 255, 105);
    floodfill(200, 80, 4);
    siz = imagesize(185, 75, 255, 105);
    bac = malloc(siz);
    getimage(185, 75, 255, 105, bac);
    setcolor(1);
    setfillstyle(1, 1);
    rectangle(82, 74, 256, 112);
    floodfill(200, 80, 1);
    setcolor(15);
    settextstyle(1, 0, 4);
    outtextxy(20, 10, "press any key");
    outtextxy(20, 50, "for stop");
    outtextxy(450, 10, "puskar");
    outtextxy(450, 50, "jasu");
    while (!kbhit())
    {
        for (r = 0; r <= x + x; r++)
        {
            k = 10;
            l = 1;
            a = 4;
            b = 1;
            setcolor(4);
            setfillstyle(1, 4);
            rectangle(0, y - 40, x + x, y + 40);
            rectangle(x - 40, y + 40, x + 40, y + y);
            rectangle(x - 40, 0, x + 40, y - 40);
            floodfill(x, y, 4);
            floodfill(x - 5, 5, 4);
            floodfill(x - 5, y + y - 20, 4);
            setfillstyle(4, 15);
            bar(x - 90, y - 40, x - 70, y + 40);
            bar(x + 70, y - 40, x + 90, y + 40);
            bar(x - 40, y - 90, x + 40, y - 70);
            bar(x - 40, y + 70, x + 40, y + 90);
            setfillstyle(1, 14);
            bar(0, y - 1, x - 90, y + 1);
            bar(x + 90, y - 1, x + x, y + 1);
            bar(x - 1, 0, x + 1, y - 90);
            bar(x - 1, y + 90, x + 1, y + y);
            setcolor(14);
            rectangle(x - 100, y - 110, x - 80, y - 40);
            rectangle(x + 80, y + 40, x + 100, y + 110);
            setcolor(k);
            setfillstyle(1, k);
            fillellipse(x - 90, y - 100, 8, 8);
            fillellipse(x + 90, y + 100, 8, 8);
            setcolor(l);
            setfillstyle(1, l);
            fillellipse(x - 90, y - 70, 8, 8);
            fillellipse(x + 90, y + 70, 8, 8);
            setcolor(14);
            rectangle(x - 110, y + 80, x - 40, y + 100);
            rectangle(x + 40, y - 80, x + 110, y - 100);
            setcolor(a);
            setfillstyle(1, a);
            fillellipse(x - 100, y + 90, 8, 8);
            fillellipse(x + 100, y - 90, 8, 8);
            setcolor(b);
            setfillstyle(1, b);
            fillellipse(x - 70, y + 90, 8, 8);
            fillellipse(x + 70, y - 90, 8, 8);
            putimage(i, y - 35, bac, 0);
            putimage(x + x - 70 - i, y + 5, bac, 0);
            putimage(x - 35, y + y - j - 70, bac1, 0);
            putimage(x + 5, j, bac1, 0);
            delay(5);
            putimage(i + 1, y - 35, car, 1);
            putimage(x + x - 70 - i - 1, y + 5, car, 1);
            putimage(x - 35, y + y - 1 - j - 70, car1, 1);
            putimage(x + 5, j + 1, car1, 1);
            if (j <= (y - 170))
                j++;
            if (i == x + x)
                i = 0;
            i++;
        }
        for (r = 0; r <= y + y; r++)
        {
            k = 4;
            l = 1;
            a = 1;
            b = 10;
            setcolor(4);
            setfillstyle(1, 4);
            rectangle(0, y - 40, x + x, y + 40);
            rectangle(x - 40, y + 40, x + 40, y + y);
            rectangle(x - 40, 0, x + 40, y - 40);
            floodfill(x, y, 4);
            floodfill(x - 5, 5, 4);
            floodfill(x - 5, y + y - 20, 4);
            setfillstyle(4, 15);
            bar(x - 90, y - 40, x - 70, y + 40);
            bar(x + 70, y - 40, x + 90, y + 40);
            bar(x - 40, y - 90, x + 40, y - 70);
            bar(x - 40, y + 70, x + 40, y + 90);
            setfillstyle(1, 14);
            bar(0, y - 1, x - 90, y + 1);
            bar(x + 90, y - 1, x + x, y + 1);
            bar(x - 1, 0, x + 1, y - 90);
            bar(x - 1, y + 90, x + 1, y + y);
            setcolor(14);
            rectangle(x - 100, y - 110, x - 80, y - 40);
            rectangle(x + 80, y + 40, x + 100, y + 110);
            setcolor(k);
            setfillstyle(1, k);
            fillellipse(x - 90, y - 100, 8, 8);
            fillellipse(x + 90, y + 100, 8, 8);
            setcolor(l);
            setfillstyle(1, l);
            fillellipse(x - 90, y - 70, 8, 8);
            fillellipse(x + 90, y + 70, 8, 8);
            setcolor(14);
            rectangle(x - 110, y + 80, x - 40, y + 100);
            rectangle(x + 40, y - 80, x + 110, y - 100);
            setcolor(a);
            setfillstyle(1, a);
            fillellipse(x - 100, y + 90, 8, 8);
            fillellipse(x + 100, y - 90, 8, 8);
            setcolor(b);
            setfillstyle(1, b);
            fillellipse(x - 70, y + 90, 8, 8);
            fillellipse(x + 70, y - 90, 8, 8);
            putimage(i, y - 35, bac, 0);
            putimage(x + x - 70 - i, y + 5, bac, 0);
            putimage(x - 35, y + y - j - 70, bac1, 0);
            putimage(x + 5, j, bac1, 0);
            delay(5);
            putimage(i + 1, y - 35, car, 1);
            putimage(x + x - 70 - i - 1, y + 5, car, 1);
            putimage(x - 35, y + y - 1 - j - 70, car1, 1);
            putimage(x + 5, j + 1, car1, 1);
            if (i <= x - 170)
                i++;
            if (j == y + y)
                j = 0;
            j++;
        }
    }
    free(car);
    free(bac);
    free(car1);
    free(bac1);
    closegraph();
    return 0;
}

Output :

After running the above program, you can see the cars moving across the traffic signal and observe when the traffic lights change, the cars move accordingly on the screen. You can also see the output of moving cars across the traffic signal in my YouTube channel.

Conclusion :

In this above article, you learned how to draw moving cars across the traffic signal using the C or C++ graphics programming language. By following above program and utilizing the graphics.h library of Turbo C++ IDE, you can create a simple animation of cars moving across the traffic signal. Thank you for visiting my site.

Scroll to Top