The NEW Pong Game V13.2.1
An interesting implemnettaion of the pong game
Loading...
Searching...
No Matches
BallBase Class Referenceabstract

Abstract base class for all ball types in the game. More...

#include <ball_base.hpp>

+ Inheritance diagram for BallBase:

Public Member Functions

 BallBase (float size=35.0f)
 Constructor for BallBase.
 
virtual SDL_Rect boundaries () const
 Gets the collision boundaries of the ball.
 
SDL_Color get_color () const
 Gets the color of the ball.
 
int get_game_mode () const
 
float get_pos_x () const
 Gets the x-coordinate of the ball.
 
float get_pos_y () const
 Gets the y-coordinate of the ball.
 
float get_vel_x () const
 Gets the x-velocity component of the ball.
 
float get_vel_y () const
 Gets the y-velocity component of the ball.
 
void random_direction_angle ()
 Randomizes the direction of the ball.
 
virtual void render_object (SDL_Renderer *renderer)=0
 Pure virtual method to render the ball.
 
virtual void reset ()
 Resets the ball to the center of the screen.
 
void set_color (const SDL_Color &new_color)
 Sets the color of the ball.
 
void set_game_mode (int mode)
 
void set_position (float x, float y)
 Sets the position of the ball.
 
void set_velocity (float velX, float velY)
 Sets the velocity of the ball.
 
virtual void update (float travel_time, class Paddle *paddle1, class Paddle *paddle2, User *player1, User *player2)
 Updates the ball's position and handles collisions.
 
virtual ~BallBase ()
 Virtual destructor for proper cleanup in derived classes.
 

Protected Attributes

const float ball_size
 
SDL_Color color
 
int game_mode = -1
 
float pos_x
 
float pos_y
 
float vel_x
 
float vel_y
 

Detailed Description

Abstract base class for all ball types in the game.

This class defines the common interface and functionality for all types of balls in the game. It handles movement, collision detection, and score updates.

Constructor & Destructor Documentation

◆ BallBase()

BallBase::BallBase ( float size = 35.0f)

Constructor for BallBase.

Parameters
sizeThe size (diameter) of the ball, defaults to 15.0f

Initializes the ball at the center of the screen with a specified size and randomizes its initial direction

Parameters
sizeThe size (diameter) of the ball

◆ ~BallBase()

virtual BallBase::~BallBase ( )
virtual

Virtual destructor for proper cleanup in derived classes.

Member Function Documentation

◆ boundaries()

virtual SDL_Rect BallBase::boundaries ( ) const
virtual

Gets the collision boundaries of the ball.

Returns a rectangle representing the ball's position and size for collision detection purposes.

Returns
SDL_Rect representing the ball's boundaries

◆ get_color()

SDL_Color BallBase::get_color ( ) const

Gets the color of the ball.

Returns
The ball's color as an SDL_Color structure

◆ get_game_mode()

int BallBase::get_game_mode ( ) const

◆ get_pos_x()

float BallBase::get_pos_x ( ) const

Gets the x-coordinate of the ball.

Returns
The ball's x position

◆ get_pos_y()

float BallBase::get_pos_y ( ) const

Gets the y-coordinate of the ball.

Returns
The ball's y position

◆ get_vel_x()

float BallBase::get_vel_x ( ) const

Gets the x-velocity component of the ball.

Returns
The ball's velocity along the x-axis

◆ get_vel_y()

float BallBase::get_vel_y ( ) const

Gets the y-velocity component of the ball.

Returns
The ball's velocity along the y-axis

◆ random_direction_angle()

void BallBase::random_direction_angle ( )

Randomizes the direction of the ball.

Changes the ball's velocity to move in a random direction

Sets the ball's velocity to move in a random direction with a random angle between -45 and 45 degrees

◆ render_object()

virtual void BallBase::render_object ( SDL_Renderer * renderer)
pure virtual

Pure virtual method to render the ball.

Must be implemented by derived classes to draw their specific ball shape

Parameters
rendererThe SDL renderer to use for drawing

Implemented in ClassicBall, SquareBall, and TriangleBall.

◆ reset()

void BallBase::reset ( )
virtual

Resets the ball to the center of the screen.

Repositions the ball and randomizes its direction

Repositions the ball to the center and randomizes its direction

◆ set_color()

void BallBase::set_color ( const SDL_Color & new_color)

Sets the color of the ball.

Parameters
new_colorThe new color as an SDL_Color

◆ set_game_mode()

void BallBase::set_game_mode ( int mode)

◆ set_position()

void BallBase::set_position ( float x,
float y )

Sets the position of the ball.

Parameters
xThe new x-coordinate
yThe new y-coordinate

◆ set_velocity()

void BallBase::set_velocity ( float velX,
float velY )

Sets the velocity of the ball.

Parameters
velXThe new x-velocity component
velYThe new y-velocity component

◆ update()

void BallBase::update ( float travel_time,
class Paddle * paddle1,
class Paddle * paddle2,
User * player1,
User * player2 )
virtual

Updates the ball's position and handles collisions.

Moves the ball according to its velocity, checks for collisions with walls and paddles, and updates player scores when necessary.

Parameters
travel_timeTime delta since last update (in seconds)
paddle1Pointer to the first paddle
paddle2Pointer to the second paddle
player1Pointer to the first player's data
player2Pointer to the second player's data

Member Data Documentation

◆ ball_size

const float BallBase::ball_size
protected

Ball's size (diameter by default)

◆ color

SDL_Color BallBase::color
protected

Ball's color

◆ game_mode

int BallBase::game_mode = -1
protected

◆ pos_x

float BallBase::pos_x
protected

Ball's x position

◆ pos_y

float BallBase::pos_y
protected

Ball's y position

◆ vel_x

float BallBase::vel_x
protected

Ball's x velocity

◆ vel_y

float BallBase::vel_y
protected

Ball's y velocity


The documentation for this class was generated from the following files: