Search This Blog

Caterpillar Eating Leaf - Computer Graphics Lab Animation in C

This is a CG lab program in C using graphics.h library to animate a caterpillar eating a leaf. As it eats, the caterpillar (or worm) should grow proportionally in size. The leaf should also be reduced in size. The program is given below:
 The ouput is:




The program is:

#include<stdio.h>
#include<graphics.h>
void main()
{
int graD=DETECT,graMo;
int lwidth=70,lheight=30;
int midx,midy,lx,ty,eyex,eyey;
int tailx,taily,i,j,cutx,cuty;
initgraph(&graD,&graMo,"C:\\TURBOC3\\BGI\\");
midx=getmaxx()/2;
midy=getmaxy()/2;
setbkcolor(WHITE);
setcolor(YELLOW);
setfillstyle(SOLID_FILL,GREEN);
fillellipse(midx,midy,lwidth,lheight);
setcolor(GREEN);
lx=midx-lwidth-10;
ty=midy-lheight-20;
cutx=midx+lwidth;
cuty=midy;
line(lx,ty,lx,getmaxy());
line(lx,midy+lheight,midx-lwidth,midy);
taily=midy;
for(i=6;i<lwidth-25;i++)
 {
 setfillstyle(SOLID_FILL,WHITE);
 setcolor(WHITE);
 delay(250);
 fillellipse(cutx,cuty,i,8+i/10);
 lx=6+i/15;//wormcircle width/2
 ty=7+i/13;//height/2
 tailx=cutx-i;
 for(j=0;j<7;j++)
  {
  setcolor(WHITE);
  setfillstyle(SOLID_FILL,BROWN);
  fillellipse(tailx-j*4*lx/3,taily+((i+j)%2)*1,lx,ty);
  }
 j--;
 eyex=tailx-j*4*lx/3-3-i/15;
 eyey=taily+((i+j)%2)*1;
 setcolor(RED);
 setfillstyle(SOLID_FILL,RED);
 line(eyex-2,eyey-4-i/18,eyex-2-lx,eyey-6-i/18);
 line(eyex-2,eyey+4+i/18,eyex-2-lx,eyey+6+i/18);
 setcolor(YELLOW);
 setfillstyle(SOLID_FILL,YELLOW);
 fillellipse(eyex,eyey+3+i/18,2+i/25,2+i/22);
 fillellipse(eyex,eyey-3-i/18,2+i/25,2+i/22);
 }
getch();
}

No comments: