In this article, we will discuss the Compute the Ping Pong Game in C++ with its requirements and examples.
Ping Pong Game:
A graphics library such as SFML or SDL is usually used to handle rendering, user input, and game mechanics when creating a Ping Pong game in C++ . The game consists of a window with two paddles on the left and right, as well as a moving ball that bounces off the top and bottom edges of the screen and the paddles. Players use keyboard inputs to operate the paddles, such as the Up and Down arrow keys for the right paddle and the 'W' and 'S' for the left. Since the opponent scores a point if the ball misses the paddle, the object is to keep it from passing beyond it.
Each player's score is recorded by the game and typically displayed on the screen or in the console. The ball changes its course and returns to the center after a player scores. Players can participate in the game until the window is manually closed because it runs in an indefinite loop. A fun and captivating gameplay experience is ensured by the ball movement dynamics, collision detection, and fluid paddle control.
Requirements:
- Graphics: Fundamental elements like paddles, a ball, a net, and player scores must be rendered for the game to function . On opposing sides of the screen, each player's paddle should appear as a white rectangle. The ball is shown as a white square, the score is shown as white digits above each player's zone, and a dashed line should be placed in the middle to indicate a net.
- Controls: The W and S keys should be used by Player One to control their paddle, and the Up and Down arrow keys by Player Two. To keep the paddles from escaping the playing area, they must be confined inside the screen's borders. This ensures equitable gameplay and stops excessive movement from causing unwanted behavior.
- Ball Interactions: The ball begins the game in the middle of the screen and travels to the right. Depending on where it hits, it should change direction after bouncing off the top and bottom screen boundaries. Whether a ball hits the top, middle, or bottom third of a paddle determines the angle at which it will reflect.
- Scoring System: When the ball crosses the left or right screen boundary and passes beyond a player's paddle, a point is given. Player Two receives a point if Player One misses, and vice versa. Once a point is scored, the ball passes toward the player who lost the point and resets to the center of the screen.
- Audio Effects: Playing sound effects anytime the ball hits a paddle or wall will improve the gaming experience. In order to enhance acoustic feedback, the paddle strikes sound effect should be slightly different from the wall collision sound. The realistic feeling and improved player immersion are provided by these effects.
Example:
Let us take an example to illustrate the Ping Pong Game in C++.
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
// Window dimensions
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;
// Paddle settings
const float PADDLE_WIDTH = 10.0f;
const float PADDLE_HEIGHT = 100.0f;
const float PADDLE_SPEED = 5.0f;
// Ball settings
const float BALL_RADIUS = 8.0f;
float ballSpeedX = 4.0f, ballSpeedY = 4.0f;
// Score variables
int player1Score = 0, player2Score = 0;
// Function to reset the ball after scoring
void resetBall(sf::CircleShape &ball)
{
ball.setPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);
ballSpeedX = -ballSpeedX; // Reverse direction
}
int main()
{
// Create the game window
sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "Ping Pong Game");
// Create paddles
sf::RectangleShape paddle1(sf::Vector2f(PADDLE_WIDTH, PADDLE_HEIGHT));
sf::RectangleShape paddle2(sf::Vector2f(PADDLE_WIDTH, PADDLE_HEIGHT));
paddle1.setPosition(20, WINDOW_HEIGHT / 2 - PADDLE_HEIGHT / 2);
paddle2.setPosition(WINDOW_WIDTH - 30, WINDOW_HEIGHT / 2 - PADDLE_HEIGHT / 2);
// Create ball
sf::CircleShape ball(BALL_RADIUS);
ball.setPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);
// Game loop
while (window.isOpen())
{
// Handle events
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
// Paddle 1 movement (W - Up, S - Down)
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W) && paddle1.getPosition().y > 0)
paddle1.move(0, -PADDLE_SPEED);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S) && paddle1.getPosition().y + PADDLE_HEIGHT < WINDOW_HEIGHT)
paddle1.move(0, PADDLE_SPEED);
// Paddle 2 movement (Up Arrow - Up, Down Arrow - Down)
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && paddle2.getPosition().y > 0)
paddle2.move(0, -PADDLE_SPEED);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && paddle2.getPosition().y + PADDLE_HEIGHT < WINDOW_HEIGHT)
paddle2.move(0, PADDLE_SPEED);
// Move the ball
ball.move(ballSpeedX, ballSpeedY);
// Ball collision with top and bottom
if (ball.getPosition().y <= 0 || ball.getPosition().y >= WINDOW_HEIGHT - BALL_RADIUS * 2)
ballSpeedY = -ballSpeedY;
// Ball collision with paddles
if (ball.getGlobalBounds().intersects(paddle1.getGlobalBounds()) ||
ball.getGlobalBounds().intersects(paddle2.getGlobalBounds())) {
ballSpeedX = -ballSpeedX; // Reverse ball direction
}
// Scoring conditions
if (ball.getPosition().x <= 0)
{ // Player 2 scores
player2Score++;
std::cout << "Player 2 Score: " << player2Score << " | Player 1 Score: " << player1Score << std::endl;
resetBall(ball);
}
if (ball.getPosition().x >= WINDOW_WIDTH)
{ // Player 1 scores
player1Score++;
std::cout << "Player 1 Score: " << player1Score << " | Player 2 Score: " << player2Score << std::endl;
resetBall(ball);
}
// Render objects
window.clear(sf::Color::Black);
window.draw(paddle1);
window.draw(paddle2);
window.draw(ball);
window.display();
}
return 0;
}
Output:
Player 2 Score: 1 | Player 1 Score: 0
Player 1 Score: 1 | Player 2 Score: 1
Player 2 Score: 2 | Player 1 Score: 1
Player 2 Score: 3 | Player 1 Score: 1