954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

animation program in C

Hi friends
I am a beginer in C. I want to make a simple program in which there
are two balls on the screen.
These two balls will Strike when the user presses any key ?
Please help me with full code snippets.

surender_kumar
Newbie Poster
11 posts since Feb 2009
Reputation Points: 9
Solved Threads: 0
 

Which Compiler are you using..?If Turbo C++..,You have to know little about the functions under graphics.h.

deepugtm
Junior Poster in Training
96 posts since Aug 2008
Reputation Points: 2
Solved Threads: 2
 

Actually this question has more to do with what OS(unless its, doubtfully, hardware specific).

Just use something like OpenGL or SDL, for graphics.

MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
 

I think the best graphics for beginners is allegro.I've just completed C,and I'm using it, I recommend you to try.For detail go to http://www.allegro.cc

deepugtm
Junior Poster in Training
96 posts since Aug 2008
Reputation Points: 2
Solved Threads: 2
 

Dude,,, It's a little harder to understand the graphics functions..

For this you must have the knowledge of the functions defined in graphics.h,,,,,:|

mayank.cracker
Newbie Poster
3 posts since May 2009
Reputation Points: 8
Solved Threads: 0
 

What is with all the Turbo C++ references?!! That compiler is a non-standard relic from another millennial! It's useless!

MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
 
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
int i=100,j=100,f1=1,f2=1,r=50,k;
l:
// printf("\n\nEnter the co-ordinate where from you want to start to move the circle : ");
// scanf("%d%d",&i,&j);
cleardevice();
// printf("\n\nEnter the redious : " );
// scanf("%d",&r);
// if(r<i||r<j) {printf("\nWrong entry, Try again.");goto l;}
cleardevice();
getch();
while(!kbhit())
{

setcolor(WHITE);
circle(i+1,j+1,r); // printf("%d, %d",i,j);
delay(2);
/* for(k=1;k<=10;k++)
{
setcolor(k);
circle(i+1,j+1,r-(k*r/10));
delay(1);
setcolor(BLACK);
circle(i+1,j+1,r-(k*r/10));
} */
setcolor(BLACK);

circle(i+1,j+1,r);
if(i<639-r&&f1==1)
{i++;f1=1;
}
else
{i--;
if(i==r) f1=1; else f1=-1;
}
if(j<480-r&&f2==1)
{j++;f2=1;}
else
{j--;
if(j==r) f2=1;else f2=-1;
}

}
}








Try this , Here is one single ball, read it try to upgrade into two balls
ankurbangal
Newbie Poster
1 post since Jul 2011
Reputation Points: 6
Solved Threads: 0
 
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
int i=100,j=100,f1=1,f2=1,r=50,k;
l:
// printf("\n\nEnter the co-ordinate where from you want to start to move the circle : ");
// scanf("%d%d",&i,&j);
cleardevice();
// printf("\n\nEnter the redious : " );
// scanf("%d",&r);
// if(r<i||r<j) {printf("\nWrong entry, Try again.");goto l;}
cleardevice();
getch();
while(!kbhit())
{

setcolor(WHITE);
circle(i+1,j+1,r); // printf("%d, %d",i,j);
delay(2);
/* for(k=1;k<=10;k++)
{
setcolor(k);
circle(i+1,j+1,r-(k*r/10));
delay(1);
setcolor(BLACK);
circle(i+1,j+1,r-(k*r/10));
} */
setcolor(BLACK);

circle(i+1,j+1,r);
if(i<639-r&&f1==1)
{i++;f1=1;
}
else
{i--;
if(i==r) f1=1; else f1=-1;
}
if(j<480-r&&f2==1)
{j++;f2=1;}
else
{j--;
if(j==r) f2=1;else f2=-1;
}

}
}








Try this , Here is one single ball, read it try to upgrade into two balls

Don't use "void main()", there are two proper main function headers, and they are:

int main(int argc, char *argv[]) and int main()

Using "void main()" can cause problems, for a detailed explanation as to what kind of problems it can cause please consult google.


and for your information if you ever want to upgrade to a newer compiler, (like Visual Studio Express, or MinGW) the first thing you should try is using STANDARD C++ headers, like instead of "fstream.h" it should be #include <fstream>

pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 111
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You