This lab is due by Tuesday, May 04, 2021, 11:59 PM.
As with all labs you may, and are encouraged, to pair program a solution to this lab. If you choose to pair program a solution, be sure that you individually understand how to generate the correct solution.
Concepts
You are going to create a magic bouncing ball to watch as it moves around the screen.
Class Creation
For this lab you must first create a class called
Bubble
. The
Bubble
class should have private data members to store the following
information:
- a
CircleShape
which will be used to draw the Bubble - two
double
s to store the X and Y direction respectively the Bubble should move (call themxDir
andyDir
)
Be sure to have the necessary getters and setters for your private data
members. You may make non-default constructors as you deem necessary to
set initial values on your
Bubble
class. Additionally, we want one public member function:
updatePosition()
: which should add the direction values to the position of theCircleShape
Draw It!
In our
main()
, before we get to the draw loop we want to make an object of our
Bubble
class. We want only one instance to exist in memory. If we made the
object inside the draw loop, then every iteration of the draw loop a
new object would be created.
Next, inside our draw loop, after
we've cleared the window and before we display the window, we need to
display the
CircleShape
that is a member of our
Bubble
object.
At this point, you should see a white circle. Perfect!
Let's continnue.
Move It!
When we create our
Bubble
object, we need to make sure its direction values are set to non-zero
values. Do this now, we suggest using values of 0.1
& 0.2
for x & y
respectively.
Then in our draw loop, after we've checked the
events we simply need to call our
updatePosition()
function on the object. It should be moving now. Oh no! It went off the
screen. Let's keep it in view.
Bop It!
The last part of this lab is have the Bubble bounce around the window.
Recall that the position for a
CircleShape
corresponds to the upperleft corner of a square enclosing the circle.
We need to check if any of the edges of our circle move outside the
window. We can check this by seeing if the position becomes less than
zero (to correspond to the left or top of our window) OR if the position
plus the diameter is greater than the width or height of our window (to
correspond to the right or bottom of our window). If any of these
conditions occur, then we simply need to reverse the corresponding
direction of our Bubble to simulate a bounce. Once we've checked
all four sides, we're contained!
We recommend to start with the right wall first, then bottom, then
left, then top. Get them working one at a time and keep adding to
your code.
Excellent. Move on to A8 . You will continue right where this Lab left off.
Lab Submission
You will submit your solution to this lab with the rest of Set8. Detailed instructions for doing this are posted in Assignment 8.
This lab is due by Tuesday, May 04, 2021, 11:59 PM.
As with all labs you may, and are encouraged, to pair program a solution to this lab. If you choose to pair program a solution, be sure that you individually understand how to generate the correct solution.