C Graphics Mini Projects
In this page:
Classic Screensaver
A bouncing-shapes screensaver combines several shapes each tracking their own position and direction variables, reversing each shape's direction independently whenever it reaches a screen edge, which is the same bounce logic from animation basics applied to multiple objects at once.
Example: Classic Screensaver
#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 < 3; i++) {
circle(x, 100, 10);
if (x >= 200 || 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
Interactive Paint Brush
An interactive paint program listens for mouse clicks (or drags) and, on each detected click, draws a small shape at the current cursor coordinates, building up a picture on screen from the user's own input rather than from hardcoded shape calls.
Example: Interactive Paint Brush
#include <graphics.h>
#include <dos.h>
int main() {
initgraph(0, 0, "");
union REGS in, out;
in.x.ax = 3;
int86(0x33, &in, &out);
int x = out.x.cx, y = out.x.dx;
circle(x, y, 3);
closegraph();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Analog Clock
An analog clock face uses trigonometry to convert the current time into hand positions: the angle for the minute hand comes from the current minute value, and applying sin() and cos() to that angle computes the (X, Y) endpoint each hand should be drawn to.
Example: Analog Clock
#include <graphics.h>
// Include math functions (sqrt, pow, ...)
#include <math.h>
// Include time functions
#include <time.h>
// Program execution starts in main()
int main() {
initgraph(0, 0, "");
time_t now = time(NULL);
struct tm *local = localtime(&now);
double angle = local->tm_min * 6 * (3.14159 / 180);
// Declare x and set it to 150 + (int)(50 * sin(angle))
int x = 150 + (int)(50 * sin(angle));
// Declare y and set it to 150 - (int)(50 * cos(angle))
int y = 150 - (int)(50 * cos(angle));
line(150, 150, x, y);
closegraph();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Simple Traffic Light Simulator
A traffic-light simulator cycles through drawing red, yellow, and green filled circles in sequence, pausing with delay() between each state change, which is a practical way to combine timed animation with simple state-machine logic in one small program.
Example: Simple Traffic Light Simulator
#include <graphics.h>
int main() {
initgraph(0, 0, "");
setfillstyle(SOLID_FILL, RED);
fillellipse(100, 50, 20, 20);
delay(500);
setfillstyle(SOLID_FILL, GREEN);
fillellipse(100, 150, 20, 20);
closegraph();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Dynamic Bar Chart
A dynamic bar chart draws a series of filled rectangles whose heights are proportional to a set of numeric values, and animating those heights growing from zero up to their final value over several frames turns a static chart into a visually engaging reveal.
Example: Dynamic Bar Chart
#include <graphics.h>
// Program execution starts in main()
int main() {
initgraph(0, 0, "");
int heights[3] = {50, 80, 30};
// Loop: repeat while the condition holds
for (int i = 0; i < 3; i++) {
bar(i * 50, 200 - heights[i], i * 50 + 30, 200);
}
closegraph();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Redrawing without clearing, so old frames pile up on the screen.
- Forgetting
delayso the animation runs too fast to see. - Forgetting to close the graphics mode at the end of the program.
- C graphics work uses the
graphics.hlibrary and SDL2 to draw on screen. - You draw shapes, lines, curves, colors, and text, and can add animation and mouse input.
- Mini projects combine these pieces into small graphical programs.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: