Write a C Program to perform Car Movement using graphics

By | 02.12.2016

Perform Car Movement using graphics


Write a C Program to perform Car Movement using graphics. Here’s simple Program to perform Car Movement using graphics in C Programming Language.


Below is the source code for C Program to perform Car Movement using graphics which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C Program to perform Car Movement using graphics  */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
int scan, ascii;
void getkey() {
        union REGS ii, oo;
        ii.h.ah = 0;
        int86(22, &ii, &oo);
        scan = oo.h.ah;
        ascii = oo.h.al;
}
void car(int x, int c) {
        setcolor(c);
        line(x + 150, 100, x + 242, 100);
        ellipse(x + 242, 105, 0, 90, 10, 5);
        line(x + 150, 100, x + 120, 150);
        line(x + 252, 105, x + 280, 150);
        line(x + 100, 150, x + 320, 150);
        line(x + 100, 150, x + 100, 200);
        line(x + 320, 150, x + 320, 200);
        line(x + 100, 200, x + 110, 200);
        line(x + 320, 200, x + 310, 200);
        arc(x + 130, 200, 0, 180, 20);
        arc(x + 290, 200, 0, 180, 20);
        line(x + 270, 200, x + 150, 200);
        circle(x + 130, 200, 17);
        circle(x + 290, 200, 17);
}
void main() {
        int gd = DETECT, gm = DETECT, i = 0, c = 1, prev = 80;
        initgraph(&gd, &gm, "");
        cleardevice();
        car(0, 15);
        while (!kbhit());
        while (1) {
                getkey();
                if (scan == 72) {
                        c++;
                        scan = prev;
                }
                if (scan == 80) {
                        if (c != 1)
                                        c--;
                        scan = prev;
                }
                if (ascii == 13)
                            break;
                if (scan == 77) {
                        prev = 77;
                        while (!kbhit()) {
                                car(i, 0);
                                i += c;
                                car(i, 15);
                                delay(20);
                        }
                }
                if (scan == 75) {
                        prev = 75;
                        while (!kbhit()) {
                                car(i, 0);
                                i -= c;
                                car(i, 15);
                                delay(20);
                        }
                }
                if (ascii == 32) {
                        prev = 80;
                        car(i, 15);
                        while (!kbhit());
                }
        }
        getch();
}

Above is the source code for C Program to perform Car Movement using graphics which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

4 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments