Using the Ball class
To put the ball into action, add the following code to make the Ball
class available in the main
function:
#include "Ball.h"
Add the following highlighted line of code to declare and initialize an instance of the Ball
class using the constructor function that we have just coded:
// Create a bat
Bat bat(1920 / 2, 1080 - 20);
// Create a ball
Ball ball(1920 / 2, 0);
// Create a Text object called HUD
Text hud;
Add the following code, positioned exactly as highlighted:
/*
Update the bat, the ball and the HUD
****************************************************
****************************************************
****************************************************
*/
// Update the delta time
Time dt = clock.restart();
bat.update(dt);
ball.update(dt);
// Update the HUD text
std::stringstream ss;
ss << "Score:" << score << " Lives:" << lives;
hud.setString(ss.str());
In the preceding code, we...