In this article, you will learn how to make a Self-driving Car Using graphic in C++. It is a GUI Based Program in the C++ language. If you are a beginner and want to create graphics programming then probably this one is best for you.
Source Code For Self Driving Car Using Graphics in C++
/*Moving a car*/
#include<conio.h>
#include<graphics.h>
#include<stdio.h>
#include<dos.h>
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
for (int i=0;i<500;i++)
{
/*techarge part*/
outtextxy(40,220,"TECHARGE");
line(30,200,30,250);
line(30,200,110,200);
line(30,250,110,250);
line(110,200,110,250);
line(50,250,50,390);
line(55,250,55,390);
/***CAR BODY ******/
line(50+i,370,90+i,370);
arc(110+i,370,0,180,20);
line(130+i,370,220+i,370);
arc(240+i,370,0,180,20);
line(260+i,370,300+i,370);
line(300+i,370,300+i,350);
line(300+i,350,240+i,330);
line(240+i,330,200+i,300);
line(200+i,300,110+i,300);
line(110+i,300,80+i,330);
line(80+i,330,50+i,340);
line(50+i,340,50+i,370);
/***CAR Windows***/
line(165+i,305,165+i,330);
line(165+i,330,230+i,330);
line(230+i,330,195+i,305);
line(195+i,305,165+i,305);
line(160+i,305,160+i,330);
line(160+i,330,95+i,330);
line(95+i,330,120+i,305);
line(120+i,305,160+i,305);
/**Wheels**/
circle(110+i,370,17);
circle(240+i,370,17);
delay(10);
cleardevice();
line(0,390,639,390); //ROAD
}
getch();
return 0;
}
Explanation of Code
The provided code animates a car moving across the screen. Here’s a breakdown:
Libraries:
<conio.h>
: Provides functions for console input/output likegetch
.<graphics.h>
: Provides graphics functions for creating visuals (used in older DOS graphics programming).<stdio.h>
: Provides standard input/output functions likeprintf
.<dos.h>
: Provides functions specific to the DOS operating system (may not be relevant today).
Main Function:
int main()
: This is the program’s entry point.
Graphics Setup:
int gd=DETECT,gm;
: Declares variables for graphics driver and mode.DETECT
tells the program to automatically detect the best graphics driver.initgraph(&gd,&gm,"c:\\turboc3\\bgi");
: Initializes the graphics mode using the detected driver and mode. The path specifies the location of the graphics driver files (likely for Turbo C++ compiler).
Animation Loop:
for (int i=0;i<500;i++)
: This loop runs 500 times, creating the animation frames.
“TECHARGE” Text (Commented Out):
- These commented lines (
outtextxy
,line
) would draw the text “TECHARGE” and a rectangle around it. They seem unrelated to the car animation and are likely commented out for now.
Car Body:
- Lines are drawn progressively with increasing
i
value, creating the illusion of movement. - Arcs and lines create the car’s body shape with rounded edges.
Car Windows:
- Similar to the car body, lines are drawn to create two rectangular windows.
Wheels:
circle
function draws two circles for the wheels.
Delay and Clearing:
delay(10);
: Pauses the program for 10 milliseconds, creating a smooth animation effect.cleardevice();
: Clears the graphics screen before redrawing everything for the next frame.
Road:
line(0,390,639,390);
: This line drawn at the bottom of the screen represents the road.
User Input:
getch();
: Waits for any keypress from the user before exiting the program.
Return Statement:
return 0;
: Indicates successful program execution.
Overall, this code demonstrates animating a car moving from left to right across a graphical road. However, it’s written for an older graphics library specific to DOS environments. Modern programming languages and libraries offer more user-friendly and powerful ways to achieve similar animation effects.
Hope you like this program, Happy Programming 🙂