→This assignment is due by Tuesday, April 01, 2025, 11:59 PM.←
→ As with all assignments, this must be an individual effort and cannot be pair programmed. Any debugging assistance must follow the course collaboration policy and be cited in the comment header block for the assignment.←
Jump To: Rubric Submission
Concepts
You are going to create magic bouncing balls and pop them as they move around the screen. As with the prior lab, you will want to start the SFML Template.
Class Creation
For this assignment you must first create a class calledBubble
. The
Bubble
class needs to have the following private data members to store
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 and/or you may make non-default constructors as you deem necessary to
set initial values on your
Bubble
class.
Draw It!
In ourmain()
, before we get to the draw loop we want to make our
Bubble
object. 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 draw
the bubble. Add a public method to the Bubble
class called draw()
that accepts an
SFML RenderWindow
object by reference. The function then draws the CircleShape
member of the class.
At this point, run your program and you should see a white circle. Perfect! Let's continue.
Move It!
We need to add another public member function called updatePosition()
. This function needs to add the direction values
to the position of the CircleShape
.
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 in the range of [0, 0.2]
for xDir & yDir
respectively. (Note: feel free to tweak these values up/down if it moves too slow/fast.)
Now in our draw loop, after we've checked the
events we will check the time. If the elapsed time has eclipsed 1/60th of a second, then call
updatePosition()
on the object.
Run your program. It should be moving now. Oh no! It went off the screen. Let's keep it in view.
Bop It!
The last part of the bubble is have it bounce around the window.
Recall that the position for a
CircleShape
corresponds to the upper left 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!
Modify Bubble::updatePosition()
so that it accepts
the window width and height as parameters. Now after moving the bubble,
check if the bubble went over a wall. If it did, move the bubble backwards
and then reverse the necessary direction.
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.
Run the program and watch it stay within the window. Excellent.
Bubbles Bubbles Everywhere
We want to replace our single
Bubble
object with a
vector
of
Bubble
s. Before your draw loop, create a vector
of five bubbles initially. Create each
bubble with the following random properties:
- Give each Bubble a random radius between 10 and 50.
- Give each Bubble a random starting position for X and Y so the bubble will be created within the window and not on an edge.
- Give each Bubble a random direction for X and Y in the range
[-0.1667, 0.1667]
. - Give each Bubble a random color so we can tell them apart.
Be sure to be using the Mersenne-Twister as appropriate to generate the random numbers in the appropriate ranges.
Inside our draw loop, we now need to draw all the Bubbles in our vector. After our event handling, we'll then need to update the positions of all the Bubbles in our vector. You should now see five Bubbles bouncing around the window. Excellent. Let's have the user interact.
Pop-A-Bubble
When the user presses the space bar, we want to create a new bubble at a random position within our window. This new bubble should have the same random starting properties that our original bubbles did. After the user presses space the first time, we should see six bubbles moving around the window. A second press of space, seven bubbles. And so forth as the user continues to press space. Do not let there ever be more than 10 bubbles. If the user presses space eleven times in a row, then there should be ten bubbles.
We may get to the point where there are too many Bubbles on the
screen. If the user clicks the mouse button and if there is a Bubble under the mouse, then we want to remove
that Bubble from the vector. Create a public method Bubble::checkClicked()
that accepts the Mouse
X and Y location as parameters. This method should return true if the (X, Y) coordinate is inside the circle,
and false otherwise. Inside the mouse button press event handler, call this method on every Bubble inside our
vector and remove the clicked bubbles. It is possible a well-timed click could remove more than one Bubble.
If the user starts your program, presses space twice, then clicks on three Bubbles, we should be seeing four bubbles on the screen. An example of the completed program is shown in the video below:
UI Design
As a final step to make the program more user friendly, if the user presses the Q or Escape key, automatically close the window.
Hints
- To measure time in SFML
- Checking if a click is inside a circle is a geometry problem. What can the distance from our circle to the click point tell us?
Extra Credit!!
To complete the extra credit, turn your pop-a-bubble simulation into a game. Complete the following three tasks:
- Keep track of the score. When the user clicks a Bubble, increment the score by one. Display the score in the window.
- Display in the window how many bubbles currently exist.
- Display a timer of how much time remains in the game. The timer starts at 10 seconds and decreases in real time (second increments is fine). When the timer reaches zero, the animation of bubbles moving needs to stop. Bubbles can no longer be added or deleted.
An example of the completed extra credit is demonstrated in the video below:
The following are not required, but possible extensions if you want to keep this game expanding:
- After the game ends, if the user presses the R key, then restart the game. Score is back to zero and timer back to ten.
- Keep track of the current high score using File I/O. If the user sets a new high score, display a message in the window that a new high score was set.
- Put in a cheat mode. If the user is holding the S key down, then all the bubbles slow down making them easier to click. Once S is released, the bubbles return to their original speed. (The user can still press space while S is being held down.)
- Add power-ups! If the user clicks certain colored circles (maybe those that have Green as the largest color value), then add a second to the timer.
- Add enemies! If the user clicks certain colored circles (maybe those that have Red as the largest color value), then take a point away.
- Come up with your own additional ideas!
Grading Rubric
Your submission will be graded according to the following rubric:
Points | Requirement Description |
0.5 | Submitted correctly by Tuesday, April 01, 2025, 11:59 PM |
0.5 | Project builds without errors nor warnings. |
2.0 | Best Practices and Style Guide followed. |
0.5 | Program follows specified user I/O flow. |
0.5 | Public and private tests successfully passed. |
2.0 | Fully meets specifications. |
6.00 | Total Points |
Extra Credit Points | Requirement Description |
+1.0 | Pop-A-Bubble 10-Second Game Implemented |
Submission
Always, always, ALWAYS update the header comments at the top of your main.cpp file. And if you ever get stuck, remember that there is LOTS of help available.
Zip together your main.cpp, Makefile, *.h, *.hpp, *.cpp, data/*
files and name the zip file A4.zip
. Upload this zip file to Canvas under A4.
→This assignment is due by Tuesday, April 01, 2025, 11:59 PM.←
→ As with all assignments, this must be an individual effort and cannot be pair programmed. Any debugging assistance must follow the course collaboration policy and be cited in the comment header block for the assignment.←