Graphics in C – Program for Moving a Ball on Screen
Here is a simple graphics program
in C computer language, which
demonstrates the animation of a moving ball. This ball changes its direction
and color when it reaches and touches a boundary. Actually, ball does not move.
New graphic of the ball is being generated, and old graphic of the ball is
being erased continuously, thus giving the illusion of motion. To clear the
point, please type this program in your editor.
#include<graphics.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<dos.h>
void main()
{
int gd = DETECT, gm;
int x, y, maxx, maxy;
int xdir = 1, ydir = 1;
int color = 1;
initgraph(&gd, &gm,
"c:\\tc\\bgi");
maxx = getmaxx();
maxy = getmaxy();
x = 25;
y = 45;
randomize();
while(!kbhit())
{
cleardevice();
rectangle(0,
20, maxx, maxy-20);
outtextxy(maxx/2-80,
0, "Moving Ball");
outtextxy(maxx/2-80,
maxy-15, "Press any key to exit...");
setcolor(WHITE);
setfillstyle(SOLID_FILL, color);
circle(x,
y, 25);
floodfill(x,
y, WHITE);
delay(40);
x = x + (xdir * 5);
y =
y + (ydir * 5);
if(x >= maxx - 25 || x <= 25)
{
xdir *= -1;
color = random(15) + 1;
}
if(y
>= maxy - 45 || y <= 45)
{
ydir *= -1;
color = random(15) + 1;
}
}
closegraph();
restorecrtmode();
}
Compile and execute this
program. In the output, there is a moving ball, which changes its color and
direction whenever it touches any boundary. It moves inside a rectangular area.
In the given program I have
declared two integer type variables ‘xdir’
and ‘ydir’ and initialized with the
value ‘1’, two integer type
variables ‘x’ and ‘y’, and given the initial value ‘25’, and ‘45’, an integer type variable ‘color’
with initial value ‘1’.
I have found maximum screen coordinates
with these statements.
maxx = getmaxx();
maxy = getmaxy();
I initialized a random
number generator with an initial random value.
randomize();
I started a ‘while’ loop. The statements in this loop will be executed
endlessly till the user presses any key on the keyboard.
while(!kbhit())
{
…
…
…
}
Function kbhit() checks to see if a keystroke is currently available.
If a key is pressed, it returns a non-zero integer. If a key is not pressed, it
returns ‘0’.
Declaration:
int kbhit(void);
Syntax for calling this
function is:
kbhit();
Therefore, !kbhit() will evaluate to ‘1’, if a key is not pressed, and while loop will remain true. If a
key is pressed, !kbhit() will evaluate to ‘0’, while loop becomes false, and
terminates. For function kbhit(), file “dos.h” should be included.
In the while loop, function cleardevice() will clear the screen every time the loop
executes, and function rectangle() will draw the boundary
walls for the moving ball.
cleardevice();
rectangle(0, 20, maxx, maxy-20);
Function outtextxy() has been used to display appropriate messages on
the screen.
outtextxy(maxx/2-80, 0, "Moving
Ball");
outtextxy(maxx/2-80, maxy-15,
"Press any key to exit...");
Function setfillstyle() has been used to set the color and fill style
of the ball, function circle() for drawing a circle, and function floodfill() for filling the color in the circle or ball.
setfillstyle(SOLID_FILL, color);
circle(x, y, 25);
floodfill(x, y, WHITE);
In the function setfillstyle(), second argument is ‘color’, which is a variable, and has been given an initial value ‘1’ (enumerated value for blue color),
then its value will be picked randomly during the execution of an ‘if’ construct, which checks whether the ball touches the
boundary wall or not. If touches, then value between ‘1’ and ‘15’ will be
given to the ‘color’ variable. This
way, ball changes its color after touching and bouncing back the boundary.
if(x >= maxx - 25 || x <= 25)
{
xdir *= -1;
color = random(15) + 1;
}
if(y >= maxy - 45 || y <= 45)
{
ydir *= -1;
color = random(15) + 1;
}
I have added ‘1’ in the random number, because I
don’t want to give the value ‘0’ to ‘color’, which is the color code for
black color. What is the meaning of statements xdir *= -1,
ydir *= -1, I’ll explain later
on.
For smooth movement of ball,
I have used the function delay(), and given the delay of 40 milliseconds. To change the position
of ball, I have incremented or decremented the values of coordinates of the
center of the ball (x, y).
delay(40);
x = x + (xdir * 5);
y = y + (ydir * 5);
Function delay() suspends the execution of the current program for
the time specified in milliseconds.
Declaration:
void delay(unsigned milliseconds);
Syntax for calling this
function is:
delay(milliseconds);
For function delay(), file “conio.h” should be included.
When ‘xdir’ and/or ‘ydir’ are having the value ‘1’, value(s) of ‘x’ and/or ‘y’ will be incremented
by 5px in every iteration of ‘while’ loop. If either ‘xdir’
or ‘ydir’ or both has the value ‘-1’, as changed by ‘if’ construct, value of ‘x’ or ‘y’ or both will
be decremented by 5px.
In the ‘if’ construct, the
statement xdir *= -1 implies that if ball
touches any of the boundary wall, the value of ‘xdir’ will change to ‘-1’,
if it has the value ‘+1’, and will
change to ‘+1’, if it has ‘-1’ previously. Same way, statement ydir *= -1 can be explained. This way, ball changes the
direction of its movement.
So this, a very simple
program, is now well explained. You can find the explanation of all the
functions and terms, which I have not explained here, in my previous posts.
You would also like these programs given below:
C Program to draw a line
C Program to draw a circle and a point
C Program to draw different types of ellipses
C Program to draw arc, pie-slice, and rectangle
C Program to draw a polygon
C Program to draw filled polygon, 2-D bar, and 3-D bar
C Program to create the user-defined fill-pattern
C Program to explain different line-styles
C Progarm to explain functions moveto() and lineto()
C Program to explain moverel() and linerel()
C Program to draw an elliptical pie-slice using sector()
C Program to write text in different font styles using settextstyle()
C Program to justify text using settextjustify()
C Program to explain function setusercharsize()
C Program to define palette of colors
C Program to generate captcha code
C Program for moving a ball on screen
C Program for creating animated circles
Playing with mouse using C language
C Program to restrict mouse pointer in a window
C Program for getting the position of mouse pointer
C Program to create a clock
Interrupt Handling With C
No comments:
Post a Comment
Please give your comment