What delay() actually does
delay() pauses the program for a given number of milliseconds. In the classic Borland headers, it is declared in DOS.H, not GRAPHICS.H, but it is commonly used alongside graphics.h drawing loops. By itself, it does not create animation. Its job is to slow a loop down so the screen updates become visible as separate frames instead of finishing instantly.
If you remove the delay from an animation loop, the program often runs too fast to watch. If you make the delay too large, motion looks jerky and slow. That is why timing values like 20, 40, 60, or 100 milliseconds are common starting points.
The basic frame-by-frame animation pattern
- Start graphics mode.
- Draw the object at its current position.
- Pause briefly with
delay(). - Clear the old frame or redraw the background.
- Change the coordinates.
- Draw again.
That cycle repeats until you decide to stop. Once you understand this pattern, many animation questions become simple coordinate questions instead of mysterious graphics problems.
Simple moving ball example
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
int x = 60;
initgraph(&gd, &gm, "");
while (x < 560) {
cleardevice();
setcolor(YELLOW);
circle(x, 220, 30);
setcolor(WHITE);
outtextxy(20, 20, "Simple animation using delay()");
delay(40);
x += 5;
}
getch();
closegraph();
return 0;
}
This program works because each loop creates a fresh frame. The circle is drawn, the program pauses for 40 milliseconds, the screen clears, and then the x-coordinate moves to the right.
Why clearing or redrawing matters
If you keep drawing a moving object without clearing the old frame, you do not get animation. You get a trail of old positions. Sometimes that trail is useful for special effects, but most beginner animation tasks want a single moving object.
That is why functions such as cleardevice() or a full redraw of the background are so common in animation loops. The screen must reflect the current frame, not every past frame at once.
How to control speed
Speed depends on two things:
- Delay amount: lower delay means faster updates.
- Coordinate change: bigger jumps per frame mean faster movement.
For example, changing x += 5 to x += 10 makes the ball move faster across the screen even if the delay stays the same. Changing delay(40) to delay(80) slows the visible motion down.
Common animation mistakes in graphics.h
- No delay at all. The loop finishes before the eye can follow the motion.
- No clear or redraw. The object leaves a trail.
- Updating the wrong coordinate. Horizontal movement changes x. Vertical movement changes y.
- Forgetting screen limits. The object disappears because it moves outside the visible range.
How to grow this into better animations
After you understand one moving object, the next steps are natural:
- Bounce a ball by reversing direction when it hits the screen edge.
- Move text using outtextxy().
- Animate multiple objects with separate x and y variables.
- Use
setcolor()andsetfillstyle()for more visual clarity.
At that point, animation becomes a coordination problem between timing, screen clearing, and state updates.
Best way to learn this quickly
Paste the example into the online compiler. First change only the delay value. Then change only the x increment. Watching how each change affects the movement is the fastest way to understand animation fundamentals.