Maharashtra State
Board of Technical Education
Certificate
This is to certify that Mr. Dnyaneshwar Dilip Garad with Roll No-35 has successfully
completed Micro-project in course Data Structure Using C (312301)for the academic year
2024-25 Microproject is completing Third Semester of Diploma Programme in Information
Technology from institute, PK Technical Campus Pune
Place: Chakan Enrollment No: 23213510047
Date: Exam Seat No:
Course Teacher Head of the Department Head of the
Institute
1
Computer Graphics
TITLE OF THE PROJECT: BOUNCING BALL
Bouncing Ball Simulation in Computer Graphics: Detailed Information and Explanation
Overview:
The bouncing ball simulation is a classic example used in computer graphics to
demonstrate basic principles of motion, collision detection, and graphical rendering. In
this simulation, a ball is animated within a rectangular window, where it continuously
moves, bouncing off the screen boundaries (walls), thus simulating realistic motion. The
core mechanics involve updating the position of the ball based on its speed and direction,
and reversing its motion (or "bouncing") when it reaches the screen edges.
In this case, we'll be using C programming along with the graphics.h library to create
this simulation. Though graphics.h is an older library primarily used in Turbo C/C++ IDE
for DOS and Windows environments, it is still useful for understanding fundamental
concepts of graphical rendering and motion in programming.
Let's break down the key concepts and details involved in creating a bouncing ball
simulation
1. A bouncing ball simulation demonstrates how an object moves and interacts with
the environment in a 2D space. The ball in this case moves across the screen, and
its movement is altered when it comes in contact with any boundary. The most
straightforward version of this simulation involves the ball moving at constant
speed, bouncing back whenever it hits one of the four walls of the window.
2
Computer Graphics
Features:
• The ball will move across the screen.
• The ball will bounce when it hits the edges of the window (left, right, top, or bottom).
• The ball's motion is simulated by updating its position continuously.
Bouncing Ball Program :-
1. Include Necessary Libraries:
o #include <graphics.h>: This is the main library for graphical functions in Turbo C++.
o #include <conio.h>: Used for functions like kbhit(), which checks if a key has been
pressed.
o #include <stdio.h>: Standard I/O functions (though not directly used in this
program).
o #include <dos.h>: Provides the delay() function for controlling the speed of the
animation.
2. Constants and Variables:
o #define LEFT 0, RIGHT 640, TOP 0, BOTTOM 480: These define the boundaries of the
window where the ball will move (a 640x480 resolution window).
o ballRadius = 20: The ball's radius is set to 20 pixels.
o x = 320, y = 240: Initial position of the ball is set to the center of the window (320,
240).
o xSpeed = 5, ySpeed = 5: The speed of the ball in both the horizontal (X) and vertical
(Y) directions.
3. Drawing the Ball (drawBall()):
o setfillstyle(SOLID_FILL, RED): Sets the fill style for the ball to a solid red color. o
floodfill(x, y, WHITE): Draws the ball at position (x, y) using the color red.
4. Ball Movement (moveBall()):
o x += xSpeed; y += ySpeed;: Updates the position of the ball based on its speed.
o If the ball touches the left or right boundaries, its horizontal speed is reversed
(xSpeed = -xSpeed), making the ball bounce back horizontally. o If the ball
touches the top or bottom boundaries, its vertical speed is reversed (ySpeed = -
ySpeed), making the ball bounce back vertically.
5. Main Loop:
o The while (!kbhit()) loop keeps running until the user presses a key (detected by
kbhit()).
o cleardevice(): Clears the screen for the next frame of the animation.
o drawBall(x, y): Redraws the ball at its updated position. o moveBall(): Updates the
ball’s position based on its velocity.
o delay(10): Introduces a small delay between each frame to control the ball's speed.
3
Computer Graphics
6. Exit Condition:
o When a key is pressed (kbhit() returns true), the loop ends and the program exits
the graphics mode using closegraph().
REQUIREMENTS:-
1. Turbo C++ IDE (or Turbo C++ Compiler) with access to the graphics.h library, which is
used for drawing shapes and animations.
2. The BGI files (Borland Graphics Interface) should be set up correctly in Turbo C++.
PROGRAM :-
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <dos.h> // For delay function
// Define screen boundaries
#define LEFT 0
#define RIGHT 640
#define TOP 0
#define BOTTOM 480
// Ball properties
int ballRadius = 20; int x = 320, y = 240; // Initial position of the ball
(center) int xSpeed = 5, ySpeed = 5; // Speed of ball in X and Y
directions
void drawBall(int x, int y) {
4
Computer Graphics
// Draw a filled circle (ball) at (x, y) with a radius of
'ballRadius' setfillstyle(SOLID_FILL, RED); // Red color
floodfill(x, y, WHITE);
void moveBall() {
// Move the ball by changing its x and y
positions
x += xSpeed;
y += ySpeed;
// Bounce off left and right walls
if (x - ballRadius < LEFT || x + ballRadius > RIGHT)
{ xSpeed = -xSpeed; // Reverse the horizontal
direction
// Bounce off top and bottom walls
if (y - ballRadius < TOP || y + ballRadius > BOTTOM)
{ ySpeed = -ySpeed; // Reverse the vertical
direction
int main() {
5
Computer Graphics
int gd = DETECT, gm;
// Initialize the graphics mode
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
// Main loop for the bouncing ball animation
while (!kbhit()) {
cleardevice(); // Clear the screen for the next frame
drawBall(x, y); // Draw the ball at its new position
moveBall(); // Update ball's position based on its speed
delay(10); // Delay to control the speed of the ball's
movement
// Close graphics mode when the user presses a key
closegraph();
return 0;
6
Computer Graphics
Result :