Introduction
Pong is one of the most classic video games of all time. It involves two paddles and a ball, where the objective is to prevent the ball from passing your paddle while trying to score against your opponent. In this tutorial, we will walk you through how to create a simple Pong game using the C++ programming language.
Objective:
The goal of this project is to create a playable Pong game that you can run and enjoy directly from the console. The program will feature a ball, paddles for two players, and the ability to track scores.
Code: Simple Pong Game in C++
#include #include #include using namespace std; // Global variables int width = 40; int height = 20; int ballX, ballY, ballDX = 1, ballDY = 1; int paddle1Y, paddle2Y; int score1 = 0, score2 = 0; bool quit = false; // Function to draw the game board void DrawBoard() { system("cls"); for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (x == 0) cout << "#"; // left wall else if (x == width - 1) cout << "#"; // right wall else if (x == ballX && y == ballY) cout << "O"; // ball else if (x == 1 && y >= paddle1Y && y < paddle1Y + 4) cout << "|"; // paddle 1 else if (x == width - 2 && y >= paddle2Y && y < paddle2Y + 4) cout << "|"; // paddle 2 else cout << " "; } cout << endl; } for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; cout << "Player 1 Score: " << score1 << " Player 2 Score: " << score2 << endl; } // Function to update the ball's position void UpdateBall() { ballX += ballDX; ballY += ballDY; // Ball bounce logic if (ballY == 0 || ballY == height - 1) ballDY = -ballDY; // top and bottom walls if (ballX == 1 && ballY >= paddle1Y && ballY < paddle1Y + 4) ballDX = -ballDX; // paddle 1 if (ballX == width - 2 && ballY >= paddle2Y && ballY < paddle2Y + 4) ballDX = -ballDX; // paddle 2 // Scoring logic if (ballX == 0) { score2++; ballX = width / 2; ballY = height / 2; } // player 2 scores if (ballX == width - 1) { score1++; ballX = width / 2; ballY = height / 2; } // player 1 scores } // Function to move paddles void MovePaddles() { if (_kbhit()) { char current = _getch(); if (current == 'w' && paddle1Y > 0) paddle1Y--; // move paddle 1 up if (current == 's' && paddle1Y + 4 < height) paddle1Y++; // move paddle 1 down if (current == 'i' && paddle2Y > 0) paddle2Y--; // move paddle 2 up if (current == 'k' && paddle2Y + 4 < height) paddle2Y++; // move paddle 2 down if (current == 'q') quit = true; // quit the game } } // Main function to run the game loop int main() { ballX = width / 2; ballY = height / 2; paddle1Y = height / 2 - 2; paddle2Y = height / 2 - 2; while (!quit) { DrawBoard(); UpdateBall(); MovePaddles(); Sleep(10); // Control the speed of the game } cout << "Game Over!" << endl; return 0; }
Explanation of the Program
This program creates a simple Pong game in C++ with two paddles and a ball. It is designed to be played in the console. Here’s a breakdown of the program structure:
- Global Variables: The program uses variables to store the dimensions of the game board, the ball’s position, paddle positions, and scores.
- DrawBoard Function: This function clears the console screen and then redraws the game board with the ball, paddles, and walls. It also displays the current scores.
- UpdateBall Function: This function updates the ball’s position and handles the bouncing of the ball when it hits the walls or paddles. It also checks for scoring conditions.
- MovePaddles Function: This function allows the players to control their paddles. Player 1 uses ‘W’ (up) and ‘S’ (down), while Player 2 uses ‘I’ (up) and ‘K’ (down). The game can be quit by pressing ‘Q’.
- Main Loop: The main loop of the game continuously updates the board, moves the paddles, and handles user input until the player decides to quit.
How to Run the Program:
To run the Pong game on your system:
- Ensure you have a C++ compiler installed, such as GCC or MinGW.
- Save the code in a file named pong.cpp.
- Open a terminal and compile the code using the following command:
g++ pong.cpp -o pong
- Run the compiled program:
./pong