Coding the Ball class
To get started, we will code the header file. Right-click on Header Files in the Solution Explorer window and select ADD | New Item. Next, choose the Header File (.h) option and name the new file Ball.h
. Click the Add button. Now, we are ready to code the file.
Add the following code to Ball.h
:
#pragma once #include <SFML/Graphics.hpp> using namespace sf; class Ball { private: Â Â Â Â Vector2f m_Position; Â Â Â Â Â Â Â RectangleShape m_Shape; Â Â Â Â float m_Speed = 300.0f; Â Â Â Â float m_DirectionX = .2f; Â Â Â Â float m_DirectionY = .2f; public: Â Â Â Â Ball(float startX, float startY); Â Â Â Â FloatRect getPosition(); Â Â Â Â RectangleShape getShape(); Â Â Â Â float getXVelocity(); Â Â Â Â void reboundSides(); Â Â Â Â void reboundBatOrTop(); Â Â Â ...