0% found this document useful (0 votes)
59 views8 pages

C Program for Bouncing Ball Animation

This document describes a program that animates a bouncing ball within a screen boundary using C language. The ball is drawn using fillellipse and its position is updated within a loop, incrementing the x and y coordinates. If the ball reaches the screen edge, the coordinates are reversed to simulate bouncing. Multiple balls or random shapes could enhance the animation in the future. The program demonstrates basic graphics animation in C++.

Uploaded by

Vinod Marne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views8 pages

C Program for Bouncing Ball Animation

This document describes a program that animates a bouncing ball within a screen boundary using C language. The ball is drawn using fillellipse and its position is updated within a loop, incrementing the x and y coordinates. If the ball reaches the screen edge, the coordinates are reversed to simulate bouncing. Multiple balls or random shapes could enhance the animation in the future. The program demonstrates basic graphics animation in C++.

Uploaded by

Vinod Marne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

BOUNCING BALL

Using C language

By

S.SAGAR [AC08UCS074]

E.SAKTHIVEL[AC08UCS077]

B.E III CSE-B

ACE
PROBLEM DESCRIPTION:

To design a ball which bounces of the sides of the screen without moving out of focus.

REQUIREMENT ANALYSIS:

SYSTEM REQUIREMENTS:

 WINDOWS OPERATING SYSTEM

 TURBO C/C++

DESIGN:

• A ball is drawn using inbuilt function fillellipse .

• The size of the image is obtained by imagesize() and memory is allocated by malloc().

• It is stored using the function getimage().

• The getimage() will store the bitmap image of specified region of the screen into the
memory.

• We create a loop until ESC key is pressed.Then in another loop we print the ball on the
screen by using putimage().

• The putimage() will output the saved bitmap image from memory.

• We increment the coordinates of x and y by some steps.

• If the ball moves out of the positive X and Y coordinates ,we convert the coordinates
back to screen coordinates .

• This causes an illusion that the ball bounces of the screen.

• If escape key is pressed program exits.


IMPLEMENTATION:

#include<graphics.h>

#include<conio.h>

#include<alloc.h>

#include<dos.h>

#include<stdlib.h>

void *ball;

void image()

//ball

setcolor(RED);

setfillstyle(SOLID_FILL,RED);

fillellipse(10,10,10,10);

ball=malloc(imagesize(0,0,20,20));

getimage(0,0,20,20,ball);

cleardevice();
}

void main()

int gm,gd=DETECT;

initgraph(&gd,&gm,"");

int l=getmaxx()/2,r=0,t=0,b=0;

int x=1,y=1;

int s=0,key=0;

int xstep=1,ystep=1;

image();

setbkcolor(GREEN);

while(key!=27)

while(!kbhit()){

putimage(l,t,ball,XOR_PUT);

delay(5);
putimage(l,t,ball,XOR_PUT);

if(l>=getmaxx()||l<=0)

x*=-1;

s=0;

xstep=x*(random(4)+1);

ystep=y*(random(3)+1);

if(l<=0)

l=0;

else

l=getmaxx();

if(t>=getmaxy()||t<=0)

y*=-1;

ystep=y*(random(4)+1);

xstep=x*(random(3)+1);

if(t<=0)

t=0;
else

t=getmaxy();

l+=x+xstep;

t+=y+ystep;

s++;

if(s==5)

nosound();

key=getch();

closegraph();

}
RESULT:

The output of the program would be as below


FUTURE ENHANCEMENT:

o The ball changes shape randomly into shapes like square ,rectangle

o Multiple ball bouncing about randomly

CONCLUSION:

This animation was implemented using the language C++ due to its simplicity in using graphics

You might also like