-->

Tuesday, March 8, 2022

Moving Car | C Graphics Program


To download the .EXE file click here

§  Source Code :


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

            int data(int, int);

            void phase1()

            {

                        int i,k,x,y,j,w;

                        char n, str[30];

                        x=getmaxx();

                        y=getmaxy()/2;

                        i=0;

                        w=-1;

                        for(k=0;;k++)

                        {

                                    ground:

                                    line(0,y,x,y);

           

                                    //road light

                                    setbkcolor(BLACK);

                                    setcolor(WHITE);

                                    if(i<=120)

                                                i=i+1;

                                    else

                                    {

                                                i= 0;

                                                w--;

                                    }

                                    if(i>=0){

                                    for(j=0;j<x;j+=120)

                                    {

                                                line(50+j-i,y,50+j-i,y-100);

                                                line(55+j-i,y,55+j-i,y-100);

                                                line(10+j-i,y-100,55+j-i,y-100);

                                                arc(18+j-i,y-100,180,360,8);

           

                                                //color

                                                setfillstyle(SOLID_FILL,YELLOW);

                                                floodfill(18+j-i,y-95,WHITE);

                                                //floodfill(87+j-i,y-95,WHITE);

                                                setfillstyle(SOLID_FILL,WHITE);

                                                floodfill(52+j-i,y-5,WHITE);

                                    }

                                    }

                                    else {

                                    for(j=x;j<0;j+=120)

                                    {

           

                                                line(50+j-i,y,50+j-i,y-100);

                                                line(55+j-i,y,55+j-i,y-100);

                                                line(10+j-i,y-100,55+j-i,y-100);

                                                arc(18+j-i,y-100,180,360,8);

           

                                                //color

                                                setfillstyle(SOLID_FILL,YELLOW);

                                                floodfill(18+j+i,y-95,WHITE);

                                                setfillstyle(SOLID_FILL,WHITE);

                                                floodfill(52+j-i,y-5,WHITE);

                                    }}

           

                                    //car

                                    //wheels

                                    circle(40,y-8,8);

                                    circle(80,y-8,8);

                                    if(k%2==0)

                                    {

                                                line(32,y-8,48,y-8);

                                                line(40,y-16,40,y);

           

                                                line(72,y-8,88,y-8);

                                                line(80,y-16,80,y);

                                    }

                                    else

                                    {

                                                line(36,y-12,44,y-4);

                                                line(44,y-12,36,y-4);

           

                                                line(76,y-12,84,y-4);

                                                line(84,y-12,76,y-4);

                                    }

           

                                    //body

           

                                    line(51,y-8,69,y-8);

                                    line(29,y-8,20,y-8);

                                    line(91,y-8,105,y-8);

                                    arc(40,y-8,0,180,11);

                                    arc(80,y-8,0,180,11);

           

                                    arc(28,y-13,85,220,10);

                                    line(25,y-23,100,y-23);

                                    arc(95,y-13,335,95,10);

           

                                    line(37,y-38,75,y-38);

                                    line(37,y-38,25,y-23);

                                    line(75,y-38,86,y-23);

           

                                    line(45,y-38,45,y-23);

                                    line(65,y-38,65,y-23);

           

                                    //color

                                    setfillstyle(SOLID_FILL,WHITE);

                                    floodfill(21,y-9,WHITE);

                                    floodfill(103,y-9,WHITE);

           

                                    if(kbhit())

                                    {

                                    n = getch();

                                    if(n == 'e')

                                                exit(0);

                                    else if (n == 'w'&& w<=41 ){

                                                w=w+4;

                                                outtextxy(310,70,"W");

                                    }

                                    else if(n== 's' && w>=0){

                                                w=w-1;

                                                outtextxy(310,70,"S");

                                    }

                                    }

                                    i=i+w;

                                    w= data(i,w);

                                    if(w== -1){

                                                outtextxy(200,300,"Gain your speed by pressing ' W '");

                                                outtextxy(200,310,"To exit press ' e '");

                                                n= getch();

                                                if(n== 'w')

                                                            w=4;

                                                else if(n== 'e')

                                                            exit(0);

                                    }

                                    delay(20);

                                    cleardevice( );

                        }

            }

            int data(int x , int y)

            {

                        char str[30],ch;

                        int  i=x, w= y, s;

                        if(w==-1)

                                    s=0;

                        else

                                    s= w+5;

                        sprintf(str,"speed= %d km/h ",s);

                        outtextxy(30,70,str);

                        outtextxy(30,30,"Press 's' to reduce speed");

                        outtextxy(30,40,"Press 'w' to gain speed");

                        outtextxy(30,50,"Press 'e' to stop");

                        outtextxy(200,70,"Key Pressed : ");

                        return w;

            }

            void main( )

            {

                        int gd=DETECT, gm;

                        initgraph(&gd, &gm, "c:\\Turboc3\\BGI");

                        phase1( );

                        closegraph( );

            }

 

§  Output :

 



No comments:

Post a Comment

Contact Form

Name

Email *

Message *