April 1, 2011

Bouncing Ball Code in C

Here is another exclusive code by MY C FILES.
Bouncing ball code in C, This program is build by using graphic.h header file. Graphic drivers, graphic mode are present in BGI folder of TC i.e. C:\\tc\\bgi



This is a Pink ball bouncing against the borders of window screen.

Code:
*Copyright Protected by mycfiles.com
bouncing ball in c*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>
#include<stdlib.h>
void main()
{
int dr,md,midx,i, midy,dx,dy,mx, my,k,x,y,xdir,ydir,oldx,oldy;
double pi,tpi,a,t;
void *ball;

unsigned imgsize;
dr = DETECT;

initgraph(&dr, &md, "c:\\tc\\bgi");
midx = getmaxx()/2; midy = getmaxy()/2;
pi = 3.14159;
tpi = pi * 2.0;
setcolor(13);
circle(midx,midy,10);
imgsize = imagesize(midx-10, midy-10, midx+10, midy+10);
ball = malloc(imgsize);
getimage(midx-10, midy-10, midx+10, midy+10,ball);
putimage(midx-10, midy-10, ball,XOR_PUT);


xdir = -5; ydir=5;

do
{
oldx =  midx; oldy = midy;
midx = midx + random(xdir);
midy = midy + random(ydir) ;

if (midx < 0)
{
midx = midx + 5;
xdir =5;
}
if (midx > getmaxx())
{
midx = midx - 5;
xdir = -5;
}
if (midy < 0)
{
midy = midy + 5;
ydir = 5;
}
if (midy > getmaxy())
{
midy = midy - 5;
ydir = -5;
}

putimage(midx-10, midy-10,ball,XOR_PUT);
for (a=1.0; a<30000.0; a=a+0.1);
{
}
putimage(midx-10, midy-10,ball,XOR_PUT);


} while (!kbhit());

getch();
}

Note: Note: this program will run only on windows xp and previous versions of windows. Reason is windows newer versions are not compatible with turbo c++ 3.0 graphic header files

Hope you like the post, express your feelings about code by comment... :)

Related Posts :



Categories:

3 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Related Posts with Thumbnails