C में Animation की Basics
In this page:
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
Speed Control करना
delay() program execution को आपके specify किए milliseconds की संख्या के लिए pause करता है, और यही एक animation की perceived speed control करता है: frames के बीच एक बड़ा delay value motion को धीमा दिखाता है, जबकि एक छोटा इसे तेज़ दिखाता है।
उदाहरण: Controlling Speed
#include <graphics.h>
int main() {
initgraph(0, 0, "");
circle(50, 50, 10);
delay(500);
closegraph();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Flickering रोकना
frames के बीच पूरी screen erase करने के लिए cleardevice() call करना काम करता है, लेकिन यह visible flickering भी cause करता है क्योंकि पूरा display एक पल के लिए blank हो जाता है; सिर्फ पिछले shape को background color में दोबारा draw करके erase करना, फिर नया frame draw करना, उस flicker से बचता है।
उदाहरण: Preventing Flickering
#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;
}
Login to try C/C++/Java/PHP code in the editor
Bouncing Animations
एक bouncing animation एक shape की position के साथ एक direction variable (जैसे +1 या -1) track करता है, और shape के coordinates screen boundary तक पहुँचते ही उस direction का sign reverse कर देता है, जो इसे screen से बाहर जाने की बजाय edge से bounce करता दिखाता है।
उदाहरण: Bouncing Animations
#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;
}
Login to try C/C++/Java/PHP code in the editor
Keyframe Based Updates
कई shapes को साथ animate करने का मतलब है सबको दोबारा draw करने से पहले same loop से एक बार गुज़रते हुए हर shape की position variables update करना, जो उनकी movements को अलग independent loop पर हर shape चलाने की बजाय same timing से synchronized रखता है।
उदाहरण: Keyframe Based Updates
#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;
}
Login to try C/C++/Java/PHP code in the editor
delayskip करना, ताकि shape देखने के लिए बहुत तेज़ move करे।- दोबारा draw करने से पहले
cleardevice()से पुराना shape clear न करना, जो एक trail छोड़ता है। - double buffering इस्तेमाल करने की बजाय हर frame पूरी screen clear करना, जो flicker cause करता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: