Group No.
18
             Computer Graphics Mini Project
Sr.   Group Members       Rol No.    Signature
NO
01    Pranjali Wankhede   364
02    Renuka Patil        330
03    Mahesh Patil        332
Date :                                     Signature :
Title : A Man Walking in the Rain, Holding an Umbrella,
                  Using Graphics in C
Introduction : This project showcases a simple animated scene where
a man is holding an umbrella and walking. The animation is created
using basic graphics functions in C language. The man’s movement
under the umbrella simulates a rainy day walk, highlighting how
programming can bring dynamic visuals to life. The project aims to
demonstrate the use of loops, conditions, and graphics libraries in C,
providing a creative approach to learning animation concepts in a
beginner-friendly manner.
Description : This project is an animation created using the C
programming language, where a man is walking in the rain while
holding an umbrella. The animation simulates the motion of the
man’s walk and the falling rain, providing a dynamic visual
experience. Using the graphics.h library in C, the project incorporates
essential programming techniques like loops, conditions, and
coordinate manipulation to bring the scene to life.
The man’s figure is drawn using simple shapes, and his movement
across the screen mimics the natural pace of walking. The rain is
represented by falling lines, adding to the realism of the scene. This
project showcases how C language, often considered fundamental
for system-level programming, can also be used to create graphical
animations.
By completing this project, students will gain practical experience
with C's graphics capabilities, learn how to control object movement,
and understand the basics of animation programming.
Code :
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>
// Structure to hold raindrop data
struct Raindrop {
     int x, y, speed;
};
// Function to initialize raindrops with random positions and speeds
void initRain(struct Raindrop raindrops[], int numDrops) {
     for (int i = 0; i < numDrops; i++) {
         raindrops[i].x = rand() % getmaxx();
         raindrops[i].y = rand() % getmaxy();
         raindrops[i].speed = rand() % 5 + 2; // Random speed for variation
     }
}
// Function to draw falling raindrops and update their positions
void drawRain(struct Raindrop raindrops[], int numDrops) {
     setcolor(CYAN);
     for (int i = 0; i < numDrops; i++) {
         line(raindrops[i].x, raindrops[i].y, raindrops[i].x, raindrops[i].y + 10);
        raindrops[i].y += raindrops[i].speed; // Move the raindrop downward
        // Reset raindrop position if it moves beyond the screen
        if (raindrops[i].y > getmaxy()) {
            raindrops[i].y = 0;
            raindrops[i].x = rand() % getmaxx();
        }
    }
}
// Function to draw the umbrella
void drawUmbrella(int x, int y) {
    setcolor(WHITE);
    // Umbrella canopy
    arc(x, y, 0, 180, 30); // Arc for umbrella top
    line(x - 30, y, x + 30, y); // Bottom line of umbrella
    line(x, y, x, y + 40); // Handle
}
// Function to draw a walking man with umbrella
void drawMan(int x, int y, int step) {
    // Draw head
    setcolor(WHITE);
    circle(x, y + 50, 10);
    // Draw body
    line(x, y + 60, x, y + 100);
    // Draw arms
    line(x, y + 70, x - 20, y + 85); // Left arm
    line(x, y + 70, x + 20, y + 60); // Right arm holding umbrella
    // Draw legs with alternating movement
    if (step % 2 == 0) {
        // Left leg forward, right leg back
        line(x, y + 100, x - 10, y + 120); // Left leg forward
        line(x, y + 100, x + 10, y + 110); // Right leg back
    } else {
        // Right leg forward, left leg back
        line(x, y + 100, x - 10, y + 110); // Left leg back
        line(x, y + 100, x + 10, y + 120); // Right leg forward
    }
}
// Function to draw background (can be modified to include more scenery)
void drawBackground() {
    setcolor(WHITE);
    // Draw some ground
    line(0, getmaxy() - 50, getmaxx(), getmaxy() - 50);
}
// Function to draw the complete scene
void drawScene(int x, int y, int step, struct Raindrop raindrops[], int numDrops)
{
    cleardevice(); // Clear the screen
    // Draw background elements
    drawBackground();
    // Draw rain
    drawRain(raindrops, numDrops);
    // Draw the man walking with umbrella
    drawUmbrella(x, y);
    drawMan(x, y, step);
}
// Main animation loop
int main() {
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
    // Man's starting position and step count
    int x = 50, y = 200;
    int step = 0;
    // Define number of raindrops
    const int numDrops = 300;
    struct Raindrop raindrops[numDrops];
// Initialize raindrops
initRain(raindrops, numDrops);
// Main loop to animate the man and rain
while (!kbhit()) { // Run until a key is pressed
  drawScene(x, y, step, raindrops, numDrops);
  // Update the man's position and step for walking animation
  x += 5;
  step++;
  // Reset man's position if he walks off-screen
  if (x > getmaxx()) {
      x = 0;
  }
  // Control the speed of the animation
  delay(100);
} closegraph();
return 0;}