Reputation:
I was trying to make a simple animation of a stickman walking, in graphics
library in C
.
My code:
#include<stdio.h>
#include<graphics.h>
void swap(int *x, int *y){
int temp=0;
temp=*x;
*x=*y;
*y=temp;
}
int main(){
int gd=DETECT,gm=0;
int i=0;
int a=30+i;
int b=50+i;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
while(i!=600){
swap(&a,&b);
circle(40+i,40,30);
line(40+i,70,a+i,90);
line(40+i,70,b+i,90);
i++;
delay(10);
cleardevice();
}
getch();
closegraph();
return 0;
}
The problem is that, the monitor screen keeps flickering constantly while playing the animation and moreover, the leg positions are not swapping according to the swap(&b,&c)
function. Where am I going wrong here? Can someone please help me?
Fix: So, I realised that, there's no point in having the swap()
function as, swapping the lines will make the alignment same again for both of them (as a result it will look like the legs are not moving), so I decided to tweak some things here:
while(i!=600){
if(i%2==0){
circle(40+i,40,30);
line(40+i,70,(a+b)/2+i,90);
line(40+i,70,(b+a)/2+i,90);
}else{
circle(40+i,40,30);
line(40+i,70,a+i,90);
line(40+i,70,b+i,90);
}
i++;
delay(10);
cleardevice();
}
Now, it actually looks like the figure is walking. But I still don't know how to fix the screen flickering problem.
Upvotes: 0
Views: 507