Browse » Home
»
Moving the Insect that created in last program
» Moving the Insect that created in last program
Moving the Insect that created in last program
Moving the Insect that created in last program
In the last program you learnt how to make the insect.
Now you are going to learn how to move it.
Moving the insect is very easy. Just add a for and while loop and use the kbhit() function.
Also add delay() and cleardevice() functions.
You have to change the x-axis position using for loop.
If you want to move the insect in forward direction just subtract the midx values using
for loop.
If you want to move the insect in backward direction just add the midx values using
for loop.
Hence in the below program, subtract for loop value from all midx used in fillellipse().
Important :
Do not forget to add cleardevice() , otherwise you will get overlapped result.
#include <graphics.h>
#include <conio.h>
#include <dos.h>
void main()
{
int d;
int m;
d=DETECT;
int midx;
int midy;
int i;
initgraph(&d,&m,"d:\\tc\\bgi");
midx=getmaxx()/2;
midy=getmaxy()/2;
while(!kbhit())
{
for(i=1;i<100;i++)
{
if(kbhit())
{
break;
}
setfillstyle(2,6);
fillellipse(midx-i,midy,100,50); //midx value is decremented by subtracting i from it
setfillstyle(1,6);
fillellipse(midx-i,midy-70,20,20); //midx value is decremented by subtracting i from it
setfillstyle(1,6);
fillellipse(midx-60-i,midy-61,20,20);
setfillstyle(1,6);
fillellipse(midx+60-i,midy-61,20,20);
setfillstyle(1,6);
fillellipse(midx-i,midy+70,20,20);
setfillstyle(1,6);
fillellipse(midx-60-i,midy+61,20,20);
setfillstyle(1,6);
fillellipse(midx+60-i,midy+61,20,20);
setfillstyle(1,6);
fillellipse(midx-100-i,midy,40,40);
setfillstyle(1,8);
fillellipse(midx-120-i,midy-20,7,7);
setfillstyle(1,8);
fillellipse(midx-120-i,midy+20,7,7);
floodfill(midx-120-i,midy+20,15);
delay(200);
cleardevice();
}
}
getch();
}
Output :
You will see this insect moving horizontaly :
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment