C and C++ Computer Graphics

program for man object moving

In computer graphics, animation is created by displaying objects in different positions over time. A simple example is a man object moving across the screen using C graphics programming. This program uses the <graphics.h> library to draw a stick figure and simulate walking motion by updating its position step by step.

#include <graphics.h>
#include <conio.h>
#include <dos.h>

int main() {
int gd = DETECT, gm;
int x = 50, y = 300; // starting position of man

initgraph(&gd, &gm, “”);

for (int i = 0; i < 200; i++) {
cleardevice();

// Head
circle(x, y – 50, 20);

// Body
line(x, y – 30, x, y + 30);

// Arms
line(x, y – 20, x – 30, y);
line(x, y – 20, x + 30, y);

// Legs (walking effect by alternation)
if (i % 2 == 0) {
line(x, y + 30, x – 20, y + 60); // left forward
line(x, y + 30, x + 20, y + 60); // right backward
} else {
line(x, y + 30, x – 20, y + 60); // left backward
line(x, y + 30, x + 20, y + 60); // right forward
}

delay(80); // control speed
x += 5; // move right
}

getch();
closegraph();
return 0;
}

Explanation of the Program

  1. Graphics Initialization

    • initgraph() starts graphics mode.

    • gd and gm detect the graphic driver and mode.

  2. Drawing the Man

    • circle() → draws the head.

    • line() → used for body, arms, and legs.

  3. Walking Animation

    • A loop (for) repeatedly clears the screen and redraws the man.

    • x += 5 shifts the figure to the right.

    • Legs move alternately using if (i % 2 == 0) to create a walking effect.

  4. Frame Control

    • cleardevice() removes the previous frame.

    • delay(80) gives smooth animation speed.

 Output

  • A stick-man is drawn on the screen.

  • The man moves from left to right while legs alternate to simulate walking.

Leave a comment

Your email address will not be published. Required fields are marked *