← Back to C Course | Chapter 13: Graphics | Lesson 7 of 10

C में Animation की Basics

Animation तेज़ी से थोड़ी अलग pictures दिखाना है ताकि चीज़ें move करती लगें, एक flipbook की तरह। C में आप हर बार एक shape को थोड़ा आगे erase और redraw करते हैं।
Syntax
c
while (condition) {
    shape(x, y, ...);   // draw
    delay(milliseconds);
    cleardevice();      // erase
    x = x + step;       // update position
}

एक Shape Move करना

BGI graphics में basic animation loop है: एक shape draw करें, थोड़ी देर pause करें, वह shape erase करें, इसकी position variables update करें, फिर इसे नई location पर दोबारा draw करें — इस cycle को तेज़ी से दोहराना ही screen पर smooth motion का illusion बनाता है।

उदाहरण: Moving a Shape

c
#include <graphics.h>
// Program execution starts in main()
int main() {
	initgraph(0, 0, "");
	// Declare x and set it to 0
	int x = 0;
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		circle(x, 100, 10);
		delay(100);
		cleardevice();
		x += 20;
	}
	closegraph();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Speed Control करना

delay() program execution को आपके specify किए milliseconds की संख्या के लिए pause करता है, और यही एक animation की perceived speed control करता है: frames के बीच एक बड़ा delay value motion को धीमा दिखाता है, जबकि एक छोटा इसे तेज़ दिखाता है।

उदाहरण: Controlling Speed

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	circle(50, 50, 10);
	delay(500);
	closegraph();
	return 0;
}

Flickering रोकना

frames के बीच पूरी screen erase करने के लिए cleardevice() call करना काम करता है, लेकिन यह visible flickering भी cause करता है क्योंकि पूरा display एक पल के लिए blank हो जाता है; सिर्फ पिछले shape को background color में दोबारा draw करके erase करना, फिर नया frame draw करना, उस flicker से बचता है।

उदाहरण: Preventing Flickering

c
#include <graphics.h>
// Program execution starts in main()
int main() {
	initgraph(0, 0, "");
	// Declare x and set it to 0
	int x = 0;
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		setcolor(BLACK);
		circle(x, 100, 10);
		x += 10;
		setcolor(WHITE);
		circle(x, 100, 10);
		delay(100);
	}
	closegraph();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Bouncing Animations

एक bouncing animation एक shape की position के साथ एक direction variable (जैसे +1 या -1) track करता है, और shape के coordinates screen boundary तक पहुँचते ही उस direction का sign reverse कर देता है, जो इसे screen से बाहर जाने की बजाय edge से bounce करता दिखाता है।

उदाहरण: Bouncing Animations

c
#include <graphics.h>
// Program execution starts in main()
int main() {
	initgraph(0, 0, "");
	int x = 0, direction = 1;
	// Loop: repeat while the condition holds
	for (int i = 0; i < 5; i++) {
		// Check whether x >= 100 || x <= 0
		if (x >= 100 || x <= 0) {
			direction = -direction;
		}
		x += direction * 10;
		delay(50);
	}
	closegraph();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Keyframe Based Updates

कई shapes को साथ animate करने का मतलब है सबको दोबारा draw करने से पहले same loop से एक बार गुज़रते हुए हर shape की position variables update करना, जो उनकी movements को अलग independent loop पर हर shape चलाने की बजाय same timing से synchronized रखता है।

उदाहरण: Keyframe Based Updates

c
#include <graphics.h>
// Program execution starts in main()
int main() {
	initgraph(0, 0, "");
	int x1 = 0, x2 = 200;
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		circle(x1, 100, 10);
		circle(x2, 100, 10);
		x1 += 10;
		x2 -= 10;
		delay(100);
	}
	closegraph();
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. delay skip करना, ताकि shape देखने के लिए बहुत तेज़ move करे।
  2. दोबारा draw करने से पहले cleardevice() से पुराना shape clear न करना, जो एक trail छोड़ता है।
  3. double buffering इस्तेमाल करने की बजाय हर frame पूरी screen clear करना, जो flicker cause करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.