Car

Write a program to simulate the movement of a car.



#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void wheel1(int x,int y)
{
	int r=20;
	circle(x,y,r);
	line(x,y-r,x,y+r);
	line(x-r,y,x+r,y);
}
void wheel2(int x,int y)
{
	int r=20,fac;
	fac=r*(sin((45*M_PI)/180));
	circle(x,y,r);
	line(x+fac,y+fac,x-fac,y-fac);
	line(x-fac,y+fac,x+fac,y-fac);

}
void main()
{
	int gd=DETECT,gm,x=100,y=300;
	int c=0;
	initgraph(&gd,&gm,"c:\\tc\\bgi");
	while(!kbhit())
	{
		cleardevice();
		c++;
		line(0,400,639,400);
		line(x,y,x+150,y);
		line(x+150,y,x+200,y+50);
		line(x+200,y+50,x+250,y+50);
		line(x+250,y+50,x+260,y+80);
		line(x+260,y+80,x+180,y+80);

		line(x+120,y+80,x+60,y+80);

		line(x+180,y+80,x+170,y+55);
		line(x+120,y+80,x+130,y+55);
		line(x+170,y+55,x+130,y+55);

		line(x+60,y+80,x+50,y+55);
		line(x+0,y+80,x+10,y+55);
		line(x+50,y+55,x+10,y+55);

		line(x+0,y+80,x-20,y+80);
		line(x-20,y+80,x-20,y+50);
		line(x-20,y+50,x,y);
		if(c%2==0)
		{
			wheel1(x+150,y+80);
			wheel1(x+30,y+80);
		}
		else
		{
			wheel2(x+150,y+80);
			wheel2(x+30,y+80);
		}
		x=(x+10)%639;
		delay(200);
	}
	getch();
}

Output
1

Leave a Reply

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