Assignment No.
7
Aim: Write a C++ program to implement bouncing ball using sine wave
form. Apply the concept of polymorphism.
Theory:
What is animation?
Animation is the process of designing, drawing, making layouts and
preparation of photographic sequences which are integrated in the
multimedia and gaming products. Animation involves the exploitation and
management of still images to generate the illusion of movement. How to
move an element to left, right, up and down using arrow keys?
To detect which arrow key is pressed, you can use ncurses.h header file.
Arrow key's code is defined as: KEY_UP, KEY_DOWN. KEY_LEFT.
KEY_RIGHT.
Bouncing Ball Implementation:
You are given the "flat" code that draws a ball bouncing off the walls of a
rectangle (you can think of it as ball on the a rectangular pool table without
holes and friction). The ball rolls and reflects off the walls without losing
speed, so that it will bounce in wave form. Our eventual goal is to program
color ball that is reflected off the wall of the rectangular table and other
obstacles that may be on the table. In this lab we restrict ourselves to a
fixed rectangular field without obstacles.
We need to keep, in some integer variables, the borders (walls), and, for a
ball, the current coordinates and the direction of movement (actually, the
horizontal and vertical velocities). We will draw the ball, compute the next
position (coordinates) of the ball based on its coordinates and velocity
components, update the positions and re-draw after a suitable delay (so
that things don't happen in a blur).
Additionally, if the computed next position of a ball intersects with a wall,
we'll modify the direction of the ball's movement by changing its
horizontal and vertical velocities to its opposite. The "flat" give-away code
shows how this can be done a single ball. You must read, understand, and
play with this simple code.
Code Overview:
Code starts from including <iostream> as it is code in cpp, <graphics> as it
is graphics program so it could include all require methods for graphics and
define getmaxx() as width and getmaxy() as height so it can replace
function by alias name. As program execution starts from main function,
we include graphics detecter and graphics mode. "getmax" function is
used to get mid position of X-Axis and Y-Axis.
Loop is used for repeating the process several times. As we have pass
terminating condition, for loop condition will terminate when ball reaches
end of x Axis. "if" condition is used to move ball up and down in wave form.
We require a ball to bounce, so we draw a circle using simple graphics
build-in function and color it with any color of RGB. Also we required a
rectangular box on which our ball will bounce. To watch the ball bouncing,
we have used delay.
Algorithm:
Step1: Start
Step2: Include all necessary header files used in graphics programing
Step3: Define HEIGHT, WIDTH, GROUND, MAXHEIGHT as getmaxx (),
getmaxy (), etc.
Step4: Declare Variable x and initialize y=0, c=1 and t=MAXHEIGHT
Step5: Draw a rectangle (ground) to bounce ball from x=40
Step6: Repeat step 5 until x<= getmaxx ()
Step 7: Fill the rectangle (ground) with white colour using flood fill
algorithm
Step8: Draw a ball (circle) and colour it using flood fill algorithm
Step9: If y > MAXHEIGHT - 20 Put c=0 and t = t - 40 (This condition is for
ball to bounce in waveform)
Step10: If y <= (MAXHEIGHT - t) Put c as 1 and check further condition
Step11: If t>=40 check if c is zero or not; If c = = 0 put y = y-15 Else put y =
y+15
Step12: Stop.
Conclusion: It help us to understand basic functions and modes to design
small program.