Moving car program in C++

Rate this post

Moving Car Program in C++

This program is written in C++ using graphics to create and move a car.

  • A car is made using two rectangles and two circles which act as tyres of car.
  • A for loop is used to move the car forward by changing the rectangle and circle coordinates and erasing the previous contents on screen using clearviewport, you can also use cleardevice.
  • Speed of car can be adjusted using delay function, more the delay lesser will be the speed or lesser the delay your car will move fast. In this program color of the car also keeps on changing, this is accomplished by incrementing the color value by one each time in for loop, you can also use random function for this purpose.
moving car program
moving car program

 

/*Moving a car*/

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

void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,”C:\\tc\\bgi”);
for (int i=0;i<500;i++)
{
/***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();
}

Related Posts

  • 55
    The keyword continue allows us to take up the control to the beginning of the loop, bypassing the statements inside the loop, which have not yet been executed. When continue is encountered inside any loop, control automatically passes to the beginning of the loop. A continue is usually associated with an…
    Tags: program, learn, programming, language
  • 53
    Tags: language, programming, learn
  • 52
    Here is simple C program for analog clock.  This can be used as a reference for project reference. /*Note press ctrl+pause break to stop the clock while executing in TC*/ #include<stdio.h> #include<graphics.h> #include<stdlib.h> #include<math.h> #include<dos.h> #include<time.h> #define PI 3.147 void clockLayout(); void secHand(); void hrHand(); void minHand(); int maxx,maxy; void…
    Tags: #include, program, project, programming, learn
  • 51
    This is a simple calculator program written in C Language. It Performs addition, subtraction, multiplication or division depending the input from user. This usage switch case statement. Let see the C Program # include <stdio.h> int main() { char operator; double firstNumber,secondNumber; printf("Enter an operator (+, -, *,): "); scanf("%c",…
    Tags: program, language, learn, project
  • 50
    This is a tic-tac-toe game in C Language.  You can this as a mini project in C Language.     #include<stdio.h> #include<conio.h> #include<stdlib.h> #include <windows.h> int board[10] = {2,2,2,2,2,2,2,2,2,2}; int turn = 1,flag = 0; int player,comp; void menu(); void go(int n); void start_game(); void check_draw(); void draw_board(); void player_first();…
    Tags: language, program, programming, learn, project

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: