Moving Car Program in C Language (C में चलती हुई कार का प्रोग्राम)


The Moving Car program in C is a graphics-based program that demonstrates animation using graphics functions. It draws a car and moves it from left to right on the screen.

C भाषा में Moving Car प्रोग्राम एक Graphics-Based प्रोग्राम है, जो Graphics Functions की सहायता से Animation प्रदर्शित करता है। यह स्क्रीन पर एक कार बनाता है और उसे बाएँ से दाएँ चलाता है।

C Program

#include <graphics.h>
#include <conio.h>
#include <dos.h>
int main()
{
int i;
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
outtextxy(25, 240,
"Press any key to view the moving car");
getch();
for (i = 0; i <= 420; i += 10)
{
cleardevice();
/* Draw car body */
setcolor(RED);
rectangle(50 + i, 275, 150 + i, 350);
rectangle(150 + i, 315, 200 + i, 350);
/* Draw wheels */
setcolor(WHITE);
circle(75 + i, 360, 10);
circle(175 + i, 360, 10);
delay(100);
}
getch();
closegraph();
return 0;
}

Output

The program first displays the message:

Press any key to view the moving car

After pressing a key, a car appears and moves from left to right across the graphics screen.

Explanation (English)

The program uses graphics functions to draw and animate a car.

  • initgraph() initializes the graphics mode.

  • outtextxy() displays the message on the screen.

  • getch() waits for a key press.

  • rectangle() draws the body of the car.

  • circle() draws the two wheels.

  • setcolor() sets the drawing color.

  • delay(100) pauses the program briefly between animation frames.

  • closegraph() closes the graphics mode.

Animation Logic

The for loop is the main part of the animation:

for (i = 0; i <= 420; i += 10)
{
cleardevice();
/* Draw car at new position */
delay(100);
}

The variable i changes the horizontal position of the car.

In every iteration:

  1. The previous frame is cleared using cleardevice().

  2. The car is drawn at a new position using the updated value of i.

  3. delay(100) pauses briefly before the next frame.

This repeated process creates the illusion of movement.

Animation Logic (हिन्दी)

इस प्रोग्राम में for लूप Animation का मुख्य भाग है।

  • i का मान 0 से 420 तक 10 के अंतर से बढ़ता है।

  • i कार की Horizontal Position निर्धारित करता है।

  • cleardevice() पिछले फ्रेम को साफ करता है।

  • कार को नई Position पर दोबारा Draw किया जाता है।

  • delay(100) प्रत्येक फ्रेम के बीच थोड़ा समय रोकता है।

इस प्रक्रिया को बार-बार दोहराने से कार चलती हुई दिखाई देती है।

Note

This program uses the old Turbo C/C++ BGI graphics library. It is designed for Turbo C/C++ or a compatible graphics environment. It will not run directly in a modern standard C compiler without a suitable graphics library.

यह प्रोग्राम पुराने Turbo C/C++ BGI Graphics Library का उपयोग करता है। इसे चलाने के लिए Turbo C/C++ या किसी Compatible Graphics Environment की आवश्यकता होती है।

This program demonstrates how graphics functions, loops, and delays can be used to create a simple animation in C. It helps students understand the basic working of computer graphics and animation.

यह प्रोग्राम C भाषा में Graphics Functions, Loops तथा Delay की सहायता से Animation बनाने की प्रक्रिया को समझाता है।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025