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

C Graphics Mini Projects

These graphics mini projects, such as a clock or a paint brush, pull together what you have learned about drawing, like building small models after learning each part. They are fun ways to practice.

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

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 < 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;
}

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

c
#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;
}

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

c
#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;
}

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

c
#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;
}

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

c
#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;
}
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. #}
Common Mistakes
  1. Redrawing without clearing, so old frames pile up on the screen.
  2. Forgetting delay so the animation runs too fast to see.
  3. Forgetting to close the graphics mode at the end of the program.
Chapter Summary
  • C graphics work uses the graphics.h library 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.

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.