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>
int main() {
initgraph(0, 0, "");
int x = 0, direction = 1;
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;
}
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.h>
#include <time.h>
int main() {
initgraph(0, 0, "");
time_t now = time(NULL);
struct tm *local = localtime(&now);
double angle = local->tm_min * 6 * (3.14159 / 180);
int x = 150 + (int)(50 * sin(angle));
int y = 150 - (int)(50 * cos(angle));
line(150, 150, x, y);
closegraph();
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>
int main() {
initgraph(0, 0, "");
int heights[3] = {50, 80, 30};
for (int i = 0; i < 3; i++) {
bar(i * 50, 200 - heights[i], i * 50 + 30, 200);
}
closegraph();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: